Skip to content
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

Asynchronous WasmThemis initialization #507

Merged
merged 1 commit into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/wrappers/themis/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ Then import it into your project:
const themis = require('wasm-themis')
```

Finally, wait for WebAssembly code to be loaded:

```javascript
themis.initialized.then(function() {
//
// Now you can use "themis" functions
//
})
```

([_Browserify_ your code][browserify] to use WasmThemis in web apps.)

[browserify]: http://browserify.org
Expand Down
18 changes: 18 additions & 0 deletions src/wrappers/themis/wasm/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* WasmThemis module entry point.
*/

const libthemis = require('./libthemis.js')

Object.assign(module.exports
, require('./secure_cell.js')
, require('./secure_comparator.js')
Expand All @@ -25,3 +27,19 @@ Object.assign(module.exports
, require('./secure_session.js')
, require('./themis_error.js')
)

let resolveInitialization
let initializationPromise = new Promise(function(resolve) {
resolveInitialization = resolve
})

/**
* Themis initialization promise.
*
* Resolved when Themis is loaded and ready to use.
*/
module.exports.initialized = initializationPromise

libthemis["onRuntimeInitialized"] = function() {
resolveInitialization()
}
7 changes: 7 additions & 0 deletions src/wrappers/themis/wasm/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ describe('wasm-themis', function() {
() => new Uint8Array([27, 18, 28, 18, 28]),
{ value: [3, 14, 15, 92, 6] }
]
describe('initialization', function() {
it('resolves "initialized" promise', function(done) {
themis.initialized.then(function() {
done()
})
})
})
describe('KeyPair', function() {
it('generates EC key pairs', function() {
let pair = new themis.KeyPair()
Expand Down