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) {
|
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())
|
b := make([]byte, m.Body.Get("byteLength").Int())
|
||||||
js.CopyBytesToGo(b, m.Body)
|
js.CopyBytesToGo(b, m.Body)
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
|
|
||||||
}
|
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user