| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "time" |
| 8 | "unicode" |
| 9 | "unicode/utf8" |
| 10 | ) |
| 11 | |
| 12 | // Limits of one render call (spec section 3, "Contract" and "Sandbox"). |
| 13 | const ( |
| 14 | // CallTimeout bounds one call: launch, every page and the report. |
| 15 | // Past it the sandbox is killed and the call is CodeTimeout. |
| 16 | CallTimeout = 60 * time.Second |
| 17 | // MemoryLimit bounds the sandbox's memory: every process's data |
| 18 | // segment (RLIMIT_DATA, set before the browser starts), and the sum |
| 19 | // of every process's resident memory, watched by the runner. Past it |
| 20 | // the sandbox is killed and the call is CodeCrashed. |
| 21 | MemoryLimit = 2 << 30 |
| 22 | // MaxPages is the most pages one call renders. |
| 23 | MaxPages = 6 |
| 24 | // MaxCaptureHeight clips a full-page capture, in CSS pixels. |
| 25 | MaxCaptureHeight = 10000 |
| 26 | // IdleWait is the longest a page waits after its load event for the |
| 27 | // network to go idle. |
| 28 | IdleWait = 5 * time.Second |
| 29 | |
| 30 | maxConsoleErrors = 20 |
| 31 | maxFailedLoads = 30 |
| 32 | maxBrokenLinks = 30 |
| 33 | maxOutline = 8 << 10 |
| 34 | |
| 35 | // Bounds on single strings from the page. |
| 36 | maxTitle = 300 |
| 37 | maxMessage = 1000 |
| 38 | maxURL = 2000 |
| 39 | maxName = 200 |
| 40 | ) |
| 41 | |
| 42 | // Error codes of a call the driver itself ends (the launch's are |
| 43 | // release.RendererError; the rest are render_page's, task 10). |
| 44 | const ( |
| 45 | CodeTimeout = "timeout" |
| 46 | CodeCrashed = "crashed" |
| 47 | ) |
| 48 | |
| 49 | // Error is a call that ended with one of the fixed codes. Detail is a |
| 50 | // diagnostic for the runner's log, never shown as page content. |
| 51 | type Error struct { |
| 52 | Code string |
| 53 | Detail string |
| 54 | } |
| 55 | |
| 56 | func (e *Error) Error() string { |
| 57 | if e.Detail == "" { |
| 58 | return "render: " + e.Code |
| 59 | } |
| 60 | return fmt.Sprintf("render: %s (%s)", e.Code, e.Detail) |
| 61 | } |
| 62 | |
| 63 | // ErrorCode is err's code when it is an *Error, else "". |
| 64 | func ErrorCode(err error) string { |
| 65 | var e *Error |
| 66 | if errors.As(err, &e) { |
| 67 | return e.Code |
| 68 | } |
| 69 | return "" |
| 70 | } |
| 71 | |
| 72 | // Viewport is one of the fixed viewports. |
| 73 | type Viewport string |
| 74 | |
| 75 | const ( |
| 76 | Phone Viewport = "phone" |
| 77 | Tablet Viewport = "tablet" |
| 78 | Desktop Viewport = "desktop" |
| 79 | ) |
| 80 | |
| 81 | // Size is the viewport's width and height in CSS pixels; ok is false |
| 82 | // for a viewport that is not one of the fixed set. |
| 83 | func (v Viewport) Size() (width, height int, ok bool) { |
| 84 | switch v { |
| 85 | case Phone: |
| 86 | return 390, 844, true |
| 87 | case Tablet: |
| 88 | return 834, 1194, true |
| 89 | case Desktop: |
| 90 | return 1440, 900, true |
| 91 | } |
| 92 | return 0, 0, false |
| 93 | } |
| 94 | |
| 95 | // mobile is whether the viewport is emulated as a mobile device (the |
| 96 | // page's viewport meta tag applies). |
| 97 | func (v Viewport) mobile() bool { return v == Phone || v == Tablet } |
| 98 | |
| 99 | // Page is one page to render: a site path, a viewport and whether the |
| 100 | // capture is the full page (clipped at MaxCaptureHeight) or the viewport. |
| 101 | type Page struct { |
| 102 | Path string |
| 103 | Viewport Viewport |
| 104 | FullPage bool |
| 105 | } |
| 106 | |
| 107 | // ReportSchema is the report's schema version. A change to the JSON |
| 108 | // below is a new version. |
| 109 | const ReportSchema = 1 |
| 110 | |
| 111 | // ReportName is the report's file name in the out directory. |
| 112 | const ReportName = "report.json" |
| 113 | |
| 114 | // Report is what one call wrote: report.json in the out directory, beside |
| 115 | // each page's capture. Every string in it came from the page and is |
| 116 | // page content: bounded, with control characters removed, never trusted. |
| 117 | type Report struct { |
| 118 | Schema int `json:"schema"` |
| 119 | // Error is CodeTimeout or CodeCrashed for a call that ended early; |
| 120 | // Pages then holds the pages finished before it. |
| 121 | Error string `json:"error,omitempty"` |
| 122 | ElapsedMS int64 `json:"elapsed_ms"` |
| 123 | Pages []PageReport `json:"pages"` |
| 124 | } |
| 125 | |
| 126 | // PageReport is one rendered page. |
| 127 | type PageReport struct { |
| 128 | Path string `json:"path"` |
| 129 | Viewport Viewport `json:"viewport"` |
| 130 | Width int `json:"width"` |
| 131 | Height int `json:"height"` |
| 132 | FullPage bool `json:"full_page"` |
| 133 | // Capture is the PNG's file name in the out directory, as the |
| 134 | // browser wrote it; CaptureWidth and CaptureHeight are its size. |
| 135 | Capture string `json:"capture"` |
| 136 | CaptureWidth int `json:"capture_width"` |
| 137 | CaptureHeight int `json:"capture_height"` |
| 138 | Title string `json:"title"` |
| 139 | // ConsoleErrors are the first maxConsoleErrors; Omitted counts the |
| 140 | // rest, repeats included. The same for failed loads (a repeat of |
| 141 | // one listed is not counted) and broken links. |
| 142 | ConsoleErrors []string `json:"console_errors"` |
| 143 | ConsoleErrorsOmitted int `json:"console_errors_omitted"` |
| 144 | FailedLoads []FailedLoad `json:"failed_loads"` |
| 145 | FailedLoadsOmitted int `json:"failed_loads_omitted"` |
| 146 | // BrokenLinks are same-site anchors whose URL answered 404, of |
| 147 | // LinksChecked distinct same-site anchors. |
| 148 | BrokenLinks []string `json:"broken_links"` |
| 149 | BrokenLinksOmitted int `json:"broken_links_omitted"` |
| 150 | LinksChecked int `json:"links_checked"` |
| 151 | // Outline is the accessibility tree, one node a line, indented by |
| 152 | // depth, cut at maxOutline bytes (OutlineCut). |
| 153 | Outline string `json:"outline"` |
| 154 | OutlineCut bool `json:"outline_cut"` |
| 155 | } |
| 156 | |
| 157 | // Kinds of failed load. |
| 158 | const ( |
| 159 | LoadExternalBlocked = "external blocked" |
| 160 | LoadNotFound = "not found" |
| 161 | LoadFont = "font" |
| 162 | ) |
| 163 | |
| 164 | // FailedLoad is a resource the page asked for that did not load: Kind |
| 165 | // is one of the three kinds, Status the HTTP status when one came. |
| 166 | type FailedLoad struct { |
| 167 | Kind string `json:"kind"` |
| 168 | URL string `json:"url"` |
| 169 | Status int `json:"status,omitempty"` |
| 170 | } |
| 171 | |
| 172 | // Clean is clean, for the reply that quotes page text again. |
| 173 | func Clean(s string, max int) string { return clean(s, max) } |
| 174 | |
| 175 | // clean makes page text safe to hold: invalid UTF-8 and control |
| 176 | // characters (line breaks included) become spaces, runs of space |
| 177 | // collapse, and the result is at most max bytes, cut at a rune. |
| 178 | func clean(s string, max int) string { |
| 179 | var b strings.Builder |
| 180 | space := false |
| 181 | for _, r := range s { |
| 182 | if r == utf8.RuneError || unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || unicode.IsSpace(r) { |
| 183 | if !space && b.Len() > 0 { |
| 184 | space = true |
| 185 | } |
| 186 | continue |
| 187 | } |
| 188 | need := utf8.RuneLen(r) |
| 189 | if space { |
| 190 | need++ |
| 191 | } |
| 192 | if b.Len()+need > max { |
| 193 | break |
| 194 | } |
| 195 | if space { |
| 196 | b.WriteByte(' ') |
| 197 | space = false |
| 198 | } |
| 199 | b.WriteRune(r) |
| 200 | } |
| 201 | return b.String() |
| 202 | } |
| 203 | |