-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better support for binary data #1020
Comments
A user found out that the current websocket API lacks support for binary messages: https://community.k6.io/t/converting-audio-file-into-bytes-and-send-over-web-socket/272/ But in order to fix the websocket code, it would be best if we first standardized how we support binary data in general, i.e. this issue... So, because of that and all of the other dependent issues, I'm upping the priority of this issue. |
I took a brief look at this, and the current As suggested in #420 and dop251/goja#51, the way forward seems to be having native support in Goja, which would mean resurrecting the currently unused It doesn't seem like a gargantuan amount of work, but I'm a bit out of my depths here, as Goja internals are quite complex. I can give this a shot if you agree with the approach, let me know. |
I found this issue after needing to post some binary data. I ended up making a change to k6 that would allow me to use Uint8Array in the js code but it's a little hacky. Just wanted to share here in case anyone else needs it or it sparks any other ideas. Basically, I just look at the body parameter and if it quacks like an Uint8Array, I call the goja
With this change, you can use like:
|
@imiric Did you have any luck with the goja ArrayBuffer implementation? Is this still the preferred strategy vs something like I did above? Need any help? |
@caseylucas No, I didn't make much progress with exposing the native ArrayBuffer. Our top priority right now is getting #1007 merged, so while this issue is high on the priority list, it's unlikely to be worked on for a few weeks at least. Your approach looks interesting, though I'm curious about the performance, and it should probably be done outside of |
Is there a workaround that can be used currently, even if it is inefficient? I am using
|
Prompted by this forum topic, it might not be a bad idea if we also support "generator functions" when we have binary data. So, instead of |
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
The previous default behavior of returning string should now be specified as an additional "s" argument. This is a breaking change part of #1020.
I experimented with overriding the first argument passed to the 'message' handler to be a String object with a `buffer` data property, but it doesn't expose the string primitive properties (.length, etc.) so it would be a breaking change. Passing the ArrayBuffer as the second argument is backwards compatible, and hopefully doesn't cause a doubling of memory usage (I'm expecting it to be garbage collected if the argument isn't used). We could detect the number of arguments defined on the handler and only pass the ArrayBuffer if expected, but this would be awkward to implement. Part of #1020
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
The previous default behavior of returning string should now be specified as an additional "s" argument. This is a breaking change part of #1020.
I experimented with overriding the first argument passed to the 'message' handler to be a String object with a `buffer` data property, but it doesn't expose the string primitive properties (.length, etc.) so it would be a breaking change. Passing the ArrayBuffer as the second argument is backwards compatible, and hopefully doesn't cause a doubling of memory usage (I'm expecting it to be garbage collected if the argument isn't used). We could detect the number of arguments defined on the handler and only pass the ArrayBuffer if expected, but this would be awkward to implement. Part of #1020
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
The previous default behavior of returning string should now be specified as an additional "s" argument. This is a breaking change part of #1020.
I experimented with overriding the first argument passed to the 'message' handler to be a String object with a `buffer` data property, but it doesn't expose the string primitive properties (.length, etc.) so it would be a breaking change. Passing the ArrayBuffer as the second argument is backwards compatible, and hopefully doesn't cause a doubling of memory usage (I'm expecting it to be garbage collected if the argument isn't used). We could detect the number of arguments defined on the handler and only pass the ArrayBuffer if expected, but this would be awkward to implement. Part of #1020
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
This is a breaking change part of #1020.
The previous default behavior of returning string should now be specified as an additional "s" argument. This is a breaking change part of #1020.
I experimented with overriding the first argument passed to the 'message' handler to be a String object with a `buffer` data property, but it doesn't expose the string primitive properties (.length, etc.) so it would be a breaking change. Passing the ArrayBuffer as the second argument is backwards compatible, and hopefully doesn't cause a doubling of memory usage (I'm expecting it to be garbage collected if the argument isn't used). We could detect the number of arguments defined on the handler and only pass the ArrayBuffer if expected, but this would be awkward to implement. Part of #1020
We currently don't have a good way to deal with binary data on the JS side yet 😞. We rely on
[]byte
from the Go code, which translates to int arrays in JS, which is far from optimal. These are the places where we currently have binary data that I can think of:open("somefile", "b")
{ responseType: "binary" }
as paramscrypto.randomBytes()
(Add crypto.randomBytes #922)crypto
hashing and digest functions (Add binary outputEncoding to the crypto functions #952)We need a native analogue of the node.js
Buffer
, or better yet - to properly support ESArrayBuffer
objects. We actually haveArrayBuffer
, the code below compiles and runs as you'd expect:but I'm not sure if this
ArrayBuffer
comes from the bundled core.js or from goja. It seems like theArrayBuffer
is from goja (though I wouldn't rely on it very much), but theDataView
andInt32Array
seem to be from core.js 😕So we need to investigate exactly what the situation is and what we can use to handle binary data sensibly...
Related issues: #856, #874, #873
The text was updated successfully, but these errors were encountered: