1
0
mirror of https://github.com/Luzifer/wasm-openssl.git synced 2024-09-18 23:42:57 +00:00

Improve error handling

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-09-18 14:06:59 +02:00
parent d469b667c7
commit 64dd5ad67e
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
3 changed files with 15 additions and 13 deletions

View File

@ -14,6 +14,6 @@ $ ls -lh main.wasm*
Chrome dev console output:
```
(index):14 (2) ["encryptResponse", "U2FsdGVkX1/baq9kUCX1EmUY/XZfnz7CwqGr70vqo6g="]
(index):10 (2) ["decryptResponse", "Knut"]
index.js:6 (3) ["encryptResponse", "U2FsdGVkX1+IAEdepsByQ9zEm11UWw4QSBPYsMzfiio=", null]
index.js:2 (3) ["decryptResponse", "Knut", null]
```

View File

@ -1,13 +1,15 @@
function decryptResponse(plaintext) {
console.log(["decryptResponse", plaintext])
function decryptResponse(plaintext, error) {
console.log(["decryptResponse", plaintext, error])
}
function encryptResponse(ciphertext) {
console.log(["encryptResponse", ciphertext])
decrypt(ciphertext, "password", decryptResponse)
function encryptResponse(ciphertext, error) {
console.log(["encryptResponse", ciphertext, error])
if (error === null) {
decrypt(ciphertext, "password", decryptResponse)
}
}
function wasmStartSuccess() {
function opensslLoaded() {
encrypt("Knut", "password", encryptResponse)
}

10
main.go
View File

@ -12,7 +12,7 @@ func main() {
js.Global().Set("encrypt", js.NewCallback(encrypt))
// Trigger custom "event"
js.Global().Call("wasmStartSuccess")
js.Global().Call("opensslLoaded")
<-make(chan struct{}, 0)
}
@ -31,11 +31,11 @@ func decrypt(i []js.Value) {
o := openssl.New()
plaintext, err := o.DecryptString(password, ciphertext)
if err != nil {
println(fmt.Sprintf("decrypt failed: %s", err))
callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err))
return
}
callback.Invoke(string(plaintext))
callback.Invoke(string(plaintext), nil)
}
func encrypt(i []js.Value) {
@ -53,9 +53,9 @@ func encrypt(i []js.Value) {
o := openssl.New()
ciphertext, err := o.EncryptString(password, plaintext)
if err != nil {
println(fmt.Sprintf("encrypt failed: %s", err))
callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err))
return
}
callback.Invoke(string(ciphertext))
callback.Invoke(string(ciphertext), nil)
}