Skip to content

Commit

Permalink
reduce dependencies
Browse files Browse the repository at this point in the history
This will reduce the binary size. The translator example reduced from 12mb to 8mb.

resolve #187
  • Loading branch information
ysmood committed Aug 30, 2020
1 parent 72f5749 commit 6ee5d7b
Show file tree
Hide file tree
Showing 55 changed files with 1,066 additions and 730 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We also use it in the CI to enforce the minimum test coverage.

### To run inside docker

1. `docker build -t rod -f lib/docker/test.Dockerfile .`
1. `docker build -t rod -f lib/docker/Dockerfile .`

2. `docker run --name rod -itp 9273:9273 -v $(pwd):/t -w /t rod sh`

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/check-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.15

- uses: actions/checkout@v2

Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.15

- uses: actions/checkout@v2

- name: setup
run: |
(cd ~ && GO111MODULE=on go get github.com/ysmood/kit/cmd/godev)
(cd ~ && GO111MODULE=on go get \
github.com/ysmood/kit/cmd/godev \
golang.org/x/tools/cmd/goimports \
github.com/client9/misspell/cmd/misspell \
)
go generate
- name: test
Expand All @@ -49,7 +53,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.15

- uses: actions/checkout@v2

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/lib/assets/node_modules
tmp/
coverage.txt
/rod.test
10 changes: 4 additions & 6 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/go-rod/rod/lib/defaults"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/goob"
"github.com/ysmood/kit"
)

