gui: native WebView host as nested module yetano.dev/yetano/core/gui
Extracted from prototypes/yetano-webview (AP1): packages assets, document, bridge and window (macOS Cocoa/WKWebView via cgo, Windows Win32/WebView2 pure Go). No local HTTP listener or port: assets go through the WebView's resource handler, app routes through the native message binding. Own go.mod and gui/vX.Y.Z tags keep cgo and the WebView dependencies out of the main core module. Adds backend-free tests for the window's dispatch/destroy guards and a yet-gui migration guide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
90132e2d76
commit
8c63d42a27
27 files changed
+4849
-3
No files matched your search
@@ -0,0 +1,103 @@
|
||||
package assets
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrInvalidPath marks a virtual asset path that is rejected before any
|
||||
// source is consulted (traversal, backslash, drive letter, UNC, NUL, ...).
|
||||
var ErrInvalidPath = errors.New("invalid asset path")
|
||||
|
||||
// URL prefixes that address the asset root. "/assets/x" and "assets:x" are
|
||||
// the same identity "x".
|
||||
const (
|
||||
PrefixSlash = "/assets/"
|
||||
PrefixScheme = "assets:"
|
||||
)
|
||||
|
||||
// IsAssetRef reports whether ref uses one of the two asset reference forms.
|
||||
func IsAssetRef(ref string) bool {
|
||||
return strings.HasPrefix(ref, PrefixSlash) || strings.HasPrefix(ref, PrefixScheme)
|
||||
}
|
||||
|
||||
// SplitRef removes query and fragment from a reference. They never take part
|
||||
// in the lookup.
|
||||
func SplitRef(ref string) (p, suffix string) {
|
||||
if i := strings.IndexAny(ref, "?#"); i >= 0 {
|
||||
return ref[:i], ref[i:]
|
||||
}
|
||||
return ref, ""
|
||||
}
|
||||
|
||||
// FromRef turns "/assets/a/b.png?x#y" or "assets:a/b.png" into the canonical
|
||||
// virtual path "a/b.png". The path is validated with ValidatePath; nothing is
|
||||
// cleaned silently and nothing is percent-decoded here.
|
||||
func FromRef(ref string) (string, error) {
|
||||
p, _ := SplitRef(ref)
|
||||
switch {
|
||||
case strings.HasPrefix(p, PrefixSlash):
|
||||
p = p[len(PrefixSlash):]
|
||||
case strings.HasPrefix(p, PrefixScheme):
|
||||
p = p[len(PrefixScheme):]
|
||||
default:
|
||||
return "", fmt.Errorf("%w: %q is not an asset reference", ErrInvalidPath, ref)
|
||||
}
|
||||
if err := ValidatePath(p); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// ValidatePath accepts only canonical, relative, forward-slash paths inside
|
||||
// the asset root. It rejects instead of cleaning: "a/../b" is an error, not
|
||||
// "b".
|
||||
func ValidatePath(p string) error {
|
||||
bad := func(why string) error { return fmt.Errorf("%w: %q: %s", ErrInvalidPath, p, why) }
|
||||
switch {
|
||||
case p == "":
|
||||
return bad("empty")
|
||||
case strings.ContainsRune(p, 0):
|
||||
return bad("NUL byte")
|
||||
case strings.Contains(p, `\`):
|
||||
return bad("backslash")
|
||||
case strings.HasPrefix(p, "/"):
|
||||
return bad("absolute")
|
||||
case strings.Contains(p, ":"):
|
||||
return bad("drive letter or scheme")
|
||||
case strings.HasSuffix(p, "/"):
|
||||
return bad("directory")
|
||||
}
|
||||
for _, seg := range strings.Split(p, "/") {
|
||||
switch seg {
|
||||
case "":
|
||||
return bad("empty segment")
|
||||
case ".", "..":
|
||||
return bad("dot segment")
|
||||
}
|
||||
}
|
||||
if path.Clean(p) != p {
|
||||
return bad("not canonical")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Join resolves ref relative to the directory of base, both virtual paths.
|
||||
// Unlike ValidatePath it may use ".." segments, as long as the canonical
|
||||
// result stays inside the root. This is for references found inside trusted
|
||||
// asset content (CSS url(), @import), not for paths coming from the browser.
|
||||
func Join(base, ref string) (string, error) {
|
||||
if strings.ContainsAny(ref, "\\\x00") {
|
||||
return "", fmt.Errorf("%w: %q", ErrInvalidPath, ref)
|
||||
}
|
||||
joined := path.Join(path.Dir(base), ref)
|
||||
if joined == "." || joined == ".." || strings.HasPrefix(joined, "../") || strings.HasPrefix(joined, "/") {
|
||||
return "", fmt.Errorf("%w: %q relative to %q leaves the asset root", ErrInvalidPath, ref, base)
|
||||
}
|
||||
if err := ValidatePath(joined); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return joined, nil
|
||||
}
|
||||
Reference in new issue
Block a user