Skip to content

Commit

Permalink
use registry.npmmirror.com to download the chromium
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Feb 8, 2022
1 parent 5330e09 commit e0d0a7e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
20 changes: 11 additions & 9 deletions lib/launcher/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import (
type Host func(revision int) string

var hostConf = map[string]struct {
zipName string
urlPrefix string
zipName string
}{
"darwin": {"chrome-mac.zip", "Mac"},
"linux": {"chrome-linux.zip", "Linux_x64"},
"windows": {"chrome-win.zip", "Win"},
}[runtime.GOOS]
"darwin_amd64": {"Mac", "chrome-mac.zip"},
"darwin_arm64": {"Mac_Arm", "chrome-mac.zip"},
"linux_amd64": {"Linux_x64", "chrome-linux.zip"},
"windows_386": {"Win", "chrome-win.zip"},
"windows_amd64": {"Win_x64", "chrome-win.zip"},
}[runtime.GOOS+"_"+runtime.GOARCH]

// HostGoogle to download browser
func HostGoogle(revision int) string {
Expand All @@ -42,10 +44,10 @@ func HostGoogle(revision int) string {
)
}

// HostTaobao to download browser
func HostTaobao(revision int) string {
// HostNPM to download browser
func HostNPM(revision int) string {
return fmt.Sprintf(
"https://npm.taobao.org/mirrors/chromium-browser-snapshots/%s/%d/%s",
"https://registry.npmmirror.com/-/binary/chromium-browser-snapshots/%s/%d/%s",
hostConf.urlPrefix,
revision,
hostConf.zipName,
Expand Down Expand Up @@ -85,7 +87,7 @@ func NewBrowser() *Browser {
return &Browser{
Context: context.Background(),
Revision: DefaultRevision,
Hosts: []Host{HostGoogle, HostTaobao},
Hosts: []Host{HostGoogle, HostNPM},
Dir: DefaultBrowserDir,
Logger: os.Stdout,
LockPort: defaults.LockPort,
Expand Down
2 changes: 1 addition & 1 deletion lib/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test(t *testing.T) {

func (t T) DownloadHosts() {
t.Has(launcher.HostGoogle(launcher.DefaultRevision), "https://storage.googleapis.com/chromium-browser-snapshots")
t.Has(launcher.HostTaobao(launcher.DefaultRevision), "https://npm.taobao.org/mirrors/chromium-browser-snapshots")
t.Has(launcher.HostNPM(launcher.DefaultRevision), "https://registry.npmmirror.com/-/binary/chromium-browser-snapshots")
}

func (t T) Download() {
Expand Down
28 changes: 11 additions & 17 deletions lib/launcher/revision/main.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"strings"

"github.com/go-rod/rod/lib/utils"
)

var (
// MirrorChromium to fetch the latest chromium version
MirrorChromium = "https://npm.taobao.org/mirrors/chromium-browser-snapshots/Linux_x64/"
// MirrorChromiumRegExp to match the MirrorChromium html source
MirrorChromiumRegExp = regexp.MustCompile(`\Q"/mirrors/chromium-browser-snapshots/Linux_x64/\E(\d+)`)
)
const mirror = "https://registry.npmmirror.com/-/binary/chromium-browser-snapshots/Mac_Arm/"

var slash = filepath.FromSlash

func main() {
res, err := http.Get(MirrorChromium)
res, err := http.Get(mirror)
utils.E(err)
defer func() { _ = res.Body.Close() }()

str, err := ioutil.ReadAll(res.Body)
var data interface{}
err = json.NewDecoder(res.Body).Decode(&data)
utils.E(err)

matchs := MirrorChromiumRegExp.FindAllStringSubmatch(string(str), -1)
if len(matchs) <= 0 {
utils.E(fmt.Errorf("cannot match version of the latest chromium from %s", MirrorChromium))
}

revision := matchs[len(matchs)-1][1]
list := data.([]interface{})
latest := list[len(list)-1].(map[string]interface{})["name"].(string)
revision := strings.TrimRight(latest, "/")

if revision == "" {
utils.E(fmt.Errorf("empty version of the latest chromium %s", revision))
if !regexp.MustCompile(`^\d+$`).MatchString(revision) {
utils.E(fmt.Errorf("cannot match version of the latest chromium from %s", mirror))
}

build := utils.S(`// generated by "lib/launcher/revision"
Expand Down

0 comments on commit e0d0a7e

Please sign in to comment.