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) {
switch m.Body.Type() {
case js.TypeString:
return []byte(m.Body.String()), nil
case js.TypeObject:
if m.Body.InstanceOf(jsutil.Uint8ArrayClass) || m.Body.InstanceOf(jsutil.Uint8ClampedArrayClass) {
if m.Body.Type() != js.TypeObject ||
!(m.Body.InstanceOf(jsutil.Uint8ArrayClass) || m.Body.InstanceOf(jsutil.Uint8ClampedArrayClass)) {
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
}
b := make([]byte, m.Body.Get("byteLength").Int())
js.CopyBytesToGo(b, m.Body)
return b, nil
}
}
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
}

View File

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