Skip to content

Commit 7536ead

Browse files
Feat: Encryption with multiple ciphers (#23)
* wip: sqlite3mc * random * register vfs * Split multi cipher builds * Add changeset * Export multicipher module * Fix multiple cipher builds * Test emcc flags * Test mc sqlite * WIP: Testing new builds * Add xCurrentTimex64 implementation * xCurrentTimeInt64 implementation * Update emcc flags to normal builds * Clean up * Export non asyncify mc wasm * Clean up demo logs * PR feedback * Revert formatting * Revert formatting * Revert stack size * SImplify xcurrenttime * Add julian epoch reference --------- Co-authored-by: stevensJourney <[email protected]>
1 parent 1bb58d3 commit 7536ead

23 files changed

+244
-17
lines changed

.changeset/violet-laws-prove.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@journeyapps/wa-sqlite": minor
3+
---
4+
5+
Enable database encryption with multiple ciphers

Makefile

+117-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# dependencies
2-
SQLITE_VERSION = version-3.47.0
3-
SQLITE_TARBALL_URL = https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=${SQLITE_VERSION}
4-
2+
SQLITE_VERSION = 3.47.0
3+
MC_SQLITE_VERSION = 1.9.0
4+
SQLITE_TARBALL_URL = https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=version-${SQLITE_VERSION}
5+
MC_SQLITE_URL = https://github.com/utelle/SQLite3MultipleCiphers/releases/download/v${MC_SQLITE_VERSION}/sqlite3mc-${MC_SQLITE_VERSION}-sqlite-${SQLITE_VERSION}-amalgamation.zip
56
EXTENSION_FUNCTIONS = extension-functions.c
67
EXTENSION_FUNCTIONS_URL = https://www.sqlite.org/contrib/download/extension-functions.c?get=25
78
EXTENSION_FUNCTIONS_SHA3 = ee39ddf5eaa21e1d0ebcbceeab42822dd0c4f82d8039ce173fd4814807faabfa
@@ -21,6 +22,17 @@ CFILES = \
2122

2223
POWERSYNC_CFILES = $(notdir $(wildcard powersync-static/*.c))
2324

25+
MC_CFILES = \
26+
sqlite3mc_amalgamation.c \
27+
extension-functions.c \
28+
main.c \
29+
libauthorizer.c \
30+
libfunction.c \
31+
libhook.c \
32+
libprogress.c \
33+
libvfs.c \
34+
$(CFILES_EXTRA)
35+
2436
JSFILES = \
2537
src/libauthorizer.js \
2638
src/libfunction.js \
@@ -35,9 +47,11 @@ vpath %.c powersync-static
3547

3648
POWERSYNC_EXPORTED_FUNCTIONS = powersync-static/powersync_exported_functions.json
3749
EXPORTED_FUNCTIONS = src/exported_functions.json
50+
MC_EXPORTED_FUNCTIONS = multiple-ciphers/mc_exported_functions.json
3851

3952
# EMCC does not support multiple exports files. Need to combine them temporarily
4053
COMBINED_EXPORTED_FUNCTIONS = tmp/combined_exports.json
54+
MC_COMBINED_EXPORTED_FUNCTIONS = tmp/mc_combined_exports.json
4155

4256
EXPORTED_RUNTIME_METHODS = src/extra_exported_runtime_methods.json
4357
ASYNCIFY_IMPORTS = src/asyncify_imports.json
@@ -51,6 +65,9 @@ POWERSYNC_OBJ_FILES_DEBUG = $(patsubst %.c,tmp/powersync-obj/debug/%.o,$(POWERSY
5165
POWERSYNC_OBJ_FILES_DIST = $(patsubst %.c,tmp/powersync-obj/dist/%.o,$(POWERSYNC_CFILES))
5266
POWERSYNC_STATIC_FILES = powersync-libs/libpowersync-wasm.a
5367

68+
MC_OBJ_FILES_DEBUG = $(patsubst %.c,tmp/mc-obj/debug/%.o,$(MC_CFILES))
69+
MC_OBJ_FILES_DIST = $(patsubst %.c,tmp/mc-obj/dist/%.o,$(MC_CFILES))
70+
5471
# build options
5572
EMCC ?= emcc
5673

@@ -99,6 +116,10 @@ COMBINED_EMFLAGS_INTERFACES = \
99116
-s EXPORTED_FUNCTIONS=@$(COMBINED_EXPORTED_FUNCTIONS) \
100117
-s EXPORTED_RUNTIME_METHODS=@$(EXPORTED_RUNTIME_METHODS)
101118

119+
MC_COMBINED_EMFLAGS_INTERFACES = \
120+
-s EXPORTED_FUNCTIONS=@$(MC_COMBINED_EXPORTED_FUNCTIONS) \
121+
-s EXPORTED_RUNTIME_METHODS=@$(EXPORTED_RUNTIME_METHODS)
122+
102123
EMFLAGS_LIBRARIES = \
103124
--js-library src/libadapters.js \
104125
--post-js src/libauthorizer.js \
@@ -129,6 +150,7 @@ WASQLITE_DEFINES = \
129150
-DSQLITE_DEFAULT_MEMSTATUS=0 \
130151
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
131152
-DSQLITE_DQS=0 \
153+
-D__WASM__ \
132154
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
133155
-DSQLITE_MAX_EXPR_DEPTH=0 \
134156
-DSQLITE_OMIT_AUTOINIT \
@@ -171,12 +193,23 @@ clean-deps:
171193
$(COMBINED_EXPORTED_FUNCTIONS): $(EXPORTED_FUNCTIONS) $(POWERSYNC_EXPORTED_FUNCTIONS)
172194
jq -s 'add' $(EXPORTED_FUNCTIONS) $(POWERSYNC_EXPORTED_FUNCTIONS) > $(COMBINED_EXPORTED_FUNCTIONS)
173195

196+
$(MC_COMBINED_EXPORTED_FUNCTIONS): $(EXPORTED_FUNCTIONS) $(POWERSYNC_EXPORTED_FUNCTIONS) $(MC_EXPORTED_FUNCTIONS)
197+
jq -s 'add' $(EXPORTED_FUNCTIONS) $(POWERSYNC_EXPORTED_FUNCTIONS) $(MC_EXPORTED_FUNCTIONS) > $(MC_COMBINED_EXPORTED_FUNCTIONS)
198+
174199
deps/$(SQLITE_VERSION)/sqlite3.h deps/$(SQLITE_VERSION)/sqlite3.c:
175200
mkdir -p cache/$(SQLITE_VERSION)
176201
curl -LsS $(SQLITE_TARBALL_URL) | tar -xzf - -C cache/$(SQLITE_VERSION)/ --strip-components=1
177202
mkdir -p deps/$(SQLITE_VERSION)
178203
(cd deps/$(SQLITE_VERSION); ../../cache/$(SQLITE_VERSION)/configure --enable-all && make sqlite3.c)
179204

205+
# Download and extract sqlite3mc_amalgamation.c to the deps directory
206+
deps/$(SQLITE_VERSION)/sqlite3mc_amalgamation.c:
207+
mkdir -p cache/sqlite3mc-$(MC_SQLITE_VERSION)
208+
curl -LsS $(MC_SQLITE_URL) -o cache/sqlite3mc-$(MC_SQLITE_VERSION)/sqlite3mc.zip
209+
unzip -o cache/sqlite3mc-$(MC_SQLITE_VERSION)/sqlite3mc.zip -d cache/sqlite3mc-$(MC_SQLITE_VERSION)
210+
rm -rf cache/sqlite3mc-$(MC_SQLITE_VERSION)/sqlite3mc.zip
211+
cp cache/sqlite3mc-$(MC_SQLITE_VERSION)/sqlite3mc_amalgamation.c deps/$(SQLITE_VERSION)/sqlite3mc_amalgamation.c
212+
180213
# Download static files from PowerSync Core repository
181214
$(POWERSYNC_STATIC_FILES):
182215
node scripts/download-core-build.js
@@ -241,14 +274,24 @@ tmp/powersync-obj/dist/%.o: %.c
241274
mkdir -p tmp/powersync-obj/dist
242275
$(EMCC) $(CFLAGS_DIST) $(WASQLITE_DEFINES) $^ -c -o $@
243276

277+
# Build multiple ciphers
278+
tmp/mc-obj/debug/%.o: %.c
279+
mkdir -p tmp/mc-obj/debug
280+
$(EMCC) $(CFLAGS_DEBUG) $(WASQLITE_DEFINES) $^ -c -o $@
281+
282+
# Build multiple ciphers
283+
tmp/mc-obj/dist/%.o: %.c
284+
mkdir -p tmp/mc-obj/dist
285+
$(EMCC) $(CFLAGS_DIST) $(WASQLITE_DEFINES) $^ -c -o $@
286+
244287

245288
## debug
246289
.PHONY: clean-debug
247290
clean-debug:
248291
rm -rf debug
249292

250293
.PHONY: debug
251-
debug: debug/wa-sqlite.mjs debug/wa-sqlite-async.mjs debug/wa-sqlite-jspi.mjs debug/wa-sqlite-dynamic-main.mjs debug/wa-sqlite-dynamic-main-async.mjs
294+
debug: debug/wa-sqlite.mjs debug/wa-sqlite-async.mjs debug/wa-sqlite-jspi.mjs debug/wa-sqlite-dynamic-main.mjs debug/wa-sqlite-dynamic-main-async.mjs debug/mc-wa-sqlite.mjs debug/mc-wa-sqlite-async.mjs debug/mc-wa-sqlite-jspi.mjs
252295

253296
# Statically links PowerSync Core
254297
debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
@@ -261,7 +304,7 @@ debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_
261304
$(POWERSYNC_OBJ_FILES_DEBUG) -o $@
262305

263306
# Statically links PowerSync Core
264-
debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
307+
debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
265308
mkdir -p debug
266309
$(EMCC) $(EMFLAGS_DEBUG) \
267310
$(COMBINED_EMFLAGS_INTERFACES) \
@@ -272,7 +315,7 @@ debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EX
272315
$(OBJ_FILES_DEBUG) -o $@
273316

274317
# Statically links PowerSync Core
275-
debug/wa-sqlite-jspi.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
318+
debug/wa-sqlite-jspi.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
276319
mkdir -p debug
277320
$(EMCC) $(EMFLAGS_DEBUG) $(EMFLAGS_DYNAMIC) \
278321
$(COMBINED_EMFLAGS_INTERFACES) \
@@ -282,6 +325,38 @@ debug/wa-sqlite-jspi.mjs: $(OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(JSFI
282325
$(POWERSYNC_OBJ_FILES_DEBUG) \
283326
$(OBJ_FILES_DEBUG) -o $@
284327

328+
# Statically links PowerSync Core
329+
debug/mc-wa-sqlite.mjs: $(MC_OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
330+
mkdir -p debug
331+
$(EMCC) $(EMFLAGS_DEBUG) \
332+
$(COMBINED_EMFLAGS_INTERFACES) \
333+
$(EMFLAGS_LIBRARIES) \
334+
$(POWERSYNC_STATIC_FILES) \
335+
$(OBJ_FILES_DEBUG) \
336+
$(MC_OBJ_FILES_DEBUG) -o $@
337+
338+
# Statically links PowerSync Core
339+
debug/mc-wa-sqlite-async.mjs: $(MC_OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
340+
mkdir -p debug
341+
$(EMCC) $(EMFLAGS_DEBUG) \
342+
$(COMBINED_EMFLAGS_INTERFACES) \
343+
$(EMFLAGS_LIBRARIES) \
344+
$(EMFLAGS_ASYNCIFY_DEBUG) \
345+
$(POWERSYNC_STATIC_FILES) \
346+
$(POWERSYNC_OBJ_FILES_DEBUG) \
347+
$(MC_OBJ_FILES_DEBUG) -o $@
348+
349+
# Statically links PowerSync Core
350+
debug/mc-wa-sqlite-jspi.mjs: $(MC_OBJ_FILES_DEBUG) $(POWERSYNC_OBJ_FILES_DEBUG) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
351+
mkdir -p debug
352+
$(EMCC) $(EMFLAGS_DEBUG) $(EMFLAGS_DYNAMIC) \
353+
$(COMBINED_EMFLAGS_INTERFACES) \
354+
$(EMFLAGS_LIBRARIES) \
355+
$(EMFLAGS_JSPI) \
356+
$(POWERSYNC_STATIC_FILES) \
357+
$(POWERSYNC_OBJ_FILES_DEBUG) \
358+
$(MC_OBJ_FILES_DEBUG) -o $@
359+
285360

286361
# Dynamic main module
287362
# Exported functions are omitted here since everything is currently exported
@@ -330,10 +405,10 @@ clean-dist:
330405
rm -rf dist
331406

332407
.PHONY: dist
333-
dist: dist/wa-sqlite.mjs dist/wa-sqlite-async.mjs dist/wa-sqlite-jspi.mjs dist/wa-sqlite-dynamic-main.mjs dist/wa-sqlite-async-dynamic-main.mjs
408+
dist: dist/wa-sqlite.mjs dist/wa-sqlite-async.mjs dist/wa-sqlite-jspi.mjs dist/wa-sqlite-dynamic-main.mjs dist/wa-sqlite-async-dynamic-main.mjs dist/mc-wa-sqlite.mjs dist/mc-wa-sqlite-async.mjs dist/mc-wa-sqlite-jspi.mjs
334409

335410
# Statically links PowerSync Core
336-
dist/wa-sqlite.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
411+
dist/wa-sqlite.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
337412
mkdir -p dist
338413
$(EMCC) $(EMFLAGS_DIST) \
339414
$(COMBINED_EMFLAGS_INTERFACES) \
@@ -343,7 +418,7 @@ dist/wa-sqlite.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUN
343418
$(OBJ_FILES_DIST) -o $@
344419

345420
# Statically links PowerSync Core
346-
dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
421+
dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
347422
mkdir -p dist
348423
$(EMCC) $(EMFLAGS_DIST) \
349424
$(COMBINED_EMFLAGS_INTERFACES) \
@@ -354,7 +429,7 @@ dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORT
354429
$(OBJ_FILES_DIST) -o $@
355430

356431
# Statically links PowerSync Core
357-
dist/wa-sqlite-jspi.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
432+
dist/wa-sqlite-jspi.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(COMBINED_EXPORTED_FUNCTIONS)
358433
mkdir -p dist
359434
$(EMCC) $(EMFLAGS_DIST) \
360435
$(COMBINED_EMFLAGS_INTERFACES) \
@@ -364,6 +439,38 @@ dist/wa-sqlite-jspi.mjs: $(OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(JSFILES
364439
$(POWERSYNC_OBJ_FILES_DIST) \
365440
$(OBJ_FILES_DIST) -o $@
366441

442+
# Statically links PowerSync Core with multiple ciphers
443+
dist/mc-wa-sqlite.mjs: $(MC_OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
444+
mkdir -p dist
445+
$(EMCC) $(EMFLAGS_DIST) \
446+
$(MC_COMBINED_EMFLAGS_INTERFACES) \
447+
$(EMFLAGS_LIBRARIES) \
448+
$(POWERSYNC_STATIC_FILES) \
449+
$(POWERSYNC_OBJ_FILES_DIST) \
450+
$(MC_OBJ_FILES_DIST) -o $@
451+
452+
# Statically links PowerSync Core with multiple ciphers
453+
dist/mc-wa-sqlite-async.mjs: $(MC_OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
454+
mkdir -p dist
455+
$(EMCC) $(EMFLAGS_DIST) \
456+
$(MC_COMBINED_EMFLAGS_INTERFACES) \
457+
$(EMFLAGS_LIBRARIES) \
458+
$(EMFLAGS_ASYNCIFY_DIST) \
459+
$(POWERSYNC_STATIC_FILES) \
460+
$(POWERSYNC_OBJ_FILES_DIST) \
461+
$(MC_OBJ_FILES_DIST) -o $@
462+
463+
# Statically links PowerSync Core with multiple ciphers
464+
dist/mc-wa-sqlite-jspi.mjs: $(MC_OBJ_FILES_DIST) $(POWERSYNC_OBJ_FILES_DIST) $(JSFILES) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS) $(POWERSYNC_STATIC_FILES) $(MC_COMBINED_EXPORTED_FUNCTIONS)
465+
mkdir -p dist
466+
$(EMCC) $(EMFLAGS_DIST) \
467+
$(MC_COMBINED_EMFLAGS_INTERFACES) \
468+
$(EMFLAGS_LIBRARIES) \
469+
$(EMFLAGS_JSPI) \
470+
$(POWERSYNC_STATIC_FILES) \
471+
$(POWERSYNC_OBJ_FILES_DIST) \
472+
$(MC_OBJ_FILES_DIST) -o $@
473+
367474
# Dynamic main module
368475
# Exported functions are omitted here since everything is currently exported
369476
dist/wa-sqlite-dynamic-main.mjs: $(OBJ_FILES_DIST) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(COMBINED_EXPORTED_FUNCTIONS)

demo/demo-worker.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import * as SQLite from '../src/sqlite-api.js';
44

55
const BUILDS = new Map([
66
['default', '../dist/wa-sqlite.mjs'],
7+
['mc-default', '../dist/mc-wa-sqlite.mjs'],
78
['asyncify', '../dist/wa-sqlite-async.mjs'],
9+
['mc-asyncify', '../dist/mc-wa-sqlite-async.mjs'],
810
['jspi', '../dist/wa-sqlite-jspi.mjs'],
11+
['mc-jspi', '../dist/mc-wa-sqlite-jspi.mjs'],
912
// ['default', '../debug/wa-sqlite.mjs'],
1013
// ['asyncify', '../debug/wa-sqlite-async.mjs'],
1114
// ['jspi', '../debug/wa-sqlite-jspi.mjs'],
@@ -103,7 +106,6 @@ maybeReset().then(async () => {
103106

104107
const sqlite3 = SQLite.Factory(module);
105108

106-
107109
if (buildName.endsWith('-dynamic')) {
108110
const extWasm = EXT_WASM.get(buildName);
109111
// Load the extension library into this scope
@@ -130,6 +132,12 @@ maybeReset().then(async () => {
130132
sqlite3.vfs_register(vfs, true);
131133
}
132134

135+
if(buildName.startsWith('mc-')) {
136+
const createResult = module.ccall('sqlite3mc_vfs_create', 'int', ['string', 'int'], [vfsName, 1]);
137+
if (createResult !== 0) {
138+
throw new Error(`sqlite3mc_vfs_create failed with ${createResult}`);
139+
}
140+
}
133141
// Open the database.
134142
const db = await sqlite3.open_v2(dbName);
135143

dist/mc-wa-sqlite-async.mjs

+16
Large diffs are not rendered by default.

dist/mc-wa-sqlite-async.wasm

1.8 MB
Binary file not shown.

dist/mc-wa-sqlite-jspi.mjs

+16
Large diffs are not rendered by default.

dist/mc-wa-sqlite-jspi.wasm

981 KB
Binary file not shown.

dist/mc-wa-sqlite.mjs

+16
Large diffs are not rendered by default.

dist/mc-wa-sqlite.wasm

975 KB
Binary file not shown.

dist/wa-sqlite-async-dynamic-main.mjs

+1-1
Large diffs are not rendered by default.
258 Bytes
Binary file not shown.

dist/wa-sqlite-async.mjs

+1-1
Large diffs are not rendered by default.

dist/wa-sqlite-async.wasm

2 Bytes
Binary file not shown.

dist/wa-sqlite-dynamic-main.mjs

+1-1
Large diffs are not rendered by default.

dist/wa-sqlite-dynamic-main.wasm

260 Bytes
Binary file not shown.

dist/wa-sqlite-jspi.mjs

+1-1
Large diffs are not rendered by default.

dist/wa-sqlite-jspi.wasm

0 Bytes
Binary file not shown.

dist/wa-sqlite.mjs

+1-1
Large diffs are not rendered by default.

dist/wa-sqlite.wasm

0 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
"_sqlite3mc_vfs_create",
3+
"_sqlite3mc_config",
4+
"_sqlite3mc_cipher_index",
5+
"_sqlite3mc_config_cipher",
6+
"_sqlite3mc_cipher_count",
7+
"_sqlite3mc_cipher_name"
8+
]

src/FacadeVFS.js

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import * as VFS from './VFS.js';
33

44
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
55

6+
// Milliseconds since Julian epoch as a BigInt.
7+
// https://github.com/sqlite/sqlite/blob/e57527c14f7b7cfa6e32eeab5c549d50c4fa3674/src/os_unix.c#L6872-L6882
8+
const UNIX_EPOCH = 24405875n * 8640000n;
9+
610
// Convenience base class for a JavaScript VFS.
711
// The raw xOpen, xRead, etc. function signatures receive only C primitives
812
// which aren't easy to work with. This class provides corresponding calls
@@ -215,6 +219,41 @@ export class FacadeVFS extends VFS.Base {
215219
return this.jOpen(filename, pFile, flags, pOutFlagsView);
216220
}
217221

222+
/**
223+
* @param {number} pVfs
224+
* @param {number} nByte
225+
* @param {number} pCharOut
226+
* @returns {number|Promise<number>}
227+
*/
228+
xRandomness(pVfs, nByte, pCharOut) {
229+
const randomArray = new Uint8Array(nByte);
230+
crypto.getRandomValues(randomArray);
231+
// Copy randomArray to the WebAssembly memory
232+
const buffer = pCharOut; // Pointer to memory in WebAssembly
233+
this._module.HEAPU8.set(randomArray, buffer); // Copy randomArray into memory starting at buffer
234+
return nByte;
235+
}
236+
237+
/**
238+
* Gets the current time as milliseconds since Unix epoch
239+
* @param {number} pVfs pointer to the VFS
240+
* @param {number} pTime pointer to write the time value
241+
* @returns {number} SQLite error code
242+
*/
243+
xCurrentTimeInt64(pVfs, pTime) {
244+
// Create a DataView to write the current time
245+
const timeView = this.#makeTypedDataView('BigInt64', pTime);
246+
247+
const currentTime = BigInt(Date.now());
248+
// Convert the current time to milliseconds since Unix epoch
249+
const value = UNIX_EPOCH + currentTime;
250+
251+
// Write the time value to the pointer location
252+
timeView.setBigInt64(0, value, true);
253+
254+
return VFS.SQLITE_OK;
255+
}
256+
218257
/**
219258
* @param {number} pVfs
220259
* @param {number} zName

src/exported_functions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,4 @@
260260
"_sqlite3_wal_checkpoint",
261261
"_sqlite3_wal_checkpoint_v2",
262262
"_sqlite3_wal_hook"
263-
]
263+
]

src/types/index.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,18 @@ declare module "@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs" {
11331133
export = ModuleFactory;
11341134
}
11351135

1136+
/** @ignore */
1137+
declare module "@journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs" {
1138+
function ModuleFactory(config?: object): Promise<any>;
1139+
export = ModuleFactory;
1140+
}
1141+
1142+
/** @ignore */
1143+
declare module "@journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs" {
1144+
function ModuleFactory(config?: object): Promise<any>;
1145+
export = ModuleFactory;
1146+
}
1147+
11361148
/** @ignore */
11371149
declare module "@journeyapps/wa-sqlite/src/VFS.js" {
11381150
export * from "@journeyapps/wa-sqlite/src/sqlite-constants.js";

0 commit comments

Comments
 (0)