-
Notifications
You must be signed in to change notification settings - Fork 1
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for the crypto.getRandomValues operation #21
Conversation
20931bc
to
18a8241
Compare
6604769
to
988b42d
Compare
988b42d
to
5b4af8e
Compare
82f7022
to
6085a79
Compare
6085a79
to
5c1e92e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 馃憦 Just left some nitpicks.
As for the limitations you mentioned, I see you've resolved the first one. 馃憤
I think the second one is possible, but you'd need to first make the error object constructor available to the goja VM, and then use it in Go to create an instance. I took a glance at goja tests, but my goja-fu is not great, so I summon the wizardry of @mstoykov.
But technically, these are DOMException
errors, and exposing that wouldn't make sense for k6. We could expose them as separate constructors I guess, and choose to not follow the spec slightly. I think that would be better than the currently thrown plain object. Let's maybe create an issue for it and fix it later?
webcrypto/crypto.go
Outdated
var ( | ||
isInt8Array = IsInstanceOf(c.vu.Runtime(), typedArray, Int8ArrayConstructor) | ||
isUint8Array = IsInstanceOf(c.vu.Runtime(), typedArray, Uint8ArrayConstructor) | ||
isUint8ClampedArray = IsInstanceOf(c.vu.Runtime(), typedArray, Uint8ClampedArrayConstructor) | ||
isInt16Array = IsInstanceOf(c.vu.Runtime(), typedArray, Int16ArrayConstructor) | ||
isUint16Array = IsInstanceOf(c.vu.Runtime(), typedArray, Uint16ArrayConstructor) | ||
isInt32Array = IsInstanceOf(c.vu.Runtime(), typedArray, Int32ArrayConstructor) | ||
isUint32Array = IsInstanceOf(c.vu.Runtime(), typedArray, Uint32ArrayConstructor) | ||
) | ||
|
||
// 1. | ||
if !isInt8Array && | ||
!isUint8Array && | ||
!isInt16Array && | ||
!isUint16Array && | ||
!isInt32Array && | ||
!isUint32Array && | ||
!isUint8ClampedArray { | ||
common.Throw(c.vu.Runtime(), NewError(0, TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about looping over the valid types instead? This way you can short-circuit and avoid the unnecessary IsInstanceOf()
calls if a valid type is found. A negligible optimization, to be sure, but it's slightly neater IMO.
Consider:
func (c *Crypto) validateTypedArray(val goja.Value) bool {
validTypes := []JSType{
Int8ArrayConstructor, Uint8ArrayConstructor, Uint8ArrayConstructor,
Uint8ClampedArrayConstructor, Int16ArrayConstructor, Uint16ArrayConstructor,
Int32ArrayConstructor, Uint32ArrayConstructor,
}
var valid bool
for _, t := range validTypes {
if valid = IsInstanceOf(c.vu.Runtime(), val, t); valid {
break
}
}
return valid
}
Since GetRandomValues()
is already broken up in steps, it might make it a bit more readable to turn each step into a separate smaller unexported method (or maybe just the validation?). Technically, step 2 below is also part of validation, so you might want to return an error
from the validate method depending on if it fails the type or length validation. That way you can call common.Throw()
only once here.
None of this is a big deal, so feel free to disregard if you prefer it the way it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for this, it sparked new ideas on how to approach it indeed 馃檱馃徎 What I ended up doing was:
- rewriting the
IsInstanceOf
method to take the target types as variadic args, and iterate over them using your method instead. - replacing this specific call you pointed out to use the new prototype of
IsInstanceOf
Regarding the structure of the function itself, I'd rather keep it as is. At least during the first implementation run. Considering the small scope of the function, I find it more convenient to track the implementation's correctness to have inlined and mirroring the specs. I find that splitting in smaller functions will prove useful in later functionalities (encrypt, decrypt, etc), but for the functions in the crypto
namespace, I would judge it overkill abstraction for now 馃憤馃徎
AFAIK this will require that you actually define this error and then make instances of them. |
This PR addresses #19
It adds an implementation for the
crypto.getRandomValues
operation to the extension.The underlying implementation depends on the Go
rand/crypto.Read
method as a PRNG. This standard functionality relies on the underlying OS' pseudo-random number generator, such as/dev/urandom
on Linux.This implementation suffers a few limitations that shouldn't be a deal-breaker, but for which I couldn't find any acceptable workaround at this day:
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
,Uint32Array
orUInt8ClampedArray
) on the underlyingArrayBuffer
should be accepted. I couldn't figure out how to do that in Goja. I'd be really grateful if you have any idea on that front 馃檱馃徎QuotaExceededError
should be returned under certain conditions. At the moment I have defined specific errors the Go way, and I throw them usingcommon.Throw
; but that doesn't allow me to, for instance,switch
on them in the context of acatch
statement, as in:Let me know what you think 馃檱馃徎