shithub: mycel

Download patch

ref: 9f6a5105c5f00becacda853aaba00603020f941d
parent: e2ac43435940ad220871216999ef036dc64c915d
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Oct 23 08:45:54 EDT 2021

use fs instead of json serialized stuff

- Fix repeating page error
- Gracefully handle broken pipe errors
- Improved css logging

--- a/README.md
+++ b/README.md
@@ -21,8 +21,9 @@
 
     # Setup TLS
     hget https://curl.haxx.se/ca/cacert.pem > /sys/lib/tls/ca.pem
-    # Create mountpoint (only needed on 9legacy)
+    # Create mountpoints (needed on 9legacy)
     mkdir /mnt/opossum
+    mkdir /mnt/goja
 
 ### Binary
 
@@ -80,7 +81,7 @@
 go install ./cmd/gojafs
 ```
 
-On 9legacy also the folder `/mnt/opossum` needs to exist.
+On 9legacy also the folders `/mnt/opossum` and `/mnt/goja` need to exist.
 
 Then it can be tested with:
 
--- a/browser/browser.go
+++ b/browser/browser.go
@@ -703,10 +703,7 @@
 		res, consumed, err := js.TriggerClick(q)
 		if err != nil {
 			log.Errorf("trigger click %v: %v", q, err)
-			return consumed
-		}
-
-		if consumed {
+		} else if consumed {
 			offset := scroller.Offset
 			browser.Website.layout(browser, res, ClickRelayout)
 			scroller.Offset = offset
--- a/browser/fs/experimental.go
+++ b/browser/fs/experimental.go
@@ -7,6 +7,7 @@
 	"github.com/knusbaum/go9p/proto"
 	"github.com/psilva261/opossum/logger"
 	"github.com/psilva261/opossum/nodes"
+	"github.com/psilva261/opossum/style"
 	"golang.org/x/net/html"
 )
 
@@ -63,6 +64,7 @@
 		cs["tag"] = n.tag()
 		cs["attrs"] = Attrs{attrs: &n.nt.DomSubtree.Attr}
 		cs["html"] = n.html()
+		cs["style"] = Style{cs: &n.nt.Map}
 	}
 
 	return
@@ -141,6 +143,52 @@
 	}
 	for _, attr := range *as.attrs {
 		cs[attr.Key] = ff(attr.Key)
+	}
+	return 
+}
+
+type Style struct {
+	cs *style.Map
+}
+
+func (st Style) Stat() (s proto.Stat) {
+	s = *oFS.NewStat("style", un, gn, 0500)
+	s.Mode |= proto.DMDIR
+	// qtype bits should be consistent with Stat mode.
+	s.Qid.Qtype = uint8(s.Mode >> 24)
+	return
+}
+
+func (st Style) WriteStat(s *proto.Stat) error {
+	return nil
+}
+
+func (st Style) SetParent(p fs.Dir) {
+}
+
+func (st Style) Parent() fs.Dir {
+	return nil
+}
+
+func (st Style) Children() (cs map[string]fs.FSNode) {
+	log.Infof("Style#Children()")
+	cs = make(map[string]fs.FSNode)
+	ff := func(k string) fs.FSNode {
+		return fs.NewDynamicFile(
+			oFS.NewStat(k, un, gn, 0666),
+			func() []byte {
+				var v string
+				for p, d := range st.cs.Declarations {
+					if p == k {
+						v = d.Value
+					}
+				}
+				return []byte(v)
+			},
+		)
+	}
+	for p := range st.cs.Declarations {
+		cs[p] = ff(p)
 	}
 	return 
 }
--- a/browser/website.go
+++ b/browser/website.go
@@ -54,13 +54,12 @@
 
 			nm, err := style.FetchNodeMap(doc, css, 1280)
 			if err == nil {
-				log.Printf("[%v/%v] Fetch CSS Rules successful!", i+1, len(csss))
 				if debugPrintHtml {
 					log.Printf("%v", nm)
 				}
 				style.MergeNodeMaps(nodeMap, nm)
 			} else {
-				log.Errorf("Fetch CSS Rules failed: %v", err)
+				log.Errorf("%v/css/%v.css: Fetch CSS Rules failed: %v", opossum.PathPrefix, i, err)
 			}
 		}
 
--- a/cmd/gojafs/domino/domintf.js
+++ b/cmd/gojafs/domino/domintf.js
@@ -103,16 +103,17 @@
 	if (!el) {
 		return undefined;
 	}
+	if (el.tagName === 'BODY') {
+		return '/0';
+	}
 	p = el.parentElement;
 
 	if (p) {
 		for (i = 0; i < p.children.length; i++) {
 			if (p.children[i] === el) {
-				return ___fq('', p) + ' > :nth-child(' + (i+1) + ')';
+				return ___fq('', p) + '/' + i;
 			}
 		}
-	} else {
-		return el.tagName;
 	}
 };
 
--- a/cmd/gojafs/main.go
+++ b/cmd/gojafs/main.go
@@ -3,13 +3,11 @@
 
 import (
 	"bufio"
-	"encoding/json"
 	"fmt"
 	"github.com/knusbaum/go9p/fs"
 	"github.com/psilva261/opossum"
 	"github.com/psilva261/opossum/cmd/gojafs/domino"
 	"github.com/psilva261/opossum/logger"
-	"github.com/psilva261/opossum/nodes"
 	"io"
 	"net"
 	"net/http"
@@ -140,31 +138,20 @@
 }
 
 func query(sel, prop string) (val string, err error) {
-	log.Infof("query: sel=%+v, prop=%+v\n", sel, prop)
-	rwc, err := openQuery()
+	log.Infof("gojajs: query: sel=%+v, prop=%+v\n", sel, prop)
+	fn := sel+"/style/"+prop
+	rwc, err := open(fn)
 	if err != nil {
-		return "", fmt.Errorf("open query: %w", err)
+		return "", fmt.Errorf("open %v: %v", fn, err)
 	}
 	defer rwc.Close()
-	r := json.NewDecoder(rwc)
-	_, err = io.WriteString(rwc, sel+"\n")
-	if err != nil {
-		return "", fmt.Errorf("write: %w", err)
-	}
-	log.Printf("devjs: query: sent")
-	var res []*nodes.Node
-	if err := r.Decode(&res); err != nil {
-		return "", fmt.Errorf("decodeeee: >%w<", err)
-	}
-	if n := len(res); n != 1 {
-		return "", fmt.Errorf("query returned %v results", n)
-	}
-	log.Printf("devjs: query: recvd: %v", res[0])
-	return res[0].Css(prop), nil
+	bs, err := io.ReadAll(rwc)
+	val = string(bs)
+	return
 }
 
 func xhr(req *http.Request) (resp *http.Response, err error) {
-	rwc, err := openXhr()
+	rwc, err := open("xhr")
 	if err != nil {
 		return nil, fmt.Errorf("open xhr: %w", err)
 	}
--- a/cmd/gojafs/main_plan9.go
+++ b/cmd/gojafs/main_plan9.go
@@ -36,12 +36,8 @@
 	return
 }
 
-func openQuery() (rwc io.ReadWriteCloser, err error) {
-	return os.OpenFile(mtpt+"/query", os.O_RDWR, 0600)
-}
-
-func openXhr() (rwc io.ReadWriteCloser, err error) {
-	return os.OpenFile(mtpt+"/xhr", os.O_RDWR, 0600)
+func open(fn string) (rwc io.ReadWriteCloser, err error) {
+	return os.OpenFile(mtpt+"/"+fn, os.O_RDWR, 0600)
 }
 
 func post(srv go9p.Srv) (err error) {
--- a/cmd/gojafs/main_unix.go
+++ b/cmd/gojafs/main_unix.go
@@ -16,11 +16,7 @@
 
 func Init() (err error) {
 	log.Infof("Init...")
-	if service == "" {
-		return
-	}
-	log.Infof("dial service...")
-	conn, err := client.DialService(service)
+	conn, err := client.DialService("opossum")
 	if err != nil {
 		log.Fatalf("dial: %v", err)
 	}
@@ -78,14 +74,13 @@
 	return
 }
 
-func openQuery() (rwc io.ReadWriteCloser, err error) {
-	return fsys.Open("query", plan9.ORDWR)
+func open(fn string) (rwc io.ReadWriteCloser, err error) {
+	return fsys.Open(fn, plan9.ORDWR)
 }
 
-func openXhr() (rwc io.ReadWriteCloser, err error) {
-	return fsys.Open("xhr", plan9.ORDWR)
-}
-
 func post(srv go9p.Srv) (err error) {
-	return go9p.PostSrv("goja", srv)
+	if service == "" {
+		return fmt.Errorf("no service specified")
+	}
+	return go9p.PostSrv(service, srv)
 }
--- a/js/js.go
+++ b/js/js.go
@@ -28,7 +28,8 @@
 	fetcher opossum.Fetcher
 	nt      *nodes.Node
 
-	cancel context.CancelFunc
+	service string
+	cancel  context.CancelFunc
 )
 
 func NewJS(html string, fetcher opossum.Fetcher, nn *nodes.Node) {
@@ -57,8 +58,9 @@
 
 // Start with pre-defined scripts
 func Start(scripts ...string) (resHtm string, changed bool, err error) {
+	service = fmt.Sprintf("goja.%d", os.Getpid())
 	args := make([]string, 0, len(scripts)+2)
-	args = append(args, "-s", "opossum")
+	args = append(args, "-s", service)
 	log.Infof("Start gojafs")
 
 	var ctx context.Context
--- a/js/js_unix.go
+++ b/js/js_unix.go
@@ -15,7 +15,7 @@
 
 func dial() (err error) {
 	log.Infof("Init...")
-	conn, err := client.DialService("goja")
+	conn, err := client.DialService(service)
 	if err != nil {
 		log.Fatalf("dial: %v", err)
 	}
--- a/nodes/experimental.go
+++ b/nodes/experimental.go
@@ -3,6 +3,7 @@
 import (
 	"fmt"
 	"github.com/andybalholm/cascadia"
+	"github.com/psilva261/opossum"
 	"golang.org/x/net/html"
 )
 
@@ -10,7 +11,7 @@
 func (n *Node) Path() (p string, ok bool) {
 	p, ok = n.path()
 	if ok {
-		p = PathPrefix+p
+		p = opossum.PathPrefix+p
 	}
 	return
 }
--- a/nodes/experimental_plan9.go
+++ /dev/null
@@ -1,3 +1,0 @@
-package nodes
-
-const PathPrefix = "/mnt/opossum"
--- a/nodes/experimental_unix.go
+++ /dev/null
@@ -1,5 +1,0 @@
-//go:build !plan9
-
-package nodes
-
-const PathPrefix = "opossum"
--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -71,7 +71,7 @@
 	return
 }
 
-// filterText removes line break runes (TODO: add this later but handle properly) and maps runes to canonical widths
+// filterText removes line break runes (TODO: add this later but handle properly)
 func filterText(t string) string {
 	t = strings.ReplaceAll(t, "­", "")
 	t = strings.ReplaceAll(t, "\t", "    ")
--- a/opossum_plan9.go
+++ b/opossum_plan9.go
@@ -4,6 +4,8 @@
 	"os/user"
 )
 
+const PathPrefix = "/mnt/opossum"
+
 func Group(u *user.User) (string, error) {
 	return u.Gid, nil
 }
--- a/opossum_unix.go
+++ b/opossum_unix.go
@@ -1,5 +1,4 @@
 //go:build !plan9
-// +build !plan9
 
 package opossum
 
@@ -7,6 +6,8 @@
 	"fmt"
 	"os/user"
 )
+
+const PathPrefix = "opossum"
 
 func Group(u *user.User) (string, error) {
 	g, err := user.LookupGroupId(u.Gid)
--- a/style/fonts_plan9.go
+++ b/style/fonts_plan9.go
@@ -1,5 +1,4 @@
 //go:build plan9
-// +build plan9
 
 package style
 
--- a/style/fonts_unix.go
+++ b/style/fonts_unix.go
@@ -1,5 +1,4 @@
-//go:build darwin || freebsd || netbsd || openbsd || linux
-// +build darwin freebsd netbsd openbsd linux
+//go:build !plan9
 
 package style