Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
This commit is contained in:
1 parent
9dd425f765
commit
d5d21b7841
302 files changed
+20037
-3492
No files matched your search
@@ -1,9 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -13,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/kunstzeal"
|
||||
)
|
||||
|
||||
var diagramSourceCache = map[string]diagramCacheEntry{}
|
||||
@@ -34,60 +33,15 @@ func (s *Server) diagramCachePath(kind, hash string) string {
|
||||
return filepath.Join(root, kind, hash+".svg")
|
||||
}
|
||||
|
||||
func readDiagramCache(path string) ([]byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
b, err := io.ReadAll(io.LimitReader(f, (4<<20)+1))
|
||||
if len(b) > 4<<20 {
|
||||
return nil, errors.New("diagram cache exceeds limit")
|
||||
}
|
||||
return b, err
|
||||
func readDiagramCache(path string) ([]byte, error) { return kunstzeal.ReadDiagramCache(path) }
|
||||
|
||||
func diagramHash(kind, source, format string) string {
|
||||
return kunstzeal.DiagramHash(kind, source, format)
|
||||
}
|
||||
|
||||
func diagramHash(
|
||||
diagramType string,
|
||||
source string,
|
||||
format string,
|
||||
) string {
|
||||
func allowedDiagramType(kind string) bool { return kunstzeal.AllowedDiagramType(kind) }
|
||||
|
||||
sum :=
|
||||
sha256.Sum256(
|
||||
[]byte(
|
||||
diagramType + "\x00" + format + "\x00" + source,
|
||||
),
|
||||
)
|
||||
|
||||
return hex.EncodeToString(
|
||||
sum[:],
|
||||
)
|
||||
}
|
||||
|
||||
func allowedDiagramType(
|
||||
t string,
|
||||
) bool {
|
||||
|
||||
switch t {
|
||||
case "mermaid", "plantuml", "graphviz", "dot", "d2":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeDiagramType(
|
||||
t string,
|
||||
) string {
|
||||
|
||||
switch t {
|
||||
case "dot":
|
||||
return "graphviz"
|
||||
default:
|
||||
return t
|
||||
}
|
||||
}
|
||||
func normalizeDiagramType(kind string) string { return kunstzeal.NormalizeDiagramType(kind) }
|
||||
|
||||
func krokiURL() string {
|
||||
|
||||
@@ -137,7 +91,7 @@ func (s *Server) renderDiagramsInHTML(
|
||||
htmlUnescapeString(
|
||||
m[2],
|
||||
)
|
||||
if len(source) > 32<<10 || path == "" {
|
||||
if len(source) > kunstzeal.DiagramInlineLimit || path == "" {
|
||||
return block
|
||||
}
|
||||
|
||||
@@ -311,11 +265,11 @@ func (s *Server) handleCompileDiagramPost(
|
||||
io.ReadAll(
|
||||
io.LimitReader(
|
||||
r.Body,
|
||||
(1<<20)+1,
|
||||
kunstzeal.DiagramSourceLimit+1,
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil || len(sourceBytes) > 1<<20 {
|
||||
if err != nil || len(sourceBytes) > kunstzeal.DiagramSourceLimit {
|
||||
http.Error(w, "diagram source exceeds limit", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
@@ -352,91 +306,26 @@ func (s *Server) handleCompileDiagramPost(
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramSVG(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
diagramType string,
|
||||
source string,
|
||||
cachePath string,
|
||||
) {
|
||||
|
||||
req, err :=
|
||||
http.NewRequestWithContext(
|
||||
r.Context(),
|
||||
http.MethodPost,
|
||||
krokiURL()+"/"+diagramType+"/svg",
|
||||
bytes.NewBufferString(source),
|
||||
)
|
||||
|
||||
// The configured service may be private (Compose), but it may not redirect
|
||||
// source content to another destination. Authorization is checked before this.
|
||||
func diagramHTTPClient() *http.Client {
|
||||
return &http.Client{Timeout: kunstzeal.DiagramTimeout, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramSVG(w http.ResponseWriter, r *http.Request, diagramType, source, cachePath string) {
|
||||
provider := kunstzeal.Kroki{Endpoint: krokiURL(), Client: diagramHTTPClient()}
|
||||
b, err := provider.Render(r.Context(), diagramType, source)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set(
|
||||
"Content-Type",
|
||||
"text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
client :=
|
||||
http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
status := http.StatusBadGateway
|
||||
if errors.Is(err, kunstzeal.ErrProviderConfig) {
|
||||
status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
resp, err :=
|
||||
client.Do(
|
||||
req,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 502)
|
||||
http.Error(w, err.Error(), status)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 ||
|
||||
resp.StatusCode >= 300 {
|
||||
|
||||
http.Error(w, "diagram renderer failed", 502)
|
||||
return
|
||||
}
|
||||
|
||||
b, err :=
|
||||
io.ReadAll(
|
||||
io.LimitReader(resp.Body, (4<<20)+1),
|
||||
)
|
||||
|
||||
if err != nil || len(b) > 4<<20 {
|
||||
http.Error(w, "diagram output unavailable or exceeds limit", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
_ =
|
||||
os.MkdirAll(
|
||||
filepath.Dir(cachePath),
|
||||
0755,
|
||||
)
|
||||
|
||||
_ =
|
||||
os.WriteFile(
|
||||
cachePath,
|
||||
b,
|
||||
0644,
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"image/svg+xml; charset=utf-8",
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Security-Policy",
|
||||
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
)
|
||||
|
||||
_, _ =
|
||||
w.Write(
|
||||
b,
|
||||
)
|
||||
// Cache failure must not discard a successfully generated projection.
|
||||
_ = kunstzeal.WriteDiagramCache(cachePath, b)
|
||||
w.Header().Set("Content-Type", "image/svg+xml; charset=utf-8")
|
||||
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:")
|
||||
_, _ = w.Write(b)
|
||||
}
|
||||
Reference in new issue
Block a user