// Browser implements the proto.Caller interface
Expand All @@ -34,7 +34,7 @@ type Browser struct {
ctx context.Context
ctxCancel func()
timeoutCancel func()
sleeper kit.Sleeper
sleeper utils.Sleeper

// BrowserContextID is the id for incognito window
BrowserContextID proto.BrowserBrowserContextID
Expand All @@ -48,8 +48,6 @@ type Browser struct {

defaultViewport *proto.EmulationSetDeviceMetricsOverride

monitorServer *kit.ServerContext

client *cdp.Client
cdpCall CDPCall
event *goob.Observable // all the browser events from cdp client
Expand Down Expand Up @@ -173,7 +171,7 @@ func (b *Browser) Connect() (err error) {

b.client.Context(b.ctx, b.ctxCancel).MustConnect()

b.monitorServer = b.ServeMonitor(defaults.Monitor, !defaults.Blind)
b.ServeMonitor(defaults.Monitor, !defaults.Blind)

b.initEvents()

Expand Down Expand Up @@ -372,7 +370,7 @@ func (b *Browser) PageFromTarget(targetID proto.TargetTargetID) (*Page, error) {
executionIDs: map[proto.PageFrameID]proto.RuntimeExecutionContextID{},
}).Context(context.WithCancel(b.ctx))

page.Mouse = &Mouse{lock: &sync.Mutex{}, page: page, id: kit.RandString(8)}
page.Mouse = &Mouse{lock: &sync.Mutex{}, page: page, id: utils.RandString(8)}
page.Keyboard = &Keyboard{lock: &sync.Mutex{}, page: page}

err := page.initSession()
Expand Down
67 changes: 47 additions & 20 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package rod_test
import (
"context"
"errors"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/tidwall/sjson"
"github.com/ysmood/kit"
)

func (s *S) TestIncognito() {
file := srcFile("fixtures/click.html")
k := kit.RandString(8)
k := utils.RandString(8)

b := s.browser.MustIncognito()
page := b.MustPage(file)
Expand Down Expand Up @@ -90,7 +91,7 @@ func (s *S) TestBrowserCrash() {

wait := browser.WaitEvent(&proto.PageFrameNavigated{})
go func() {
kit.Sleep(0.3)
utils.Sleep(0.3)
_ = proto.BrowserCrash{}.Call(browser)
}()

Expand All @@ -112,23 +113,25 @@ func (s *S) TestMonitor() {
b := rod.New().Timeout(1 * time.Minute).MustConnect()
defer b.MustClose()
p := b.MustPage(srcFile("fixtures/click.html")).MustWaitLoad()
host := b.ServeMonitor("127.0.0.1:0", true).Listener.Addr().String()
host := b.ServeMonitor("127.0.0.1:0", true)

page := s.page.MustNavigate("http://" + host)
page := s.page.MustNavigate(host)
s.Contains(page.MustElement("#targets a").MustParent().MustHTML(), string(p.TargetID))

page.MustNavigate("http://" + host + "/page/" + string(p.TargetID))
page.MustNavigate(host + "/page/" + string(p.TargetID))
s.Contains(page.MustEval(`document.title`).Str, p.TargetID)

s.Equal(400, kit.Req("http://"+host+"/api/page/test").MustResponse().StatusCode)
res, err := http.Get(host + "/api/page/test")
utils.E(err)
s.Equal(400, res.StatusCode)
}

func (s *S) TestRemoteLaunch() {
url, engine, close := serve()
url, mux, close := utils.Serve("")
defer close()

proxy := launcher.NewProxy()
engine.NoRoute(gin.WrapH(proxy))
mux.Handle("/", proxy)

ctx, cancel := context.WithCancel(context.Background())
l := launcher.NewRemote(strings.ReplaceAll(url, "http", "ws"))
Expand Down Expand Up @@ -183,7 +186,7 @@ func (s *S) TestConcurrentOperations() {
list = append(list, item)
}

kit.All(func() {
utils.All(func() {
add(p.MustEval(`new Promise(r => setTimeout(r, 100, 2))`).Int())
}, func() {
add(p.MustEval(`1`).Int())
Expand All @@ -203,10 +206,10 @@ func (s *S) TestPromiseLeak() {
p := s.page.MustNavigate(srcFile("fixtures/click.html"))
var out string

kit.All(func() {
utils.All(func() {
out = p.MustEval(`new Promise(r => setTimeout(() => r(location.href), 200))`).String()
}, func() {
kit.Sleep(0.1)
utils.Sleep(0.1)
p.MustNavigate(srcFile("fixtures/input.html"))
})()

Expand All @@ -233,15 +236,15 @@ func (s *S) TestBlockingNavigation() {
If one page is blocked, other pages should still work.
*/

url, engine, close := serve()
url, mux, close := utils.Serve("")
defer close()
pause, cancel := context.WithCancel(context.Background())
defer cancel()

engine.GET("/a", func(ctx kit.GinContext) {
mux.HandleFunc("/a", func(w http.ResponseWriter, r *http.Request) {
<-pause.Done()
})
engine.GET("/b", ginHTML(`<html>ok</html>`))
mux.HandleFunc("/b", httpHTML(`<html>ok</html>`))

blocked := s.browser.MustPage("")
defer blocked.MustClose()
Expand All @@ -252,28 +255,28 @@ func (s *S) TestBlockingNavigation() {
})
}()

kit.Sleep(0.3)
utils.Sleep(0.3)

p := s.browser.MustPage(url + "/b")
defer p.MustClose()
}

func (s *S) TestResolveBlocking() {
url, engine, close := serve()
url, mux, close := utils.Serve("")
defer close()

pause, cancel := context.WithCancel(context.Background())
defer cancel()

engine.NoRoute(func(ctx kit.GinContext) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
<-pause.Done()
})

p := s.browser.MustPage("")
defer p.MustClose()

go func() {
kit.Sleep(0.1)
utils.Sleep(0.1)
p.MustStopLoading()
}()

Expand Down Expand Up @@ -303,6 +306,30 @@ func (s *S) TestBrowserOthers() {
})
}

func (s *S) TestBinarySize() {
if runtime.GOOS == "windows" {
s.T().SkipNow()
}

cmd := exec.Command("go", "build",
"-trimpath",
"-ldflags", "-w -s",
"-o", "tmp/translator",
"./lib/examples/translator")

cmd.Env = append(os.Environ(), "GOOS=linux")

out, err := cmd.CombinedOutput()
if err != nil {
s.T().Skip(err, string(out))
}

stat, err := os.Stat("tmp/translator")
utils.E(err)

s.Less(float64(stat.Size())/1024/1024, 8.65) // mb
}

// It's obvious that, the v8 will take more time to parse long function.
// For BenchmarkCache and BenchmarkNoCache, the difference is nearly 12% which is too much to ignore.
func BenchmarkCacheOff(b *testing.B) {
Expand Down
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

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

// Context creates a clone with a context that inherits the previous one
Expand Down Expand Up @@ -34,7 +34,7 @@ func (b *Browser) CancelTimeout() *Browser {
}

// Sleeper for chained sub-operations
func (b *Browser) Sleeper(sleeper kit.Sleeper) *Browser {
func (b *Browser) Sleeper(sleeper utils.Sleeper) *Browser {
newObj := *b
newObj.sleeper = sleeper
return &newObj
Expand Down Expand Up @@ -67,7 +67,7 @@ func (p *Page) CancelTimeout() *Page {
}

// Sleeper for chained sub-operations
func (p *Page) Sleeper(sleeper kit.Sleeper) *Page {
func (p *Page) Sleeper(sleeper utils.Sleeper) *Page {
newObj := *p
newObj.sleeper = sleeper
return &newObj
Expand Down Expand Up @@ -100,7 +100,7 @@ func (el *Element) CancelTimeout() *Element {
}

// Sleeper for chained sub-operations
func (el *Element) Sleeper(sleeper kit.Sleeper) *Element {
func (el *Element) Sleeper(sleeper utils.Sleeper) *Element {
newObj := *el
newObj.sleeper = sleeper
return &newObj
Expand Down
Loading

0 comments on commit 6ee5d7b

Please sign in to comment.