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

Bring back initialized promise #854

Merged
merged 5 commits into from
Jul 14, 2021

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Jul 11, 2021

Restoring compatibility, one step at a time.

Background

Historically, WasmThemis has exported only the initialized promise. WasmThemis was not modularized and kicked off libthemis.wasm download right after the module is loaded. Moreover, there was no way to use a different path to WebAssembly code: it had to be located in a file named libthemis.wasm next to the JavaScript code bundle.

TypeScript rewrite introduced an alternative interface initialize() which allows to specify the URL. However, this required WasmThemis modularization. That is, WasmThemis will not be initialized unless initialize() is called.

This introduces two issues with backwards compatibility:

  1. initialize promise was gone.
  2. You can no longer expect that WasmThemis will be downloaded in background without awaiting any promise.

Due to modularization, there is no (practical) way to fix the second issue. Just don't do, it's a bad practice.

However, absence of initialized is a real breaking change that will break well-formed applications.

Reintroduce the initialized promise and make sure it works by calling the initialize() function behind the scenes. It's a bit of a hack, but it works, so whatever.

Note that initialize is not deprecated or anything. It's a perfectly valid API if you don't need to specify a custom path to libthemis.wasm

Initialization APIs

So basically, now WasmThemis has two initialization APIs.

  • Historically first:

    await themis.initialized

    which will download libthemis.wasm relative to the executing JS bundle.

  • Explicit — available since WasmThemis 0.14.0:

    await themis.initialize()
    // OR
    await themis.initialize('https://your.CND.example.com/storage/libthemis.wasm')

    which allows you to specify the exact URL for downloading WebAssembly code.

Maybe later it will accept more parameters that can be passed to Emscripten (see their docs), but I don't want to expand the API unless there are users for it.

Ancillary changes

Existing unit tests, examples, and tools are rolled back to using initialized.

New tests verify the all initialization APIs work as expected.

Checklist

Historically, WasmThemis has exported only the "initialized" promise.
WasmThemis was not modularized and kicked off "libthemis.wasm" download
right after the module is loaded. Moreover, there was no way to use
a different path to WebAssembly code: it had to be located in a file
named "libthemis.wasm" next to the JavaScript code bundle.

TypeScript rewrite introduced an alternative interface "initialize()"
which allows to specify the URL. However, this required WasmThemis
modularization. That is, WasmThemis *will not* be initialized unless
initialize() is called.

This introduces two issues with backwards compatibility:

  1. "initialize" promise was gone.

  2. You can no longer expect that WasmThemis will be downloaded
     in background without awaiting any promise.

Due to modularization, there is no (practical) way to fix the second
issue. Just don't do, it's a bad practice.

However, absence of "initialized" is a real breaking change that will
break well-formed applications.

Reintroduce the "initialized" promise and make sure it works by calling
the "initialize()" function behind the scenes. It's a bit of a hack, but
it works, so whatever.

Note that "initialize" is *not* deprecated or anything. It's a perfectly
valid API if you don't need to specify a custom path to "libthemis.wasm"
Revert previous changes that made all examples and tests use
"initialize()" due to absence of "initialized". Now that it's there,
let's use the old thing. (And this also ensures that it exists and
working.)
Add a bunch of tests to verify initialization specifically. Each of
these has to be a separate file, separately tested by an individual
mocha process (becaue you can only initialize WasmThemis once).

The "invalid path" test prints out some errors to stderr. This is
default behavior for Emscripten shim. It can be overriden with more
parameters to libthemisFn(), but I'm not ready to introduce too much
customizability to it yet.
We're going through with this API, so let's tell the users that it's
available now.
@ilammy ilammy added the W-WasmThemis 🌐 Wrapper: WasmThemis, JavaScript API, npm packages label Jul 11, 2021
@ilammy ilammy changed the base branch from wasm-typescript to master July 12, 2021 11:07
@ilammy ilammy changed the base branch from master to wasm-typescript July 12, 2021 11:07
@ilammy ilammy marked this pull request as ready for review July 12, 2021 11:07
@ilammy ilammy requested a review from vixentael as a code owner July 12, 2021 11:07
@ilammy ilammy requested a review from a team July 12, 2021 11:07
@ilammy ilammy mentioned this pull request Jul 13, 2021
5 tasks
Copy link
Collaborator

@Lagovas Lagovas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ilammy ilammy merged commit 2c95534 into cossacklabs:wasm-typescript Jul 14, 2021
@ilammy ilammy deleted the typescript-initialized branch July 14, 2021 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
W-WasmThemis 🌐 Wrapper: WasmThemis, JavaScript API, npm packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants