Skip to content

Commit

Permalink
chore: rename existing wasm engine to wasm-js
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Phelps <[email protected]>
  • Loading branch information
markphelps committed Aug 12, 2024
1 parent 62b1594 commit 1e14906
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 279 deletions.
File renamed without changes.
File renamed without changes
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"flipt-engine-ffi": "0.1.15",
"flipt-engine-wasm": "0.1.0"
"flipt-engine-wasm-js": "0.2.0"
}
29 changes: 25 additions & 4 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ The client SDKs are responsible for the following:

## WebAssembly

[`flipt-engine-wasm`](./flipt-engine-wasm) is a Rust library that compiles to [WebAssembly](https://webassembly.org/) and is designed to be embedded in the client-side SDKs that run in the browser.
### JavaScript

[`flipt-engine-wasm-js`](./flipt-engine-wasm-js) is a Rust library that compiles to [WebAssembly](https://webassembly.org/) and is designed to be embedded in the client-side SDKs that run in the browser.

It uses [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) to interface with the JavaScript runtime, which is why it's only supported in JavaScript.

The client-side SDKs send context to the client engine via the WebAssembly interface and receive the results of the evaluation from the engine.

Expand All @@ -61,14 +65,31 @@ Because of the current limitations of WebAssembly, the engine is designed to be
You can refer to the architecture diagram below:

<p align="center">
<img src=".github/images/architecture-wasm.png" alt="Client SDKs Architecture (WASM)" width="500px" />
<img src=".github/images/architecture-wasm-js.png" alt="Client SDKs Architecture (WASM)" width="500px" />
</p>

### Native

[`flipt-engine-wasm`](./flipt-engine-wasm) is a Rust library that compiles to WebAssembly without relying on wasm-bindgen. This approach allows for a more portable WebAssembly module that can be used in various environments beyond just JavaScript runtimes.

The native library is designed to be embedded in the native language client SDKs.

The architecture for the native WebAssembly implementation can be visualized as follows:

<p align="center">
<img src=".github/images/architecture-wasm-native.png" alt="Client SDKs Architecture (WASM Native)" width="500px" />
</p>

In this architecture, the `flipt-engine-wasm` library compiles to a raw WebAssembly module. The host environment (which could be various runtimes or languages) is responsible for loading and instantiating the WebAssembly module, as well as providing the necessary imports and handling exports.

This approach offers greater flexibility and portability, allowing the Flipt engine to be integrated into a wider range of environments and languages that support WebAssembly, beyond just web browsers.


### Responsibilities

#### WASM Engine
#### WASM Engine (JavaScript)

The [`flipt-engine-wasm`](./flipt-engine-wasm) library is responsible for the following:
The [`flipt-engine-wasm-js`](./flipt-engine-wasm-js) library is responsible for the following:

- Deserializing the evaluation state from JSON to memory.
- Calling the evaluation logic from the [`flipt-evaluation`](./flipt-evaluation) library to evaluate context against the evaluation state and returning the evaluation results.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[workspace]
resolver = "2"
members = ["./flipt-engine-ffi", "./flipt-engine-wasm", "./flipt-evaluation", "./flipt-evaluation-wasi"]
members = ["./flipt-engine-ffi", "./flipt-engine-wasm-js", "./flipt-evaluation"]

[profile.release]
strip = true
rpath = true

[profile.release.package.flipt-engine-wasm]
opt-level = "s"
[profile.release.package.flipt-engine-wasm-js]
opt-level = "s"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Currently, we support the following languages/platforms:
1. [Ruby](./flipt-client-ruby) (FFI)
1. [NodeJS](./flipt-client-node) (FFI)
1. [Java](./flipt-client-java) (FFI)
1. [JavaScript/Typescript (Browser)](./flipt-client-browser) (WASM)
1. [JavaScript/Typescript (Browser)](./flipt-client-browser) (WASM (JavaScript))

Documentation for each client can be found in the README of that client's directory.

Expand Down
6 changes: 3 additions & 3 deletions build/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func browserBuild(ctx context.Context, client *dagger.Client, hostDirectory *dag
rust := client.Container().From("rust:1.74.0-bookworm").
WithWorkdir("/src").
WithDirectory("/src/flipt-engine-ffi", hostDirectory.Directory("flipt-engine-ffi")).
WithDirectory("/src/flipt-engine-wasm", hostDirectory.Directory("flipt-engine-wasm"), dagger.ContainerWithDirectoryOpts{
WithDirectory("/src/flipt-engine-wasm-js", hostDirectory.Directory("flipt-engine-wasm-js"), dagger.ContainerWithDirectoryOpts{
Exclude: []string{"pkg/", ".gitignore"},
}).
WithDirectory("/src/flipt-evaluation", hostDirectory.Directory("flipt-evaluation")).
Expand All @@ -83,7 +83,7 @@ func browserBuild(ctx context.Context, client *dagger.Client, hostDirectory *dag

rust, err = rust.
WithExec([]string{"cargo", "install", "wasm-pack"}). // Install wasm-pack
WithWorkdir("/src/flipt-engine-wasm").
WithWorkdir("/src/flipt-engine-wasm-js").
WithExec([]string{"wasm-pack", "build", "--target", "web"}). // Build the wasm package
Sync(ctx)

Expand All @@ -95,7 +95,7 @@ func browserBuild(ctx context.Context, client *dagger.Client, hostDirectory *dag
WithDirectory("/src", hostDirectory.Directory("flipt-client-browser"), dagger.ContainerWithDirectoryOpts{
Exclude: []string{".node_modules/", ".gitignore", "pkg/"},
}).
WithDirectory("/src/dist", rust.Directory("/src/flipt-engine-wasm/pkg"), dagger.ContainerWithDirectoryOpts{
WithDirectory("/src/dist", rust.Directory("/src/flipt-engine-wasm-js/pkg"), dagger.ContainerWithDirectoryOpts{
Exclude: []string{".node_modules/", ".gitignore", "package.json", "README.md", "LICENSE"},
}).
WithWorkdir("/src").
Expand Down
4 changes: 2 additions & 2 deletions flipt-client-browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ This allows you to update the flag state in a controlled manner, such as in a po

## Development

### WASM
### WASM (JavaScript)

This library uses a WebAssembly (WASM) layer to interact with the Flipt server. It is written in Rust and exposes a JavaScript API using the `wasm-bindgen` and `wasm-pack` tools. We wrap the built WASM layer in a JavaScript API to make it easier to use in a browser environment.

### Prerequisites

- [flipt-engine-wasm](../flipt-engine-wasm)
- [flipt-engine-wasm-js](../flipt-engine-wasm-js)

### Build

Expand Down
4 changes: 2 additions & 2 deletions flipt-client-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import init, { Engine } from '../dist/flipt_engine_wasm.js';
import wasm from '../dist/flipt_engine_wasm_bg.wasm';
import init, { Engine } from '../dist/flipt_engine_wasm_js.js';
import wasm from '../dist/flipt_engine_wasm_js_bg.wasm';

import {
BatchResult,
Expand Down
2 changes: 2 additions & 0 deletions flipt-client-go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

*.wasm
Loading

0 comments on commit 1e14906

Please sign in to comment.