| 1 |
// Package main ... |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net/http" |
| 10 |
"path/filepath" |
| 11 |
"sort" |
| 12 |
"strconv" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"github.com/go-rod/rod/lib/utils" |
| 16 |
"github.com/ysmood/gson" |
| 17 |
) |
| 18 |
|
| 19 |
const mirror = "https://registry.npmmirror.com/-/binary/chromium-browser-snapshots/" |
| 20 |
|
| 21 |
func main() { |
| 22 |
list := getList(mirror) |
| 23 |
|
| 24 |
revLists := [][]int{} |
| 25 |
for _, os := range list { |
| 26 |
// skip win32 |
| 27 |
if os == "Win" { |
| 28 |
continue |
| 29 |
} |
| 30 |
|
| 31 |
revList := []int{} |
| 32 |
for _, s := range getList(mirror + os + "/") { |
| 33 |
rev, err := strconv.ParseInt(s, 10, 32) |
| 34 |
if err != nil { |
| 35 |
log.Fatal(err) |
| 36 |
} |
| 37 |
revList = append(revList, int(rev)) |
| 38 |
} |
| 39 |
sort.Ints(revList) |
| 40 |
revLists = append(revLists, revList) |
| 41 |
} |
| 42 |
|
| 43 |
rev := largestCommonRevision(revLists) |
| 44 |
|
| 45 |
if rev < 969819 { |
| 46 |
utils.E(fmt.Errorf("cannot match version of the latest chromium from %s", mirror)) |
| 47 |
} |
| 48 |
|
| 49 |
playwright := getFromPlaywright() |
| 50 |
|
| 51 |
out := utils.S(`// generated by "lib/launcher/revision" |
| 52 |
|
| 53 |
package launcher |
| 54 |
|
| 55 |
// RevisionDefault for chromium. |
| 56 |
const RevisionDefault = {{.default}} |
| 57 |
|
| 58 |
// RevisionPlaywright for arm linux. |
| 59 |
const RevisionPlaywright = {{.playwright}} |
| 60 |
`, |
| 61 |
"default", rev, |
| 62 |
"playwright", playwright, |
| 63 |
) |
| 64 |
|
| 65 |
utils.E(utils.OutputFile(filepath.FromSlash("lib/launcher/revision.go"), out)) |
| 66 |
} |
| 67 |
|
| 68 |
func getList(path string) []string { |
| 69 |
res, err := http.Get(path) |
| 70 |
utils.E(err) |
| 71 |
defer func() { _ = res.Body.Close() }() |
| 72 |
|
| 73 |
var data interface{} |
| 74 |
err = json.NewDecoder(res.Body).Decode(&data) |
| 75 |
utils.E(err) |
| 76 |
|
| 77 |
list := data.([]interface{}) |
| 78 |
|
| 79 |
names := []string{} |
| 80 |
for _, it := range list { |
| 81 |
name := it.(map[string]interface{})["name"].(string) |
| 82 |
names = append(names, strings.TrimRight(name, "/")) |
| 83 |
} |
| 84 |
|
| 85 |
return names |
| 86 |
} |
| 87 |
|
| 88 |
func largestCommonRevision(revLists [][]int) int { |
| 89 |
sort.Slice(revLists, func(i, j int) bool { |
| 90 |
return len(revLists[i]) < len(revLists[j]) |
| 91 |
}) |
| 92 |
|
| 93 |
shortest := revLists[0] |
| 94 |
|
| 95 |
for i := len(shortest) - 1; i >= 0; i-- { |
| 96 |
r := shortest[i] |
| 97 |
|
| 98 |
isCommon := true |
| 99 |
for i := 1; i < len(revLists); i++ { |
| 100 |
if !has(revLists[i], r) { |
| 101 |
isCommon = false |
| 102 |
break |
| 103 |
} |
| 104 |
} |
| 105 |
if isCommon { |
| 106 |
return r |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return 0 |
| 111 |
} |
| 112 |
|
| 113 |
func has(list []int, i int) bool { |
| 114 |
index := sort.SearchInts(list, i) |
| 115 |
return index < len(list) && list[index] == i |
| 116 |
} |
| 117 |
|
| 118 |
func getFromPlaywright() int { |
| 119 |
pv := strings.TrimSpace(utils.ExecLine(false, "npm --no-update-notifier -s show playwright version")) |
| 120 |
out := fetch(fmt.Sprintf( |
| 121 |
"https://raw.githubusercontent.com/microsoft/playwright/v%s/packages/playwright-core/browsers.json", |
| 122 |
pv)) |
| 123 |
rev, err := strconv.ParseInt(gson.NewFrom(out).Get("browsers.0.revision").Str(), 10, 32) |
| 124 |
utils.E(err) |
| 125 |
return int(rev) |
| 126 |
} |
| 127 |
|
| 128 |
func fetch(u string) string { |
| 129 |
res, err := http.Get(u) |
| 130 |
utils.E(err) |
| 131 |
defer func() { _ = res.Body.Close() }() |
| 132 |
|
| 133 |
b, err := io.ReadAll(res.Body) |
| 134 |
utils.E(err) |
| 135 |
return string(b) |
| 136 |
} |