1fbfe5af9Sstephan# sqlite3-api.js And Friends 2fbfe5af9Sstephan 3e1c34624SstephanThis is the README for the files `sqlite3-*.js` and 4e1c34624Sstephan`sqlite3-wasm.c`. This collection of files is used to build a 5e1c34624Sstephansingle-file distribution of the sqlite3 WASM API. It is broken into 6e1c34624Sstephanmultiple JS files because: 7fbfe5af9Sstephan 8fbfe5af9Sstephan1. To facilitate including or excluding certain components for 9fbfe5af9Sstephan specific use cases. e.g. by removing `sqlite3-api-oo1.js` if the 10fbfe5af9Sstephan OO#1 API is not needed. 11fbfe5af9Sstephan 12fbfe5af9Sstephan2. To facilitate modularizing the pieces for use in different WASM 13fbfe5af9Sstephan build environments. e.g. the files `post-js-*.js` are for use with 14fbfe5af9Sstephan Emscripten's `--post-js` feature, and nowhere else. 15fbfe5af9Sstephan 16fbfe5af9Sstephan3. Certain components must be in their own standalone files in order 17fbfe5af9Sstephan to be loaded as JS Workers. 18fbfe5af9Sstephan 19fbfe5af9SstephanNote that the structure described here is the current state of things, 20fbfe5af9Sstephannot necessarily the "final" state. 21fbfe5af9Sstephan 22fbfe5af9SstephanThe overall idea is that the following files get concatenated 23fbfe5af9Sstephantogether, in the listed order, the resulting file is loaded by a 24fbfe5af9Sstephanbrowser client: 25fbfe5af9Sstephan 26fbfe5af9Sstephan- `sqlite3-api-prologue.js`\ 27fbfe5af9Sstephan Contains the initial bootstrap setup of the sqlite3 API 28fbfe5af9Sstephan objects. This is exposed as a function, rather than objects, so that 29fbfe5af9Sstephan the next step can pass in a config object which abstracts away parts 30fbfe5af9Sstephan of the WASM environment, to facilitate plugging it in to arbitrary 31fbfe5af9Sstephan WASM toolchains. 325b0e63ebSstephan- `../common/whwasmutil.js`\ 33fbfe5af9Sstephan A semi-third-party collection of JS/WASM utility code intended to 34fbfe5af9Sstephan replace much of the Emscripten glue. The sqlite3 APIs internally use 35fbfe5af9Sstephan these APIs instead of their Emscripten counterparts, in order to be 36fbfe5af9Sstephan more portable to arbitrary WASM toolchains. This API is 37fbfe5af9Sstephan configurable, in principle, for use with arbitrary WASM 38fbfe5af9Sstephan toolchains. It is "semi-third-party" in that it was created in order 39fbfe5af9Sstephan to support this tree but is standalone and maintained together 40fbfe5af9Sstephan with... 415b0e63ebSstephan- `../jaccwabyt/jaccwabyt.js`\ 42fbfe5af9Sstephan Another semi-third-party API which creates bindings between JS 43fbfe5af9Sstephan and C structs, such that changes to the struct state from either JS 44fbfe5af9Sstephan or C are visible to the other end of the connection. This is also an 45fbfe5af9Sstephan independent spinoff project, conceived for the sqlite3 project but 46fbfe5af9Sstephan maintained separately. 47fbfe5af9Sstephan- `sqlite3-api-glue.js`\ 489892883eSstephan Invokes functionality exposed by the previous two files to 499892883eSstephan flesh out low-level parts of `sqlite3-api-prologue.js`. Most of 509892883eSstephan these pieces related to the `sqlite3.capi.wasm` object. 518ffc9899Sstephan- `sqlite3-api-build-version.js`\ 528ffc9899Sstephan Gets created by the build process and populates the 538ffc9899Sstephan `sqlite3.version` object. This part is not critical, but records the 548ffc9899Sstephan version of the library against which this module was built. 55fbfe5af9Sstephan- `sqlite3-api-oo1.js`\ 56fbfe5af9Sstephan Provides a high-level object-oriented wrapper to the lower-level C 57fbfe5af9Sstephan API, colloquially known as OO API #1. Its API is similar to other 58fbfe5af9Sstephan high-level sqlite3 JS wrappers and should feel relatively familiar 59fbfe5af9Sstephan to anyone familiar with such APIs. That said, it is not a "required 60fbfe5af9Sstephan component" and can be elided from builds which do not want it. 61453af2f6Sstephan- `sqlite3-api-worker1.js`\ 62fbfe5af9Sstephan A Worker-thread-based API which uses OO API #1 to provide an 63fbfe5af9Sstephan interface to a database which can be driven from the main Window 64fbfe5af9Sstephan thread via the Worker message-passing interface. Like OO API #1, 65fbfe5af9Sstephan this is an optional component, offering one of any number of 66fbfe5af9Sstephan potential implementations for such an API. 67*3117dc60Sstephan - `sqlite3-worker1.js`\ 68fbfe5af9Sstephan Is not part of the amalgamated sources and is intended to be 69fbfe5af9Sstephan loaded by a client Worker thread. It loads the sqlite3 module 70453af2f6Sstephan and runs the Worker #1 API which is implemented in 71453af2f6Sstephan `sqlite3-api-worker1.js`. 72*3117dc60Sstephan - `sqlite3-worker1-promiser.js`\ 735b9973d8Sstephan Is likewise not part of the amalgamated sources and provides 745b9973d8Sstephan a Promise-based interface into the Worker #1 API. This is 755b9973d8Sstephan a far user-friendlier way to interface with databases running 765b9973d8Sstephan in a Worker thread. 77fbfe5af9Sstephan- `sqlite3-api-opfs.js`\ 785b9973d8Sstephan is an sqlite3 VFS implementation which supports Google Chrome's 795b9973d8Sstephan Origin-Private FileSystem (OPFS) as a storage layer to provide 805b9973d8Sstephan persistent storage for database files in a browser. It requires... 81*3117dc60Sstephan - `sqlite3-opfs-async-proxy.js`\ 825b9973d8Sstephan is the asynchronous backend part of the OPFS proxy. It speaks 835b9973d8Sstephan directly to the (async) OPFS API and channels those results back 845b9973d8Sstephan to its synchronous counterpart. This file, because it must be 855b9973d8Sstephan started in its own Worker, is not part of the amalgamation. 8698e120fcSstephan- **`api/sqlite3-api-cleanup.js`**\ 8798e120fcSstephan The previous files do not immediately extend the library. Instead 8898e120fcSstephan they add callback functions to be called during its 8998e120fcSstephan bootstrapping. Some also temporarily create global objects in order 9098e120fcSstephan to communicate their state to the files which follow them. This file 9198e120fcSstephan cleans up any dangling globals and runs the API bootstrapping 9298e120fcSstephan process, which is what finally executes the initialization code 9398e120fcSstephan installed by the previous files. As of this writing, this code 9498e120fcSstephan ensures that the previous files leave no more than a single global 9598e120fcSstephan symbol installed. When adapting the API for non-Emscripten 9698e120fcSstephan toolchains, this "should" be the only file where changes are needed. 97fbfe5af9Sstephan 98fbfe5af9SstephanThe build process glues those files together, resulting in 99fbfe5af9Sstephan`sqlite3-api.js`, which is everything except for the `post-js-*.js` 100fbfe5af9Sstephanfiles, and `sqlite3.js`, which is the Emscripten-generated amalgamated 101fbfe5af9Sstephanoutput and includes the `post-js-*.js` parts, as well as the 102fbfe5af9SstephanEmscripten-provided module loading pieces. 103fbfe5af9Sstephan 104e1c34624SstephanThe non-JS outlier file is `sqlite3-wasm.c`: it is a proxy for 105e1c34624Sstephan`sqlite3.c` which `#include`'s that file and adds a couple more 106e1c34624SstephanWASM-specific helper functions, at least one of which requires access 107e1c34624Sstephanto private/static `sqlite3.c` internals. `sqlite3.wasm` is compiled 108e1c34624Sstephanfrom this file rather than `sqlite3.c`. 1096479c5a3Sstephan 1106479c5a3SstephanThe following files are part of the build process but are injected 1116479c5a3Sstephaninto the build-generated `sqlite3.js` along with `sqlite3-api.js`. 1126479c5a3Sstephan 1136479c5a3Sstephan- `extern-pre-js.js`\ 1146479c5a3Sstephan Emscripten-specific header for Emscripten's `--extern-pre-js` 1156479c5a3Sstephan flag. As of this writing, that file is only used for experimentation 1166479c5a3Sstephan purposes and holds no code relevant to the production deliverables. 1176479c5a3Sstephan- `pre-js.js`\ 1186479c5a3Sstephan Emscripten-specific header for Emscripten's `--pre-js` flag. This 1196479c5a3Sstephan file is intended as a place to override certain Emscripten behavior 1206479c5a3Sstephan before it starts up, but corner-case Emscripten bugs keep that from 1216479c5a3Sstephan being a reality. 12298e120fcSstephan- `post-js-header.js`\ 12398e120fcSstephan Emscripten-specific header for the `--post-js` input. It opens up 12498e120fcSstephan a lexical scope by starting a post-run handler for Emscripten. 12598e120fcSstephan- `post-js-footer.js`\ 12698e120fcSstephan Emscripten-specific footer for the `--post-js` input. This closes 12798e120fcSstephan off the lexical scope opened by `post-js-header.js`. 1286479c5a3Sstephan- `extern-post-js.js`\ 1296479c5a3Sstephan Emscripten-specific header for Emscripten's `--extern-post-js` 1306479c5a3Sstephan flag. This file overwrites the Emscripten-installed 1316479c5a3Sstephan `sqlite3InitModule()` function with one which, after the module is 1326479c5a3Sstephan loaded, also initializes the asynchronous parts of the sqlite3 1336479c5a3Sstephan module. For example, the OPFS VFS support. 134