2022-11-29 05:01:15PM

This commit is contained in:
Alexander Wang 2022-11-29 17:01:15 -08:00
parent 9b982a684e
commit 7cbcd88f9c
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 9 additions and 19 deletions

View file

@ -214,22 +214,14 @@ func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plu
} }
svg, err = imgbundler.InlineLocal(ctx, ms, svg) svg, err = imgbundler.InlineLocal(ctx, ms, svg)
if err != nil { if err != nil {
if !isWatching { ms.Log.Error.Printf("missing/broken local image(s), writing partial output: %v", err)
ms.Log.Error.Printf("missing/broken remote image(s), writing partial output: %v", err)
} else {
ms.Log.Debug.Printf("ignoring missing/broken local image(s) in watch mode: %v", err)
}
} }
out := svg out := svg
if filepath.Ext(outputPath) == ".png" { if filepath.Ext(outputPath) == ".png" {
svg, err = imgbundler.InlineRemote(ctx, ms, svg) svg, err = imgbundler.InlineRemote(ctx, ms, svg)
if err != nil { if err != nil {
if !isWatching {
ms.Log.Error.Printf("missing/broken remote image(s), writing partial output: %v", err) ms.Log.Error.Printf("missing/broken remote image(s), writing partial output: %v", err)
} else {
ms.Log.Debug.Printf("ignoring missing/broken remote image(s) in watch mode: %v", err)
}
} }
out, err = png.ConvertSVG(ms, page, svg) out, err = png.ConvertSVG(ms, page, svg)
@ -246,7 +238,7 @@ func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plu
// Missing/broken images are fine during watch mode, as the user is likely building up a diagram. // Missing/broken images are fine during watch mode, as the user is likely building up a diagram.
// Otherwise, the assumption is that this diagram is building for production, and broken images are not okay. // Otherwise, the assumption is that this diagram is building for production, and broken images are not okay.
if !isWatching && ms.Log.Nerrors() > 0 { if !isWatching && ms.Log.Nerrors() > 0 {
os.Exit(1) xmain.ExitErrorf(1, "errors logged while rendering, partial output written to %v", outputPath)
} }
return svg, nil return svg, nil

View file

@ -97,7 +97,6 @@ func inline(ctx context.Context, ms *xmain.State, svg []byte, isRemote bool) (_
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
ms.Log.Debug.Printf("there")
return nil, fmt.Errorf("failed to wait for imgbundler workers: %w", ctx.Err()) return nil, fmt.Errorf("failed to wait for imgbundler workers: %w", ctx.Err())
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
ms.Log.Info.Printf("fetching images...") ms.Log.Info.Printf("fetching images...")
@ -114,7 +113,7 @@ func inline(ctx context.Context, ms *xmain.State, svg []byte, isRemote bool) (_
} }
} }
var transport = http.DefaultTransport var imgClient = &http.Client{}
func fetch(ctx context.Context, href string) (string, error) { func fetch(ctx context.Context, href string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute) ctx, cancel := context.WithTimeout(ctx, time.Minute)
@ -125,8 +124,7 @@ func fetch(ctx context.Context, href string) (string, error) {
return "", err return "", err
} }
client := &http.Client{Transport: transport} imgResp, err := imgClient.Do(req)
imgResp, err := client.Do(req)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -90,7 +90,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
} }
ms.Log = cmdlog.Log(ms.Env, os.Stderr) ms.Log = cmdlog.Log(ms.Env, os.Stderr)
transport = roundTripFunc(func(req *http.Request) *http.Response { imgClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
respRecorder := httptest.NewRecorder() respRecorder := httptest.NewRecorder()
switch req.URL.String() { switch req.URL.String() {
case svgURL: case svgURL:
@ -119,7 +119,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
} }
// Test almost too large response // Test almost too large response
transport = roundTripFunc(func(req *http.Request) *http.Response { imgClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
respRecorder := httptest.NewRecorder() respRecorder := httptest.NewRecorder()
bytes := make([]byte, maxImageSize) bytes := make([]byte, maxImageSize)
rand.Read(bytes) rand.Read(bytes)
@ -133,7 +133,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
} }
// Test too large response // Test too large response
transport = roundTripFunc(func(req *http.Request) *http.Response { imgClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
respRecorder := httptest.NewRecorder() respRecorder := httptest.NewRecorder()
bytes := make([]byte, maxImageSize+1) bytes := make([]byte, maxImageSize+1)
rand.Read(bytes) rand.Read(bytes)
@ -147,7 +147,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
} }
// Test error response // Test error response
transport = roundTripFunc(func(req *http.Request) *http.Response { imgClient.Transport = roundTripFunc(func(req *http.Request) *http.Response {
respRecorder := httptest.NewRecorder() respRecorder := httptest.NewRecorder()
respRecorder.WriteHeader(500) respRecorder.WriteHeader(500)
return respRecorder.Result() return respRecorder.Result()