refactor(plugin,fetch): address PR #98 review feedback
This commit is contained in:
1 parent
89cfdd8935
commit
efc80a46aa
7 files changed
+151
-67
No files matched your search
+12
-5
@@ -184,8 +184,10 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
have = st.Size()
|
||||
}
|
||||
}
|
||||
req, err := NewRequest(ctx, url)
|
||||
reqCtx, cancelReq := context.WithCancel(ctx)
|
||||
req, err := NewRequest(reqCtx, url)
|
||||
if err != nil {
|
||||
cancelReq()
|
||||
return "", err
|
||||
}
|
||||
if have > 0 {
|
||||
@@ -193,6 +195,7 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
}
|
||||
resp, err := clientOr(client).Do(req)
|
||||
if err != nil {
|
||||
cancelReq()
|
||||
return "", fmt.Errorf("download %s: %w", name, err)
|
||||
}
|
||||
|
||||
@@ -205,6 +208,7 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
flags |= os.O_TRUNC
|
||||
case http.StatusRequestedRangeNotSatisfiable:
|
||||
resp.Body.Close()
|
||||
cancelReq()
|
||||
os.Remove(part)
|
||||
if attempt > 0 {
|
||||
return "", fmt.Errorf("download %s: HTTP 416", name)
|
||||
@@ -212,10 +216,12 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
continue
|
||||
default:
|
||||
resp.Body.Close()
|
||||
cancelReq()
|
||||
return "", fmt.Errorf("download %s: HTTP %d", name, resp.StatusCode)
|
||||
}
|
||||
if opts.MaxBytes > 0 && resp.ContentLength > 0 && have+resp.ContentLength > opts.MaxBytes {
|
||||
resp.Body.Close()
|
||||
cancelReq()
|
||||
_ = os.Remove(part)
|
||||
return "", fmt.Errorf("download %s: content length %d exceeds %d byte limit", name, have+resp.ContentLength, opts.MaxBytes)
|
||||
}
|
||||
@@ -223,8 +229,9 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
if total <= 0 && resp.ContentLength > 0 {
|
||||
total = have + resp.ContentLength
|
||||
}
|
||||
err = copyBody(resp.Body, part, flags, have, total, opts.MaxBytes, opts.StallTimeout, name, onProgress)
|
||||
err = copyBody(resp.Body, cancelReq, part, flags, have, total, opts.MaxBytes, opts.StallTimeout, name, onProgress)
|
||||
resp.Body.Close()
|
||||
cancelReq()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -245,7 +252,7 @@ func resumeWithOptions(ctx context.Context, client *http.Client, url, final stri
|
||||
return final, nil
|
||||
}
|
||||
|
||||
func copyBody(body io.ReadCloser, part string, flags int, have, total, maxBytes int64, stallTimeout time.Duration, name string, onProgress Progress) error {
|
||||
func copyBody(body io.Reader, cancelReq context.CancelFunc, part string, flags int, have, total, maxBytes int64, stallTimeout time.Duration, name string, onProgress Progress) error {
|
||||
f, err := os.OpenFile(part, flags, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -254,10 +261,10 @@ func copyBody(body io.ReadCloser, part string, flags int, have, total, maxBytes
|
||||
stalled atomic.Bool
|
||||
timer *time.Timer
|
||||
)
|
||||
if stallTimeout > 0 {
|
||||
if stallTimeout > 0 && cancelReq != nil {
|
||||
timer = time.AfterFunc(stallTimeout, func() {
|
||||
stalled.Store(true)
|
||||
_ = body.Close()
|
||||
cancelReq()
|
||||
})
|
||||
defer timer.Stop()
|
||||
}
|
||||
|
||||
@@ -750,7 +750,7 @@ func (m *Manager) Remove(ctx context.Context, id string, purgeData bool) error {
|
||||
if !existsInState {
|
||||
return fmt.Errorf("plugin %q not found", id)
|
||||
}
|
||||
if activeThemeID != "" && activeThemePluginID == "" && (activeThemeID == id || strings.HasPrefix(activeThemeID, id+".")) {
|
||||
if activeThemeID != "" && activeThemePluginID == "" && m.registry.ThemeOwner(activeThemeID) == "" && (activeThemeID == id || strings.HasPrefix(activeThemeID, id+".")) {
|
||||
ownsActiveTheme = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,12 +274,10 @@ func NewMarketClient(opts MarketClientOptions) *MarketClient {
|
||||
func (c *MarketClient) LoadIndex(ctx context.Context, forceRefresh bool) (idx MarketIndex, source string, stale bool, fetchErr error) {
|
||||
if !forceRefresh {
|
||||
c.mu.RLock()
|
||||
if c.memIndex != nil && time.Since(c.memLoaded) < c.cacheTTL {
|
||||
if c.memIndex != nil && !c.memStale && time.Since(c.memLoaded) < c.cacheTTL {
|
||||
cp := cloneMarketIndex(*c.memIndex)
|
||||
st := c.memStale
|
||||
fe := c.memFetchErr
|
||||
c.mu.RUnlock()
|
||||
return cp, "cache", st, fe
|
||||
return cp, "cache", false, nil
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
}
|
||||
@@ -305,13 +303,14 @@ func (c *MarketClient) LoadIndex(ctx context.Context, forceRefresh bool) (idx Ma
|
||||
fetchErr = errors.New(strings.Join(errs, "; "))
|
||||
|
||||
// Remote failed: fall back gracefully without overwriting valid caches.
|
||||
// Clear memFetched so stale data never vouches for official/verified trust.
|
||||
// Clear memFetched and memLoaded so stale data never vouches for trust
|
||||
// and subsequent non-forced Browse/Install calls retry immediately once online.
|
||||
c.mu.Lock()
|
||||
c.memStale = true
|
||||
c.memFetched = false
|
||||
c.memFetchErr = fetchErr
|
||||
c.memLoaded = time.Time{}
|
||||
if c.memIndex != nil {
|
||||
c.memLoaded = time.Now()
|
||||
cp := cloneMarketIndex(*c.memIndex)
|
||||
c.mu.Unlock()
|
||||
return cp, "cache", true, fetchErr
|
||||
@@ -661,7 +660,11 @@ func (c *MarketClient) storeMem(idx MarketIndex, fetched, stale bool, fetchErr e
|
||||
cp := cloneMarketIndex(idx)
|
||||
c.mu.Lock()
|
||||
c.memIndex = &cp
|
||||
c.memLoaded = time.Now()
|
||||
if stale {
|
||||
c.memLoaded = time.Time{}
|
||||
} else {
|
||||
c.memLoaded = time.Now()
|
||||
}
|
||||
c.memFetched = fetched && !stale
|
||||
c.memStale = stale
|
||||
c.memFetchErr = fetchErr
|
||||
|
||||
+39
-54
@@ -797,37 +797,48 @@ func stripCSSNonCode(css string) string {
|
||||
var b strings.Builder
|
||||
b.Grow(len(css))
|
||||
n := len(css)
|
||||
for i := 0; i < n; {
|
||||
// CSS block comment /* ... */
|
||||
if i+1 < n && css[i] == '/' && css[i+1] == '*' {
|
||||
end := strings.Index(css[i+2:], "*/")
|
||||
b.WriteByte(' ')
|
||||
if end < 0 {
|
||||
break
|
||||
}
|
||||
i += 2 + end + 2
|
||||
continue
|
||||
i := 0
|
||||
|
||||
skipComment := func() bool {
|
||||
if i+1 >= n || css[i] != '/' || css[i+1] != '*' {
|
||||
return false
|
||||
}
|
||||
// Quoted string literal
|
||||
if css[i] == '"' || css[i] == '\'' {
|
||||
q := css[i]
|
||||
i++
|
||||
b.WriteByte(' ')
|
||||
for i < n {
|
||||
if css[i] == '\\' && i+1 < n {
|
||||
if css[i+1] == '\r' && i+2 < n && css[i+2] == '\n' {
|
||||
i += 3
|
||||
continue
|
||||
}
|
||||
i += 2
|
||||
end := strings.Index(css[i+2:], "*/")
|
||||
if end < 0 {
|
||||
i = n
|
||||
return true
|
||||
}
|
||||
i += 2 + end + 2
|
||||
return true
|
||||
}
|
||||
|
||||
skipQuoted := func() bool {
|
||||
if i >= n || (css[i] != '"' && css[i] != '\'') {
|
||||
return false
|
||||
}
|
||||
q := css[i]
|
||||
i++
|
||||
for i < n {
|
||||
if css[i] == '\\' && i+1 < n {
|
||||
if css[i+1] == '\r' && i+2 < n && css[i+2] == '\n' {
|
||||
i += 3
|
||||
continue
|
||||
}
|
||||
if css[i] == q || css[i] == '\n' || css[i] == '\r' {
|
||||
i++
|
||||
break
|
||||
}
|
||||
i++
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
if css[i] == q || css[i] == '\n' || css[i] == '\r' {
|
||||
i++
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
for i < n {
|
||||
if skipComment() || skipQuoted() {
|
||||
b.WriteByte(' ')
|
||||
continue
|
||||
}
|
||||
// url(...) function (case-insensitive, not part of a longer identifier)
|
||||
@@ -835,33 +846,7 @@ func stripCSSNonCode(css string) string {
|
||||
b.WriteString("url()")
|
||||
i += 4
|
||||
for i < n {
|
||||
if i+1 < n && css[i] == '/' && css[i+1] == '*' {
|
||||
end := strings.Index(css[i+2:], "*/")
|
||||
if end < 0 {
|
||||
i = n
|
||||
break
|
||||
}
|
||||
i += 2 + end + 2
|
||||
continue
|
||||
}
|
||||
if css[i] == '"' || css[i] == '\'' {
|
||||
q := css[i]
|
||||
i++
|
||||
for i < n {
|
||||
if css[i] == '\\' && i+1 < n {
|
||||
if css[i+1] == '\r' && i+2 < n && css[i+2] == '\n' {
|
||||
i += 3
|
||||
continue
|
||||
}
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
if css[i] == q || css[i] == '\n' || css[i] == '\r' {
|
||||
i++
|
||||
break
|
||||
}
|
||||
i++
|
||||
}
|
||||
if skipComment() || skipQuoted() {
|
||||
continue
|
||||
}
|
||||
if css[i] == '\\' && i+1 < n {
|
||||
|
||||
@@ -825,6 +825,26 @@ func (r *Registry) PluginOwnsTheme(pluginID, themeID string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ThemeOwner returns the plugin ID that registers or declares themeID, or "".
|
||||
func (r *Registry) ThemeOwner(themeID string) string {
|
||||
if themeID == "" {
|
||||
return ""
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if th, ok := r.themes[themeID]; ok && th.PluginID != "" {
|
||||
return th.PluginID
|
||||
}
|
||||
for pid, e := range r.entries {
|
||||
for _, th := range e.def.Manifest.Contributes.Themes {
|
||||
if th.ID == themeID {
|
||||
return pid
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Registry) RegisterUIScript(sc *Scope, us UIScriptContribution) {
|
||||
if us.ID == "" {
|
||||
return
|
||||
|
||||
@@ -356,4 +356,68 @@ func TestDisableAndRemoveActiveThemeSemantics(t *testing.T) {
|
||||
if st.ActiveThemeID != "" || st.ActiveThemePluginID != "" || st.ActiveSchemeID != "" {
|
||||
t.Fatalf("removing orphaned theme plugin left stale state: theme=%q plugin=%q scheme=%q", st.ActiveThemeID, st.ActiveThemePluginID, st.ActiveSchemeID)
|
||||
}
|
||||
|
||||
// 5. Removing an orphaned legacy plugin whose ID happens to be a prefix of
|
||||
// another live plugin's active theme ID MUST NOT clear that live theme.
|
||||
prefixMan, _ := json.Marshal(plugin.Manifest{
|
||||
Schema: 1,
|
||||
ID: "test.theme.pack.dark",
|
||||
Version: "1.0.0",
|
||||
Name: plugin.LocalizedText{EnUS: "Live Dark Theme"},
|
||||
Publisher: "tester",
|
||||
License: "MIT",
|
||||
Contributes: plugin.Contributes{
|
||||
Themes: []plugin.ThemeContribution{{
|
||||
ID: "test.theme.pack.dark",
|
||||
Name: plugin.LocalizedText{EnUS: "Live Dark"},
|
||||
Stylesheet: "theme.css",
|
||||
}},
|
||||
},
|
||||
})
|
||||
liveDarkZip := makePluginZip(t, map[string][]byte{
|
||||
"plugin.json": prefixMan,
|
||||
"theme.css": []byte(".rg-p-sidebar { background: #181818; }"),
|
||||
}, nil, false)
|
||||
orphanMgr.Close()
|
||||
|
||||
mgr5 := openMgr(false)
|
||||
if _, err := mgr5.InstallLocalArchive(ctx, liveDarkZip, plugin.PackageInstallOptions{}); err != nil {
|
||||
t.Fatalf("install live dark theme: %v", err)
|
||||
}
|
||||
if err := mgr5.Enable(ctx, "test.theme.pack.dark", false); err != nil {
|
||||
t.Fatalf("enable live dark theme: %v", err)
|
||||
}
|
||||
if err := mgr5.SetActiveTheme("test.theme.pack.dark", "carbon"); err != nil {
|
||||
t.Fatalf("activate live dark theme: %v", err)
|
||||
}
|
||||
mgr5.Close()
|
||||
|
||||
// Inject a legacy state.json (no active_theme_plugin_id) with an orphaned prefix plugin "test.theme.pack".
|
||||
store := plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root)
|
||||
st, err = store.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("load state for prefix collision test: %v", err)
|
||||
}
|
||||
st.ActiveThemePluginID = ""
|
||||
st.Plugins["test.theme.pack"] = &plugin.PluginRecord{
|
||||
ID: "test.theme.pack",
|
||||
Version: "1.0.0",
|
||||
Source: plugin.SourceLocal,
|
||||
}
|
||||
if err := store.Save(st); err != nil {
|
||||
t.Fatalf("save legacy state: %v", err)
|
||||
}
|
||||
|
||||
mgr6 := openMgr(true)
|
||||
defer mgr6.Close()
|
||||
if err := mgr6.Remove(ctx, "test.theme.pack", true); err != nil {
|
||||
t.Fatalf("remove orphaned prefix plugin: %v", err)
|
||||
}
|
||||
st, err = store.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("reload state after prefix orphan remove: %v", err)
|
||||
}
|
||||
if st.ActiveThemeID != "test.theme.pack.dark" || st.ActiveSchemeID != "carbon" {
|
||||
t.Fatalf("removing unrelated orphaned prefix plugin wiped live theme: theme=%q scheme=%q", st.ActiveThemeID, st.ActiveSchemeID)
|
||||
}
|
||||
}
|
||||
@@ -174,6 +174,11 @@ func TestStaleIndexDoesNotVouch(t *testing.T) {
|
||||
if afterFail := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); !afterFail.Stale || afterFail.Items[0].Entry.Trust != plugin.TrustCommunity {
|
||||
t.Fatalf("non-forced Browse after failed refresh rebounded: stale=%v trust=%q", afterFail.Stale, afterFail.Items[0].Entry.Trust)
|
||||
}
|
||||
// Once remote recovers, the next non-forced Browse must retry and restore official trust immediately.
|
||||
failRemote = false
|
||||
if recovered := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); recovered.Stale || recovered.Source != "remote" || recovered.Items[0].Entry.Trust != plugin.TrustOfficial {
|
||||
t.Fatalf("non-forced Browse after recovery stayed stale: source=%q stale=%v trust=%q", recovered.Source, recovered.Stale, recovered.Items[0].Entry.Trust)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficialPluginsStillNeedApproval(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user