Skip to content

Commit

Permalink
Replace makeHandledPromise with usage of the promises.New package
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Jan 19, 2024
1 parent b881625 commit 334e146
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
23 changes: 0 additions & 23 deletions webcrypto/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,3 @@ const (
// BigUint64ArrayConstructor is the name of the BigUint64ArrayConstructor constructor
BigUint64ArrayConstructor = "BigUint64Array"
)

// makeHandledPromise will create a promise and return its resolve and reject methods,
// wrapped in such a way that it will block the eventloop from exiting before they are
// called even if the promise isn't resolved by the time the current script ends executing.
func (sc *SubtleCrypto) makeHandledPromise() (*goja.Promise, func(interface{}), func(interface{})) {
runtime := sc.vu.Runtime()
callback := sc.vu.RegisterCallback()
p, resolve, reject := runtime.NewPromise()

return p, func(i interface{}) {
// more stuff
callback(func() error {
resolve(i)
return nil
})
}, func(i interface{}) {
// more stuff
callback(func() error {
reject(i)
return nil
})
}
}
17 changes: 9 additions & 8 deletions webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dop251/goja"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/promises"
)

// FIXME: SubtleCrypto is described as an "interface", should it be a nested module
Expand Down Expand Up @@ -37,7 +38,7 @@ type SubtleCrypto struct {
// The `data` parameter should contain the data to be encryption.
func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

// 2.
// We obtain a copy of the key data, because we might need to modify it.
Expand Down Expand Up @@ -129,7 +130,7 @@ func (sc *SubtleCrypto) Encrypt(algorithm, key, data goja.Value) *goja.Promise {
// The `data` parameter should contain the data to be decrypted.
func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

// 2.
// We obtain a copy of the key data, because we might need to modify it.
Expand Down Expand Up @@ -219,7 +220,7 @@ func (sc *SubtleCrypto) Decrypt(algorithm, key, data goja.Value) *goja.Promise {
// The `data` parameter should contain the data to be signed.
func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

// 2.
// We obtain a copy of the key data, because we might need to modify it.
Expand Down Expand Up @@ -322,7 +323,7 @@ func (sc *SubtleCrypto) Sign(algorithm, key, data goja.Value) *goja.Promise {
// The `data` parameter should contain the original signed data.
func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

// 2.
signatureData, err := exportArrayBuffer(sc.vu.Runtime(), signature)
Expand Down Expand Up @@ -421,7 +422,7 @@ func (sc *SubtleCrypto) Verify(algorithm, key, signature, data goja.Value) *goja
//
// The `data` parameter should contain the data to be digested.
func (sc *SubtleCrypto) Digest(algorithm goja.Value, data goja.Value) *goja.Promise {
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)
rt := sc.vu.Runtime()

// Validate that the value we received is either an ArrayBuffer, TypedArray, or DataView
Expand Down Expand Up @@ -488,7 +489,7 @@ func (sc *SubtleCrypto) Digest(algorithm goja.Value, data goja.Value) *goja.Prom
//
// The `keyUsages` parameter is an array of strings indicating what the key can be used for.
func (sc *SubtleCrypto) GenerateKey(algorithm goja.Value, extractable bool, keyUsages []CryptoKeyUsage) *goja.Promise {
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

normalized, err := normalizeAlgorithm(sc.vu.Runtime(), algorithm, OperationIdentifierGenerateKey)
if err != nil {
Expand Down Expand Up @@ -630,7 +631,7 @@ func (sc *SubtleCrypto) ImportKey(
keyUsages []CryptoKeyUsage,
) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

// 2.
ab, err := exportArrayBuffer(rt, keyData)
Expand Down Expand Up @@ -703,7 +704,7 @@ func (sc *SubtleCrypto) ImportKey(
// TODO @oleiade: implement support for JWK format
func (sc *SubtleCrypto) ExportKey(format KeyFormat, key goja.Value) *goja.Promise {
rt := sc.vu.Runtime()
promise, resolve, reject := sc.makeHandledPromise()
promise, resolve, reject := promises.New(sc.vu)

var algorithm Algorithm
algValue := key.ToObject(rt).Get("algorithm")
Expand Down

0 comments on commit 334e146

Please sign in to comment.