xref: /sqlite-3.40.0/ext/wasm/api/README.md (revision ef9cd12e)
1# sqlite3-api.js And Friends
2
3This is the README for the files `sqlite3-*.js` and
4`sqlite3-wasm.c`. This collection of files is used to build a
5single-file distribution of the sqlite3 WASM API. It is broken into
6multiple JS files because:
7
81. To facilitate including or excluding certain components for
9   specific use cases. e.g. by removing `sqlite3-api-oo1.js` if the
10   OO#1 API is not needed.
11
122. To facilitate modularizing the pieces for use in different WASM
13   build environments. e.g. the files `post-js-*.js` are for use with
14   Emscripten's `--post-js` feature, and nowhere else.
15
163. Certain components must be in their own standalone files in order
17   to be loaded as JS Workers.
18
19Note that the structure described here is the current state of things,
20not necessarily the "final" state.
21
22The overall idea is that the following files get concatenated
23together, in the listed order, the resulting file is loaded by a
24browser client:
25
26- `post-js-header.js`\
27  Emscripten-specific header for the `--post-js` input.
28- `sqlite3-api-prologue.js`\
29  Contains the initial bootstrap setup of the sqlite3 API
30  objects. This is exposed as a function, rather than objects, so that
31  the next step can pass in a config object which abstracts away parts
32  of the WASM environment, to facilitate plugging it in to arbitrary
33  WASM toolchains.
34- `../common/whwasmutil.js`\
35  A semi-third-party collection of JS/WASM utility code intended to
36  replace much of the Emscripten glue. The sqlite3 APIs internally use
37  these APIs instead of their Emscripten counterparts, in order to be
38  more portable to arbitrary WASM toolchains. This API is
39  configurable, in principle, for use with arbitrary WASM
40  toolchains. It is "semi-third-party" in that it was created in order
41  to support this tree but is standalone and maintained together
42  with...
43- `../jaccwabyt/jaccwabyt.js`\
44  Another semi-third-party API which creates bindings between JS
45  and C structs, such that changes to the struct state from either JS
46  or C are visible to the other end of the connection. This is also an
47  independent spinoff project, conceived for the sqlite3 project but
48  maintained separately.
49- `sqlite3-api-glue.js`\
50  Invokes functionality exposed by the previous two files to
51  flesh out low-level parts of `sqlite3-api-prologue.js`. Most of
52  these pieces related to the `sqlite3.capi.wasm` object.
53- `sqlite3-api-oo1.js`\
54  Provides a high-level object-oriented wrapper to the lower-level C
55  API, colloquially known as OO API #1. Its API is similar to other
56  high-level sqlite3 JS wrappers and should feel relatively familiar
57  to anyone familiar with such APIs. That said, it is not a "required
58  component" and can be elided from builds which do not want it.
59- `sqlite3-api-worker1.js`\
60  A Worker-thread-based API which uses OO API #1 to provide an
61  interface to a database which can be driven from the main Window
62  thread via the Worker message-passing interface. Like OO API #1,
63  this is an optional component, offering one of any number of
64  potential implementations for such an API.
65    - `../sqlite3-worker1.js`\
66      Is not part of the amalgamated sources and is intended to be
67      loaded by a client Worker thread. It loads the sqlite3 module
68      and runs the Worker #1 API which is implemented in
69      `sqlite3-api-worker1.js`.
70    - `../sqlite3-worker1-promiser.js`\
71      Is likewise not part of the amalgamated sources and provides
72      a Promise-based interface into the Worker #1 API. This is
73      a far user-friendlier way to interface with databases running
74      in a Worker thread.
75- `sqlite3-api-opfs.js`\
76  is an sqlite3 VFS implementation which supports Google Chrome's
77  Origin-Private FileSystem (OPFS) as a storage layer to provide
78  persistent storage for database files in a browser. It requires...
79    - `../sqlite3-opfs-async-proxy.js`\
80      is the asynchronous backend part of the OPFS proxy. It speaks
81      directly to the (async) OPFS API and channels those results back
82      to its synchronous counterpart. This file, because it must be
83      started in its own Worker, is not part of the amalgamation.
84- `sqlite3-api-cleanup.js`\
85  the previous files temporarily create global objects in order to
86  communicate their state to the files which follow them, and _this_
87  file connects any final components together and cleans up those
88  globals. As of this writing, this code ensures that the previous
89  files leave no more than a single global symbol installed. When
90  adapting the API for non-Emscripten toolchains, this "should"
91  be the only file where changes are needed.
92- `post-js-footer.js`\
93  Emscripten-specific footer for the `--post-js` input. This closes
94  off the lexical scope opened by `post-js-header.js`.
95
96The build process glues those files together, resulting in
97`sqlite3-api.js`, which is everything except for the `post-js-*.js`
98files, and `sqlite3.js`, which is the Emscripten-generated amalgamated
99output and includes the `post-js-*.js` parts, as well as the
100Emscripten-provided module loading pieces.
101
102The non-JS outlier file is `sqlite3-wasm.c`: it is a proxy for
103`sqlite3.c` which `#include`'s that file and adds a couple more
104WASM-specific helper functions, at least one of which requires access
105to private/static `sqlite3.c` internals. `sqlite3.wasm` is compiled
106from this file rather than `sqlite3.c`.
107
108The following files are part of the build process but are injected
109into the build-generated `sqlite3.js` along with `sqlite3-api.js`.
110
111- `extern-pre-js.js`\
112  Emscripten-specific header for Emscripten's `--extern-pre-js`
113  flag. As of this writing, that file is only used for experimentation
114  purposes and holds no code relevant to the production deliverables.
115- `pre-js.js`\
116  Emscripten-specific header for Emscripten's `--pre-js` flag. This
117  file is intended as a place to override certain Emscripten behavior
118  before it starts up, but corner-case Emscripten bugs keep that from
119  being a reality.
120- `extern-post-js.js`\
121  Emscripten-specific header for Emscripten's `--extern-post-js`
122  flag. This file overwrites the Emscripten-installed
123  `sqlite3InitModule()` function with one which, after the module is
124  loaded, also initializes the asynchronous parts of the sqlite3
125  module. For example, the OPFS VFS support.
126