Serveur : canal d'Excalidraw suivi (latest/next) dans les paramètres
- Lecture des dist-tags npm (latest, next) en une requête ; /api/update
expose tags{latest,next} (latest conservé pour compatibilité).
- Paramètre channel (latest par défaut) dans DATA_DIR/.settings.json,
modifiable par PUT /api/settings {"channel":...} ; titre et canal sont
mis à jour indépendamment, sous verrou.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019EuvvuTv7AUc8ksDHv8VKx
This commit is contained in:
1 parent
573a2527e5
commit
7da52b6755
6 files changed
+153
-46
No files matched your search
+50
-20
@@ -1,12 +1,14 @@
|
||||
// Package update surveille la dernière version publiée de @excalidraw/excalidraw
|
||||
// sur le registre npm. Le frontend la compare à la version embarquée au build et
|
||||
// signale qu'une reconstruction de l'image est possible.
|
||||
// Package update surveille les versions publiées de @excalidraw/excalidraw sur
|
||||
// le registre npm (canaux latest et next). Le frontend compare celle du canal
|
||||
// choisi dans les paramètres à la version embarquée au build et signale qu'une
|
||||
// reconstruction de l'image est possible.
|
||||
package update
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -16,11 +18,26 @@ import (
|
||||
// Package suivi sur le registre npm.
|
||||
const Package = "@excalidraw/excalidraw"
|
||||
|
||||
// Channels sont les dist-tags proposés : latest (versions stables) et next
|
||||
// (préversion publiée à chaque modification de la branche principale).
|
||||
var Channels = []string{"latest", "next"}
|
||||
|
||||
// ValidChannel indique si ch est un canal proposé.
|
||||
func ValidChannel(ch string) bool {
|
||||
for _, c := range Channels {
|
||||
if c == ch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Status est la réponse de GET /api/update.
|
||||
type Status struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Latest string `json:"latest,omitempty"` // vide tant que le registre n'a pas répondu
|
||||
CheckedAt *time.Time `json:"checkedAt,omitempty"` // dernière interrogation réussie
|
||||
Enabled bool `json:"enabled"`
|
||||
Tags map[string]string `json:"tags,omitempty"` // canal -> version ; vide tant que le registre n'a pas répondu
|
||||
Latest string `json:"latest,omitempty"` // = Tags["latest"] (compatibilité)
|
||||
CheckedAt *time.Time `json:"checkedAt,omitempty"` // dernière interrogation réussie
|
||||
}
|
||||
|
||||
// Checker interroge le registre au démarrage puis à intervalle régulier.
|
||||
@@ -48,7 +65,7 @@ func (c *Checker) Run(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (c *Checker) check(ctx context.Context) {
|
||||
v, err := c.fetch(ctx)
|
||||
tags, err := c.fetch(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
slog.Warn("vérification de mise à jour d'Excalidraw", "err", err)
|
||||
@@ -57,36 +74,42 @@ func (c *Checker) check(ctx context.Context) {
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
c.mu.Lock()
|
||||
c.status = Status{Enabled: true, Latest: v, CheckedAt: &now}
|
||||
c.status = Status{Enabled: true, Tags: tags, Latest: tags["latest"], CheckedAt: &now}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *Checker) fetch(ctx context.Context) (string, error) {
|
||||
// fetch lit les dist-tags du paquet (une seule requête, réponse de quelques
|
||||
// centaines d'octets) et ne garde que les canaux proposés.
|
||||
func (c *Checker) fetch(ctx context.Context) (map[string]string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.Registry+"/"+Package+"/latest", nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.Registry+"/-/package/"+Package+"/dist-tags", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("registre npm : %s", res.Status)
|
||||
return nil, fmt.Errorf("registre npm : %s", res.Status)
|
||||
}
|
||||
var body struct {
|
||||
Version string `json:"version"`
|
||||
var all map[string]string
|
||||
if err := json.NewDecoder(io.LimitReader(res.Body, 1<<16)).Decode(&all); err != nil {
|
||||
return nil, fmt.Errorf("registre npm : %w", err)
|
||||
}
|
||||
if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
|
||||
return "", fmt.Errorf("registre npm : %w", err)
|
||||
tags := map[string]string{}
|
||||
for _, ch := range Channels {
|
||||
if v := all[ch]; v != "" {
|
||||
tags[ch] = v
|
||||
}
|
||||
}
|
||||
if body.Version == "" {
|
||||
return "", fmt.Errorf("registre npm : version absente")
|
||||
if tags["latest"] == "" {
|
||||
return nil, fmt.Errorf("registre npm : dist-tag latest absent")
|
||||
}
|
||||
return body.Version, nil
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// Status renvoie le dernier état connu. Un Checker nil signifie « désactivé ».
|
||||
@@ -98,5 +121,12 @@ func (c *Checker) Status() Status {
|
||||
defer c.mu.RUnlock()
|
||||
s := c.status
|
||||
s.Enabled = true
|
||||
if s.Tags != nil { // copie : la map est partagée avec les lectures concurrentes
|
||||
tags := make(map[string]string, len(s.Tags))
|
||||
for k, v := range s.Tags {
|
||||
tags[k] = v
|
||||
}
|
||||
s.Tags = tags
|
||||
}
|
||||
return s
|
||||
}
|
||||
Reference in new issue
Block a user