@livestore/sqlite-wasm 3.46.0-dev.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 +232 -0
- package/bin/index.js +61 -0
- package/index.d.ts +8055 -0
- package/index.mjs +7 -0
- package/package.json +65 -0
- package/sqlite-wasm/jswasm/speedtest1.js +21064 -0
- package/sqlite-wasm/jswasm/speedtest1.wasm +0 -0
- package/sqlite-wasm/jswasm/sqlite3-api-bundler-friendly.mjs +14346 -0
- package/sqlite-wasm/jswasm/sqlite3-api-node.mjs +11456 -0
- package/sqlite-wasm/jswasm/sqlite3-api.js +14344 -0
- package/sqlite-wasm/jswasm/sqlite3-api.mjs +14344 -0
- package/sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs +21010 -0
- package/sqlite-wasm/jswasm/sqlite3-node.mjs +18029 -0
- package/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js +1029 -0
- package/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs +35 -0
- package/sqlite-wasm/jswasm/sqlite3-worker1-promiser-bundler-friendly.js +263 -0
- package/sqlite-wasm/jswasm/sqlite3-worker1-promiser.js +273 -0
- package/sqlite-wasm/jswasm/sqlite3-worker1.js +46 -0
- package/sqlite-wasm/jswasm/sqlite3.js +21065 -0
- package/sqlite-wasm/jswasm/sqlite3.mjs +21029 -0
- package/sqlite-wasm/jswasm/sqlite3.wasm +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# SQLite Wasm
|
|
2
|
+
|
|
3
|
+
SQLite Wasm conveniently wrapped as an ES Module.
|
|
4
|
+
|
|
5
|
+
> **Warning**
|
|
6
|
+
>
|
|
7
|
+
> This project wraps the code of
|
|
8
|
+
> [SQLite Wasm](https://sqlite.org/wasm/doc/trunk/index.md) with _no_ changes,
|
|
9
|
+
> apart from added TypeScript types. Please do _not_ file issues or feature
|
|
10
|
+
> requests regarding the underlying SQLite Wasm code here. Instead, please
|
|
11
|
+
> follow the
|
|
12
|
+
> [SQLite bug filing instructions](https://www.sqlite.org/src/wiki?name=Bug+Reports).
|
|
13
|
+
> Filing TypeScript type related issues and feature requests is fine.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @sqlite.org/sqlite-wasm
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
There are three ways to use SQLite Wasm:
|
|
24
|
+
|
|
25
|
+
- [in the main thread with a wrapped worker](#in-a-wrapped-worker-with-opfs-if-available)
|
|
26
|
+
(🏆 preferred option)
|
|
27
|
+
- [in a worker](#in-a-worker-with-opfs-if-available)
|
|
28
|
+
- [in the main thread](#in-the-main-thread-without-opfs)
|
|
29
|
+
|
|
30
|
+
Only the worker versions allow you to use the origin private file system (OPFS)
|
|
31
|
+
storage back-end.
|
|
32
|
+
|
|
33
|
+
### In a wrapped worker (with OPFS if available):
|
|
34
|
+
|
|
35
|
+
> **Warning**
|
|
36
|
+
>
|
|
37
|
+
> For this to work, you need to set the following headers on your server:
|
|
38
|
+
>
|
|
39
|
+
> `Cross-Origin-Opener-Policy: same-origin`
|
|
40
|
+
>
|
|
41
|
+
> `Cross-Origin-Embedder-Policy: require-corp`
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { sqlite3Worker1Promiser } from '@sqlite.org/sqlite-wasm';
|
|
45
|
+
|
|
46
|
+
const log = (...args) => console.log(...args);
|
|
47
|
+
const error = (...args) => console.error(...args);
|
|
48
|
+
|
|
49
|
+
(async () => {
|
|
50
|
+
try {
|
|
51
|
+
log('Loading and initializing SQLite3 module...');
|
|
52
|
+
|
|
53
|
+
const promiser = await new Promise((resolve) => {
|
|
54
|
+
const _promiser = sqlite3Worker1Promiser({
|
|
55
|
+
onready: () => {
|
|
56
|
+
resolve(_promiser);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
log('Done initializing. Running demo...');
|
|
62
|
+
|
|
63
|
+
let response;
|
|
64
|
+
|
|
65
|
+
response = await promiser('config-get', {});
|
|
66
|
+
log('Running SQLite3 version', response.result.version.libVersion);
|
|
67
|
+
|
|
68
|
+
response = await promiser('open', {
|
|
69
|
+
filename: 'file:mydb.sqlite3?vfs=opfs',
|
|
70
|
+
});
|
|
71
|
+
const { dbId } = response;
|
|
72
|
+
log(
|
|
73
|
+
'OPFS is available, created persisted database at',
|
|
74
|
+
response.result.filename.replace(/^file:(.*?)\?vfs=opfs$/, '$1'),
|
|
75
|
+
);
|
|
76
|
+
// Your SQLite code here.
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (!(err instanceof Error)) {
|
|
79
|
+
err = new Error(err.result.message);
|
|
80
|
+
}
|
|
81
|
+
error(err.name, err.message);
|
|
82
|
+
}
|
|
83
|
+
})();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The `promiser` object above implements the
|
|
87
|
+
[Worker1 API](https://sqlite.org/wasm/doc/trunk/api-worker1.md#worker1-methods).
|
|
88
|
+
|
|
89
|
+
### In a worker (with OPFS if available):
|
|
90
|
+
|
|
91
|
+
> **Warning**
|
|
92
|
+
>
|
|
93
|
+
> For this to work, you need to set the following headers on your server:
|
|
94
|
+
>
|
|
95
|
+
> `Cross-Origin-Opener-Policy: same-origin`
|
|
96
|
+
>
|
|
97
|
+
> `Cross-Origin-Embedder-Policy: require-corp`
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
// In `main.js`.
|
|
101
|
+
const worker = new Worker('worker.js', { type: 'module' });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
// In `worker.js`.
|
|
106
|
+
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
107
|
+
|
|
108
|
+
const log = (...args) => console.log(...args);
|
|
109
|
+
const error = (...args) => console.error(...args);
|
|
110
|
+
|
|
111
|
+
const start = function (sqlite3) {
|
|
112
|
+
log('Running SQLite3 version', sqlite3.version.libVersion);
|
|
113
|
+
let db;
|
|
114
|
+
if ('opfs' in sqlite3) {
|
|
115
|
+
db = new sqlite3.oo1.OpfsDb('/mydb.sqlite3');
|
|
116
|
+
log('OPFS is available, created persisted database at', db.filename);
|
|
117
|
+
} else {
|
|
118
|
+
db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
|
|
119
|
+
log('OPFS is not available, created transient database', db.filename);
|
|
120
|
+
}
|
|
121
|
+
// Your SQLite code here.
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
log('Loading and initializing SQLite3 module...');
|
|
125
|
+
sqlite3InitModule({
|
|
126
|
+
print: log,
|
|
127
|
+
printErr: error,
|
|
128
|
+
}).then((sqlite3) => {
|
|
129
|
+
log('Done initializing. Running demo...');
|
|
130
|
+
try {
|
|
131
|
+
start(sqlite3);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
error(err.name, err.message);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The `db` object above implements the
|
|
139
|
+
[Object Oriented API #1](https://sqlite.org/wasm/doc/trunk/api-oo1.md).
|
|
140
|
+
|
|
141
|
+
### In the main thread (without OPFS):
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
145
|
+
|
|
146
|
+
const log = (...args) => console.log(...args);
|
|
147
|
+
const error = (...args) => console.error(...args);
|
|
148
|
+
|
|
149
|
+
const start = function (sqlite3) {
|
|
150
|
+
log('Running SQLite3 version', sqlite3.version.libVersion);
|
|
151
|
+
const db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
|
|
152
|
+
// Your SQLite code here.
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
log('Loading and initializing SQLite3 module...');
|
|
156
|
+
sqlite3InitModule({
|
|
157
|
+
print: log,
|
|
158
|
+
printErr: error,
|
|
159
|
+
}).then((sqlite3) => {
|
|
160
|
+
try {
|
|
161
|
+
log('Done initializing. Running demo...');
|
|
162
|
+
start(sqlite3);
|
|
163
|
+
} catch (err) {
|
|
164
|
+
error(err.name, err.message);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The `db` object above implements the
|
|
170
|
+
[Object Oriented API #1](https://sqlite.org/wasm/doc/trunk/api-oo1.md).
|
|
171
|
+
|
|
172
|
+
## Usage with vite
|
|
173
|
+
|
|
174
|
+
If you are using [vite](https://vitejs.dev/), you need to add the following
|
|
175
|
+
config option in `vite.config.js`:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
import { defineConfig } from 'vite';
|
|
179
|
+
|
|
180
|
+
export default defineConfig({
|
|
181
|
+
server: {
|
|
182
|
+
headers: {
|
|
183
|
+
'Cross-Origin-Opener-Policy': 'same-origin',
|
|
184
|
+
'Cross-Origin-Embedder-Policy': 'require-corp',
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
optimizeDeps: {
|
|
188
|
+
exclude: ['@sqlite.org/sqlite-wasm'],
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Check out a
|
|
194
|
+
[sample project](https://stackblitz.com/edit/vitejs-vite-ttrbwh?file=main.js)
|
|
195
|
+
that shows this in action.
|
|
196
|
+
|
|
197
|
+
## Demo
|
|
198
|
+
|
|
199
|
+
See the [demo](https://github.com/sqlite/sqlite-wasm/tree/main/demo) folder for
|
|
200
|
+
examples of how to use this in the main thread and in a worker. (Note that the
|
|
201
|
+
worker variant requires special HTTP headers, so it can't be hosted on GitHub
|
|
202
|
+
Pages.) An example that shows how to use this with vite is available on
|
|
203
|
+
[StackBlitz](https://stackblitz.com/edit/vitejs-vite-ttrbwh?file=main.js).
|
|
204
|
+
|
|
205
|
+
## Projects using this package
|
|
206
|
+
|
|
207
|
+
See the list of
|
|
208
|
+
[npm dependents](https://www.npmjs.com/browse/depended/@sqlite.org/sqlite-wasm)
|
|
209
|
+
for this package.
|
|
210
|
+
|
|
211
|
+
## Deploying a new version
|
|
212
|
+
|
|
213
|
+
(These steps can only be executed by maintainers.)
|
|
214
|
+
|
|
215
|
+
1. Update the version number in `package.json` reflecting the current
|
|
216
|
+
[SQLite version number](https://sqlite.org/download.html) and add a build
|
|
217
|
+
identifier suffix like `-build1`. The complete version number should read
|
|
218
|
+
something like `3.41.2-build1`.
|
|
219
|
+
1. Run `npm run build` to build the ES Module. This downloads the latest SQLite
|
|
220
|
+
Wasm binary and builds the ES Module.
|
|
221
|
+
1. Run `npm run deploy` to commit the changes, push to GitHub, and publish the
|
|
222
|
+
new version to npm.
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
Apache 2.0.
|
|
227
|
+
|
|
228
|
+
## Acknowledgements
|
|
229
|
+
|
|
230
|
+
This project is based on [SQLite Wasm](https://sqlite.org/wasm), which it
|
|
231
|
+
conveniently wraps as an ES Module and publishes to npm as
|
|
232
|
+
[`@sqlite.org/sqlite-wasm`](https://www.npmjs.com/package/@sqlite.org/sqlite-wasm).
|
package/bin/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import fetch from 'node-fetch';
|
|
3
|
+
import decompress from 'decompress';
|
|
4
|
+
|
|
5
|
+
async function getSqliteWasmDownloadLink() {
|
|
6
|
+
const response = await fetch('https://sqlite.org/download.html');
|
|
7
|
+
const html = await response.text();
|
|
8
|
+
const sqliteWasmLink =
|
|
9
|
+
'https://sqlite.org/' +
|
|
10
|
+
html
|
|
11
|
+
.replace(
|
|
12
|
+
/^.*?<!-- Download product data for scripts to read(.*?)-->.*?$/gms,
|
|
13
|
+
'$1',
|
|
14
|
+
)
|
|
15
|
+
.split(/\n/)
|
|
16
|
+
.filter((row) => /sqlite-wasm/.test(row))[0]
|
|
17
|
+
.split(/,/)[2];
|
|
18
|
+
console.log(`Found SQLite Wasm download link: ${sqliteWasmLink}`);
|
|
19
|
+
return sqliteWasmLink;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function downloadAndUnzipSqliteWasm(sqliteWasmDownloadLink) {
|
|
23
|
+
if (!sqliteWasmDownloadLink) {
|
|
24
|
+
throw new Error('Unable to find SQLite Wasm download link');
|
|
25
|
+
}
|
|
26
|
+
console.log('Downloading and unzipping SQLite Wasm...');
|
|
27
|
+
const response = await fetch(sqliteWasmDownloadLink);
|
|
28
|
+
if (!response.ok || response.status !== 200) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Unable to download SQLite Wasm from ${sqliteWasmDownloadLink}`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
const buffer = await response.arrayBuffer();
|
|
34
|
+
fs.writeFileSync('sqlite-wasm.zip', Buffer.from(buffer));
|
|
35
|
+
const files = await decompress('sqlite-wasm.zip', 'sqlite-wasm', {
|
|
36
|
+
strip: 1,
|
|
37
|
+
filter: (file) =>
|
|
38
|
+
/jswasm/.test(file.path) && /(\.mjs|\.wasm|\.js)$/.test(file.path),
|
|
39
|
+
});
|
|
40
|
+
console.log(
|
|
41
|
+
`Downloaded and unzipped:\n${files
|
|
42
|
+
.map((file) => (/\//.test(file.path) ? '‣ ' + file.path + '\n' : ''))
|
|
43
|
+
.join('')}`,
|
|
44
|
+
);
|
|
45
|
+
fs.rmSync('sqlite-wasm.zip');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function main() {
|
|
49
|
+
try {
|
|
50
|
+
const sqliteWasmLink = await getSqliteWasmDownloadLink();
|
|
51
|
+
await downloadAndUnzipSqliteWasm(sqliteWasmLink);
|
|
52
|
+
fs.copyFileSync(
|
|
53
|
+
'./node_modules/module-workers-polyfill/module-workers-polyfill.min.js',
|
|
54
|
+
'./demo/module-workers-polyfill.min.js',
|
|
55
|
+
);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
console.error(err.name, err.message);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
main();
|