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