From 9f431462db9ea0140c3a1ec16eeddfd1a3cb19f8 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 19 Jun 2025 09:40:01 +0100 Subject: [PATCH] Improve render procedures --- guix-data-service/web/render.scm | 49 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/guix-data-service/web/render.scm b/guix-data-service/web/render.scm index 3e2cf79..95c5a48 100644 --- a/guix-data-service/web/render.scm +++ b/guix-data-service/web/render.scm @@ -26,6 +26,7 @@ #:use-module (ice-9 ftw) #:use-module (ice-9 iconv) #:use-module (ice-9 binary-ports) + #:use-module (ice-9 textual-ports) #:use-module (web request) #:use-module (web response) #:use-module (web uri) @@ -147,15 +148,15 @@ '((content-type . (text/html (charset . "utf-8"))) (vary . (accept))))) - (if stream? - (lambda (port) - (set-port-encoding! port "utf-8") - (setvbuf port 'block (expt 2 20)) - (sxml->html sxml port)) - (call-with-encoded-output-string - "utf-8" + (and + sxml + (if stream? (lambda (port) - (sxml->html sxml port)))))) + (sxml->html sxml port)) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (sxml->html sxml port))))))) (define* (render-json json #:key (extra-headers '()) (code 200) @@ -166,27 +167,35 @@ '((content-type . (application/json (charset . "utf-8"))) (vary . (accept))))) - (if stream? - (lambda (port) - (setvbuf port 'block (expt 2 16)) - (scm->json json port #:unicode #t)) - (call-with-encoded-output-string - "utf-8" + + (and + json + (if stream? (lambda (port) - (scm->json json port #:unicode #t)))))) + (scm->json json port #:unicode #t)) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (scm->json json port))))))) (define* (render-text text #:key (extra-headers '()) - (code 200)) + (code 200) + stream?) (list (build-response #:code code #:headers (append extra-headers '((content-type . (text/plain (charset . "utf-8"))) (vary . (accept))))) - (call-with-encoded-output-string - "utf-8" - (lambda (port) - (display text port))))) + (and + text + (if stream? + (lambda (port) + (put-string port text)) + (call-with-encoded-output-string + "utf-8" + (lambda (port) + (display text port))))))) (define (not-found uri) (list (build-response #:code 404)