forked from Botanical/BotanJS
Removed old impl
This commit is contained in:
@@ -14,8 +14,10 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/tgckpg/botanres-go/internal/classmap"
|
||||
"github.com/tgckpg/resolver-go/internal/classmap"
|
||||
"github.com/tgckpg/resolver-go/internal/closure"
|
||||
)
|
||||
|
||||
type OutputMode string
|
||||
@@ -38,6 +40,9 @@ type Result struct {
|
||||
type Resolver struct {
|
||||
Root string
|
||||
Map *classmap.Map
|
||||
|
||||
externMu sync.RWMutex
|
||||
externCache map[string]closure.SourceInput
|
||||
}
|
||||
|
||||
func New(root string, m *classmap.Map) (*Resolver, error) {
|
||||
@@ -45,7 +50,11 @@ func New(root string, m *classmap.Map) (*Resolver, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Resolver{Root: root, Map: m}, nil
|
||||
return &Resolver{
|
||||
Root: root,
|
||||
Map: m,
|
||||
externCache: make(map[string]closure.SourceInput),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Resolver) ResolveRequest(code string, mode OutputMode, excludes []string) (Result, error) {
|
||||
@@ -242,6 +251,55 @@ func (r *Resolver) MergeCSS(files []classmap.Resource) ([]byte, string, error) {
|
||||
return b.Bytes(), hashList(files, "css"), nil
|
||||
}
|
||||
|
||||
func (r *Resolver) GetExterns(files []string) ([]closure.SourceInput, error) {
|
||||
sources := make([]closure.SourceInput, 0, len(files))
|
||||
|
||||
for _, f := range files {
|
||||
src, ok, err := r.getExtern(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
sources = append(sources, src)
|
||||
}
|
||||
|
||||
return sources, nil
|
||||
}
|
||||
|
||||
func (r *Resolver) getExtern(f string) (closure.SourceInput, bool, error) {
|
||||
// Fast path: read cache.
|
||||
r.externMu.RLock()
|
||||
src, ok := r.externCache[f]
|
||||
r.externMu.RUnlock()
|
||||
if ok {
|
||||
return src, true, nil
|
||||
}
|
||||
|
||||
// Slow path: read file.
|
||||
b, err := os.ReadFile(filepath.Join(r.Root, filepath.FromSlash(f)))
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return closure.SourceInput{}, false, nil
|
||||
}
|
||||
return closure.SourceInput{}, false, err
|
||||
}
|
||||
|
||||
src = closure.SourceInput{
|
||||
Name: f,
|
||||
Source: string(b),
|
||||
}
|
||||
|
||||
// Store cache.
|
||||
r.externMu.Lock()
|
||||
r.externCache[f] = src
|
||||
r.externMu.Unlock()
|
||||
|
||||
return src, true, nil
|
||||
}
|
||||
|
||||
func DecodeRequest(code string) ([]string, error) {
|
||||
sep := "/"
|
||||
decoded := code
|
||||
|
||||
Reference in New Issue
Block a user