attempt fixing parseBody
- why? when using the mixfetch SDK, i was encountering issues, when posting requests to specific endpoints. It was not parsing the response correctly with: Error: panic:syscall/js: Value.Call: property getReader is not a function By updating the above, i've tested this works on all variations of post and get request using mixfetch. Locally I had to upgrade my version of go to 1.20
This commit is contained in:
@@ -223,13 +223,20 @@ func parseHeaders(headers js.Value, reqOpts types.RequestOptions, method string)
|
||||
func parseBody(request *js.Value) (io.Reader, error) {
|
||||
jsBody := request.Get(fieldRequestBody)
|
||||
var bodyReader io.Reader
|
||||
|
||||
if !jsBody.IsUndefined() && !jsBody.IsNull() {
|
||||
log.Debug("stream body - getReader")
|
||||
bodyReader = external.NewStreamReader(jsBody.Call("getReader"))
|
||||
// Check to see if getReader is a function
|
||||
if jsBody.InstanceOf(js.Global().Get("ReadableStream")) && jsBody.Get("getReader").Type() == js.TypeFunction {
|
||||
log.Debug("stream body - getReader")
|
||||
bodyReader = external.NewStreamReader(jsBody.Call("getReader"))
|
||||
} else {
|
||||
// Fall back to using ArrayBuffer
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Body/arrayBuffer
|
||||
log.Debug("getReader not available - fallback to ArrayBuffer")
|
||||
bodyReader = external.NewArrayReader(request.Call("arrayBuffer"))
|
||||
}
|
||||
} else {
|
||||
log.Debug("unstremable body - fallback to ArrayBuffer")
|
||||
// Fall back to using ArrayBuffer
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Body/arrayBuffer
|
||||
log.Debug("unstreamable body - fallback to ArrayBuffer")
|
||||
bodyReader = external.NewArrayReader(request.Call("arrayBuffer"))
|
||||
}
|
||||
|
||||
@@ -237,9 +244,11 @@ func parseBody(request *js.Value) (io.Reader, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Leaving historical notes as reference points
|
||||
// TODO: that seems super awkward. we're constructing a reader only to fully consume it
|
||||
// and create it all over again so that we the recipient wouldn't complain about content-length
|
||||
// surely there must be a better way?
|
||||
|
||||
return bytes.NewReader(bodyBytes), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user