@rhydb/rhydb-wasm 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -0
- package/dist/rhydb_wasm.d.ts +424 -0
- package/dist/rhydb_wasm.js +2 -0
- package/dist/rhydb_wasm.wasm +0 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# SILO WASM
|
|
2
|
+
|
|
3
|
+
This directory builds a browser-usable version of SILO using WebAssembly (WASM). It uses Emscripten to compile the C++ sources to a `.wasm` binary and generates a small JavaScript loader that lets browser code call selected C++ functions.
|
|
4
|
+
|
|
5
|
+
`CMakeLists.txt` collects SILO core sources, excludes the CLI, HTTP API, tests, and benchmark code, and links the dependencies needed for preprocessing and SaneQL query execution. `src/rhydb_wasm.cpp` is the boundary between JavaScript and C++.
|
|
6
|
+
|
|
7
|
+
## What It Exposes
|
|
8
|
+
|
|
9
|
+
The generated JavaScript module exports these embind functions:
|
|
10
|
+
|
|
11
|
+
- `preprocess(preprocessingConfigPath)`: reads a SILO preprocessing config from Emscripten's virtual filesystem and returns a database handle.
|
|
12
|
+
- `save(handle, outputDirectory)`: writes the in-memory database as SILO's normal processed-state files into the virtual filesystem.
|
|
13
|
+
- `load(stateDirectory)`: loads a saved SILO processed state from the virtual filesystem and returns a database handle.
|
|
14
|
+
- `query(handle, saneqlQuery)`: runs SaneQL and returns the result as NDJSON.
|
|
15
|
+
- `info(handle)`: returns database information as JSON.
|
|
16
|
+
- `dispose(handle)`: releases a database handle.
|
|
17
|
+
|
|
18
|
+
The module also exposes Emscripten's `FS` runtime API. Browser code uses `FS` to write uploaded files into the virtual filesystem before calling `preprocess` or `load`, and to read files back after calling `save`.
|
|
19
|
+
|
|
20
|
+
The WASM build supports raw and `.zst` preprocessing inputs. The native `.xz` input path is not included in the WASM target.
|
|
21
|
+
|
|
22
|
+
Browser queries use a smaller materialization cutoff than native SILO to keep intermediate batches within browser memory limits, especially when projecting full nucleotide sequences.
|
|
23
|
+
|
|
24
|
+
## Artifacts
|
|
25
|
+
|
|
26
|
+
Building the WASM target creates these files relative to the SILO repository root:
|
|
27
|
+
|
|
28
|
+
- `wasm/dist/rhydb_wasm.js`: ES module loader generated by Emscripten. Import this from browser code.
|
|
29
|
+
- `wasm/dist/rhydb_wasm.d.ts`: Autogenerated type declarations.
|
|
30
|
+
- `wasm/dist/rhydb_wasm.wasm`: compiled SILO C++ code and linked dependencies.
|
|
31
|
+
|
|
32
|
+
All files must be served by the web app. The JavaScript loader fetches the `.wasm` file at runtime.
|
|
33
|
+
|
|
34
|
+
## Install from NPM
|
|
35
|
+
|
|
36
|
+
The WASM build is also published as an NPM package.
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
npm install @rhydb/rhydb-wasm
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The package is an ES module. Import the loader as a default import:
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import createRhyDbModule from "@rhydb/rhydb-wasm";
|
|
46
|
+
|
|
47
|
+
const silo = await createRhyDbModule();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The `.wasm` file ships alongside `rhydb_wasm.js` and must be resolvable by your bundler so the loader can fetch it at runtime. The package version matches the SILO release version.
|
|
51
|
+
|
|
52
|
+
The current build uses pthreads, so the page serving the module must be cross-origin isolated. Pthreads are the POSIX thread API that Emscripten maps to Web Workers, and SILO uses them because its dependencies and query execution are built around threaded native code. Serve it over HTTP with these headers:
|
|
53
|
+
|
|
54
|
+
```text
|
|
55
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
56
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For a local example server, run this from the SILO repository root:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
python3 - <<'PY'
|
|
63
|
+
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
|
64
|
+
|
|
65
|
+
class Handler(SimpleHTTPRequestHandler):
|
|
66
|
+
def end_headers(self):
|
|
67
|
+
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
|
|
68
|
+
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
|
|
69
|
+
super().end_headers()
|
|
70
|
+
|
|
71
|
+
ThreadingHTTPServer(("127.0.0.1", 8088), Handler).serve_forever()
|
|
72
|
+
PY
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Build Reference
|
|
76
|
+
|
|
77
|
+
Prerequisites:
|
|
78
|
+
|
|
79
|
+
- Emscripten SDK activated so `emcmake`, `emcc`, and `em++` are on `PATH`. Install it via
|
|
80
|
+
[emsdk](https://emscripten.org/docs/getting_started/downloads.html):
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
git clone https://github.com/emscripten-core/emsdk.git
|
|
84
|
+
cd emsdk && ./emsdk install latest && ./emsdk activate latest
|
|
85
|
+
source ./emsdk_env.sh
|
|
86
|
+
```
|
|
87
|
+
- CMake
|
|
88
|
+
- Ninja
|
|
89
|
+
- uv
|
|
90
|
+
- a native C++ compiler for Conan build tools
|
|
91
|
+
|
|
92
|
+
From the SILO repository root, build the WASM artifacts:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
make wasm
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The compatibility wrapper `./wasm/scripts/build-wasm.sh` also calls `make wasm`. The Makefile writes the Emscripten Conan profile to `build/wasm/conanprofile-emscripten`, installs dependencies into `build/wasm`, configures CMake with `emcmake`, builds `rhydb_wasm`, and copies the artifacts to `wasm/dist`.
|
|
99
|
+
|
|
100
|
+
## Tests
|
|
101
|
+
|
|
102
|
+
`./test` contains a Node.js smoke test (`node:test`) that loads the built module, preprocesses
|
|
103
|
+
the `testBaseData/unitTestDummyDataset` fixture, runs a SaneQL query, and round-trips a saved
|
|
104
|
+
state through `save`/`load`. Run it with:
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
make wasm-test
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
This builds `rhydb_wasm` first if necessary, then runs `node --test wasm/test/*.test.mjs`.
|
|
111
|
+
|
|
112
|
+
## Example App
|
|
113
|
+
|
|
114
|
+
An example app is available in `./example`. Any static web server can be used as long as it sets the headers. Opening the `index.html` directly as a local file is not enough for the pthread-enabled build.
|
|
115
|
+
|
|
116
|
+
## Usage Reference
|
|
117
|
+
|
|
118
|
+
Import and initialize the module:
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
import createRhydbModule from "./rhydb_wasm.js";
|
|
122
|
+
|
|
123
|
+
const silo = await createRhydbModule();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This assumes `rhydb_wasm.js` and `rhydb_wasm.wasm` are served from the same URL directory. If a web app serves them from different locations, pass Emscripten's `locateFile` option to tell the loader where to fetch `rhydb_wasm.wasm`.
|
|
127
|
+
|
|
128
|
+
Write uploaded files into the virtual filesystem and preprocess:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
function mkdirp(path) {
|
|
132
|
+
let current = "";
|
|
133
|
+
for (const part of path.split("/").filter(Boolean)) {
|
|
134
|
+
current += `/${part}`;
|
|
135
|
+
if (!silo.FS.analyzePath(current).exists) {
|
|
136
|
+
silo.FS.mkdir(current);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
mkdirp("/input");
|
|
142
|
+
silo.FS.writeFile("/input/preprocessing_config.yaml", preprocessingConfigBytes);
|
|
143
|
+
silo.FS.writeFile("/input/database_config.yaml", databaseConfigBytes);
|
|
144
|
+
silo.FS.writeFile("/input/reference_genomes.json", referenceGenomeBytes);
|
|
145
|
+
silo.FS.writeFile("/input/sequences.ndjson.zst", ndjsonBytes);
|
|
146
|
+
|
|
147
|
+
silo.FS.chdir("/input");
|
|
148
|
+
const handle = silo.preprocess("preprocessing_config.yaml");
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Query an in-memory database:
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
const ndjson = silo.query(handle, "default.groupBy({count:=count()})");
|
|
155
|
+
console.log(ndjson);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Save a processed state and read the files back:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
mkdirp("/state");
|
|
162
|
+
silo.save(handle, "/state");
|
|
163
|
+
|
|
164
|
+
for (const entry of silo.FS.readdir("/state")) {
|
|
165
|
+
if (entry !== "." && entry !== "..") {
|
|
166
|
+
console.log(entry);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Load an existing processed state:
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
mkdirp("/loaded-state");
|
|
175
|
+
// Write the saved SILO state files under /loaded-state first.
|
|
176
|
+
const loadedHandle = silo.load("/loaded-state");
|
|
177
|
+
console.log(silo.info(loadedHandle));
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Release memory when a database is no longer needed:
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
silo.dispose(handle);
|
|
184
|
+
silo.dispose(loadedHandle);
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
For a complete browser example that uploads input files, downloads a saved state, uploads that state again, and runs a query, see `example/`.
|