Merge pull request #124 from syumai/fix-fetch-stream-empty-response

fixed panic on fetch returning empty response
This commit is contained in:
syumai 2024-08-11 22:15:33 +09:00 committed by GitHub
commit ad33cfb9ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 1 additions and 12 deletions

View File

@ -30,5 +30,5 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp
return nil, err
}
return jshttp.ToStreamResponse(jsRes)
return jshttp.ToResponse(jsRes)
}

View File

@ -30,17 +30,6 @@ func ToResponse(res js.Value) (*http.Response, error) {
return toResponse(res, body)
}
// ToStreamResponse pipes JavaScript sides Response to TransformStream and converts to *http.Response.
// - see: https://developers.cloudflare.com/workers/runtime-apis/streams/
func ToStreamResponse(res js.Value) (*http.Response, error) {
ts := js.Global().Get("IdentityTransformStream").New()
readable := ts.Get("readable")
writable := ts.Get("writable")
res.Get("body").Call("pipeTo", writable)
body := jsutil.ConvertReadableStreamToReadCloser(readable)
return toResponse(res, body)
}
// ToJSResponse converts *http.Response to JavaScript sides Response class object.
func ToJSResponse(res *http.Response) js.Value {
return newJSResponse(res.StatusCode, res.Header, res.Body, nil)