mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
stop treating string body as bytes body
This commit is contained in:
parent
85790d319c
commit
0d08d319d6
@ -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) {
|
||||
b := make([]byte, m.Body.Get("byteLength").Int())
|
||||
js.CopyBytesToGo(b, m.Body)
|
||||
return b, nil
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user