-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
93 lines (79 loc) · 2.15 KB
/
Copy pathcache.go
File metadata and controls
93 lines (79 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package ssr
import (
"crypto/md5"
"encoding/hex"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/tinywasm/fmt"
)
// ssrCacheEntry holds a cached extraction result keyed by module hash set.
type ssrCacheEntry struct {
hashSet string // Combined hash of all module Go files
results map[string]ssrCollectorOutput
}
// ssrCache manages content-hash based caching for SSR asset extraction.
type ssrCache struct {
entries map[string]*ssrCacheEntry
}
// newSSRCache creates a new cache instance.
func newSSRCache() *ssrCache {
return &ssrCache{
entries: make(map[string]*ssrCacheEntry),
}
}
// computeModuleHashSet computes a combined hash of all Go files in the module set.
func computeModuleHashSet(modules []module) (string, error) {
var filePaths []string
for _, m := range modules {
if m.dir == "" {
continue
}
// Walk the module directory and collect all .go files (excluding *_test.go)
err := filepath.Walk(m.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go") {
filePaths = append(filePaths, path)
}
return nil
})
if err != nil {
return "", fmt.Err("failed to walk module dir", m.dir, err)
}
}
// Sort for deterministic order
sort.Strings(filePaths)
// Compute hash of all file contents
h := md5.New()
for _, filePath := range filePaths {
f, err := os.Open(filePath)
if err != nil {
return "", fmt.Err("failed to open file", filePath, err)
}
if _, err := io.Copy(h, f); err != nil {
f.Close()
return "", fmt.Err("failed to hash file", filePath, err)
}
f.Close()
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// get retrieves cached results if the module hash set matches.
func (c *ssrCache) get(hashSet string) (map[string]ssrCollectorOutput, bool) {
entry, ok := c.entries[hashSet]
if !ok {
return nil, false
}
return entry.results, true
}
// set caches extraction results for a module hash set.
func (c *ssrCache) set(hashSet string, results map[string]ssrCollectorOutput) {
c.entries[hashSet] = &ssrCacheEntry{
hashSet: hashSet,
results: results,
}
}