From afebbac9d5bb67b71a4e265e5ab57266eb9ba051 Mon Sep 17 00:00:00 2001 From: aki-0421 <118268728+aki-0421@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:56:34 +0900 Subject: [PATCH] R: fix memory error --- cloudflare/fetch/bind.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/cloudflare/fetch/bind.go b/cloudflare/fetch/bind.go index afc6ea1..28c08e5 100644 --- a/cloudflare/fetch/bind.go +++ b/cloudflare/fetch/bind.go @@ -3,6 +3,7 @@ package fetch import ( "errors" "net/http" + "strconv" "syscall/js" "github.com/syumai/workers/internal/jshttp" @@ -15,8 +16,8 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp if namespace.IsUndefined() { return nil, errors.New("fetch function not found") } - fetchObj := namespace.Get("fetch") - promise := fetchObj.Invoke( + fetchFunc := namespace.Get("fetch") + promise := fetchFunc.Invoke( // The Request object to fetch. // Docs: https://developers.cloudflare.com/workers/runtime-apis/request jshttp.ToJSRequest(req), @@ -30,5 +31,18 @@ func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Resp return nil, err } - return jshttp.ToResponse(jsRes) + // Create TransformStream + ts := js.Global().Get("IdentityTransformStream").New() + readable := ts.Get("readable") + writable := ts.Get("writable") + jsRes.Get("body").Call("pipeTo", writable) + + // Create response + res := new(http.Response) + res.StatusCode = jsRes.Get("status").Int() + res.Status = strconv.Itoa(res.StatusCode) + " " + jsRes.Get("statusText").String() + res.Header = jshttp.ToHeader(jsRes.Get("headers")) + res.Body = jsutil.ConvertReadableStreamToReadCloser(readable) + + return res, nil }