SQLite adds WASM support for using the DBMS in a web browser

SQLite developers are developing a project to implement the ability to compile the library into an intermediate WebAssembly code that can run in a web browser and is suitable for organizing work with the database from web applications in the JavaScript language. The code for WebAssembly support has been added to the main project repository. Unlike the WebSQL API, which is based on SQLite, WASM SQLite is completely isolated from the browser and does not affect its security (Google decided to drop support for WebSQL in Chrome after several vulnerabilities in SQLite that could be exploited through WebSQL to attack the browser) .

The goal of the project is to provide a working JavaScript binding that is identical in functionality to the SQLite API. Web developers are provided with a high-level object-oriented interface for working with data in the style of sql.js or Node.js, wrapping over a low-level C API, and an API based on the Web Worker mechanism that allows you to create asynchronous handlers that run on separate threads. To hide the intricacies of organizing work with threads on top of the Web Worker-based API, a variant of the programming interface based on the Promise mechanism is also being developed.

The data that web applications store in the WASM version of SQLite can be localized within the current session (lost after page reloads) or client-side persisted (persisted between sessions). For persistent storage, backends have been prepared for placing data in a local file system using OPFS (Origin-Private FileSystem, an extension to the File System Access API, available so far only in browsers based on WebKit and Chromium) and in local browser storages based on the window.localStorage API and window.sessionStorage. When using localStorage/sessionStorage, the data is mapped to the appropriate key/value stores, while when using OPFS, there are two options: simulating a virtual file system using WASMFS, and a separate sqlite3_vfs implementation that offers an OPFS-based SQLite VFS layer.

To build SQLite into a WASM representation, the Emscripten compiler is used (it is enough to build the ext/wasm extension: "./configure --enable-all; make sqlite3.c; cd ext/wasm; make"). The output is sqlite3.js and sqlite3.wasm files that you can include in your JavaScript project (HTML and JavaScript example).

Source: opennet.ru

Add a comment