Просмотр исходного кода

fix(httpapi): writeJSON sin salto de línea final (paridad D5/Hono)

agent 4 месяцев назад
Родитель
Сommit
1dc82f379c
1 измененных файлов с 9 добавлено и 5 удалено
  1. 9 5
      server-go/internal/httpapi/server.go

+ 9 - 5
server-go/internal/httpapi/server.go

@@ -10,6 +10,7 @@
 package httpapi
 package httpapi
 
 
 import (
 import (
+	"bytes"
 	"encoding/json"
 	"encoding/json"
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
@@ -129,14 +130,17 @@ func noStoreMux(next http.Handler) http.Handler {
 	})
 	})
 }
 }
 
 
-// writeJSON serializa como JSON.stringify: compacto y SIN escapar HTML
-// (SetEscapeHTML(false)).
+// writeJSON serializa como JSON.stringify: compacto, SIN escapar HTML
+// (SetEscapeHTML(false)) y SIN el '\n' final que añade Encoder.Encode
+// (consistencia con D5 WriteError / paridad Hono: marshal + write).
 func writeJSON(w http.ResponseWriter, status int, v any) {
 func writeJSON(w http.ResponseWriter, status int, v any) {
-	w.Header().Set("Content-Type", "application/json")
-	w.WriteHeader(status)
-	enc := json.NewEncoder(w)
+	var buf bytes.Buffer
+	enc := json.NewEncoder(&buf)
 	enc.SetEscapeHTML(false)
 	enc.SetEscapeHTML(false)
 	_ = enc.Encode(v)
 	_ = enc.Encode(v)
+	w.Header().Set("Content-Type", "application/json")
+	w.WriteHeader(status)
+	_, _ = w.Write(bytes.TrimSuffix(buf.Bytes(), []byte("\n")))
 }
 }
 
 
 // writeError escribe el envelope {error, message?}.
 // writeError escribe el envelope {error, message?}.