Skip to content

Commit

Permalink
Make blob the default binaryType
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
mstoykov committed Sep 10, 2024
1 parent 75c13b2 commit 5044343
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
12 changes: 2 additions & 10 deletions websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (r *WebSocketsAPI) websocket(c sobek.ConstructorCall) *sobek.Object {
obj: rt.NewObject(),
tagsAndMeta: params.tagsAndMeta,
sendPings: ping{timestamps: make(map[string]time.Time)},
binaryType: blobBinaryType,
}

// Maybe have this after the goroutine below ?!?
Expand Down Expand Up @@ -421,10 +422,6 @@ func (w *webSocket) loop() {
}
}

const binarytypeError = `websocket's binaryType hasn't been set to either "blob" or "arraybuffer", ` +
`but a binary message has been received. ` +
`"blob" is still not the default so the websocket is erroring out`

func (w *webSocket) queueMessage(msg *message) {
w.tq.Queue(func() error {
if w.readyState != OPEN {
Expand All @@ -446,12 +443,7 @@ func (w *webSocket) queueMessage(msg *message) {

if msg.mtype == websocket.BinaryMessage {
var data any
// Lets error out for a k6 release, at least, when there's no binaryType set.
// In the future, we'll use "blob" as default, as per spec:
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
switch w.binaryType {
case "":
return errors.New(binarytypeError)
case blobBinaryType:
var err error
data, err = rt.New(w.blobConstructor, rt.ToValue([]interface{}{msg.data}))
Expand All @@ -461,7 +453,7 @@ func (w *webSocket) queueMessage(msg *message) {
case arraybufferBinaryType:
data = rt.NewArrayBuffer(msg.data)
default:
return fmt.Errorf(`unknown binaryType %s, the supported ones are "blob" and "arraybuffer"`, w.binaryType)
return fmt.Errorf(`unknown binaryType %q, the supported ones are "blob" and "arraybuffer"`, w.binaryType)
}
must(rt, ev.DefineDataProperty("data", rt.ToValue(data), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
} else {
Expand Down
19 changes: 9 additions & 10 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ func TestBinaryState(t *testing.T) {
}
})
if (ws.binaryType != "") {
throw new Error("Wrong binaryType value, expected empty got "+ ws.binaryType)
if (ws.binaryType != "blob") {
throw new Error("Wrong binaryType value, expected to be blob got "+ ws.binaryType)
}
var thrown = false;
Expand All @@ -330,11 +330,10 @@ func TestBinaryState(t *testing.T) {
thrown = true
}
if (!thrown) {
throw new Error("Expects ws.binaryType to not be writable")
throw new Error("Expects ws.binaryType to be writable only with valid values")
}
`))
require.Error(t, err)
require.ErrorContains(t, err, binarytypeError)
require.NoError(t, err)
logs := hook.Drain()
require.Len(t, logs, 0)
}
Expand All @@ -349,21 +348,21 @@ func TestBinaryType_Default(t *testing.T) {
ws.addEventListener("open", () => {
const sent = new Uint8Array([164,41]).buffer
ws.send(sent)
ws.onmessage = (e) => {
ws.onmessage = async (e) => {
if (!(e.data instanceof Blob)) {
throw new Error("Wrong event.data type; expected: Blob, got: "+ typeof e.data)
}
const received = await e.data.arrayBuffer();
if (sent.byteLength !== e.data.arrayBuffer().byteLength) {
throw new Error("The data received isn't equal to the data sent")
if (sent.byteLength !== received.byteLength) {
throw new Error("The data received " + received.byteLength +" isn't equal to the data sent "+ sent.byteLength)
}
ws.close()
}
})
`))
require.Error(t, err)
require.ErrorContains(t, err, binarytypeError)
require.NoError(t, err)
logs := hook.Drain()
require.Len(t, logs, 0)
}
Expand Down

0 comments on commit 5044343

Please sign in to comment.