stop treating string body as bytes body

This commit is contained in:
syumai 2025-01-15 21:03:29 +09:00
parent 85790d319c
commit 0d08d319d6
2 changed files with 7 additions and 19 deletions

View File

@ -67,16 +67,11 @@ func (m *ConsumerMessage) StringBody() (string, error) {
} }
func (m *ConsumerMessage) BytesBody() ([]byte, error) { func (m *ConsumerMessage) BytesBody() ([]byte, error) {
switch m.Body.Type() { if m.Body.Type() != js.TypeObject ||
case js.TypeString: !(m.Body.InstanceOf(jsutil.Uint8ArrayClass) || m.Body.InstanceOf(jsutil.Uint8ClampedArrayClass)) {
return []byte(m.Body.String()), nil return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
case js.TypeObject:
if m.Body.InstanceOf(jsutil.Uint8ArrayClass) || m.Body.InstanceOf(jsutil.Uint8ClampedArrayClass) {
b := make([]byte, m.Body.Get("byteLength").Int())
js.CopyBytesToGo(b, m.Body)
return b, nil
}
} }
b := make([]byte, m.Body.Get("byteLength").Int())
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body) js.CopyBytesToGo(b, m.Body)
return b, nil
} }

View File

@ -174,13 +174,6 @@ func TestConsumerMessage_BytesBody(t *testing.T) {
want []byte want []byte
wantErr bool wantErr bool
}{ }{
{
name: "string",
body: func() js.Value {
return js.ValueOf("hello")
},
want: []byte("hello"),
},
{ {
name: "uint8 array", name: "uint8 array",
body: func() js.Value { body: func() js.Value {
@ -202,7 +195,7 @@ func TestConsumerMessage_BytesBody(t *testing.T) {
{ {
name: "incorrect type", name: "incorrect type",
body: func() js.Value { body: func() js.Value {
return js.ValueOf(42) return js.ValueOf("hello")
}, },
wantErr: true, wantErr: true,
}, },