Skip to content

Commit b6c8c2a

Browse files
add patches
1 parent 35d50bc commit b6c8c2a

File tree

7 files changed

+350
-1
lines changed

7 files changed

+350
-1
lines changed

packages/dev-adapter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
"@rollup/plugin-alias": "^5.1.0",
3636
"@types/sql.js": "^1.4.9",
3737
"rollup": "4.14.3",
38-
"sql.js": "link:/Users/stevenontong/Documents/sql.js"
38+
"sql.js": "workspace:*"
3939
}
4040
}

packages/sqljs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sql.js

packages/sqljs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# PowerSync SQL.js
2+
3+
This is a small fork of SQL.js which includes PowerSync.
4+
5+
This is bundled internally via rollup.
6+
7+
This is compiled from source. TODO, don't require users of the repo to build.

packages/sqljs/build.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
set -e
3+
4+
SQLITE_VERSION="2.7.6"
5+
POWERSYNC_CORE_VERSION="0.4.0"
6+
SQLITE_PATH="sql.js"
7+
8+
if [ -d "$SQLITE_PATH" ]; then
9+
echo "Deleting existing clone"
10+
rm -rf $SQLITE_PATH
11+
fi
12+
13+
git clone --depth 1 https://github.com/sql-js/sql.js.git $SQLITE_PATH
14+
15+
cd $SQLITE_PATH
16+
git apply ../patches/*
17+
mkdir -p powersync-libs
18+
curl -L -o powersync-libs/libpowersync-wasm.a "https://github.com/powersync-ja/powersync-sqlite-core/releases/download/v${POWERSYNC_CORE_VERSION}/libpowersync-wasm.a"
19+
20+
make
21+
22+
cd ../
23+
mkdir -p dist
24+
cp -r $SQLITE_PATH/dist/ dist

packages/sqljs/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@powersync/sql.js",
3+
"private": "true",
4+
"version": "0.0.0",
5+
"description": "API definitions for JourneyApps PowerSync",
6+
"type": "module",
7+
"main": "dist/bundle.mjs",
8+
"module": "dist/bundle.mjs",
9+
"types": "lib/index.d.ts",
10+
"author": "JOURNEYAPPS",
11+
"license": "Apache-2.0",
12+
"files": [
13+
"lib",
14+
"dist"
15+
],
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/powersync-ja/powersync-js.git"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/powersync-ja/powersync-js/issues"
22+
},
23+
"homepage": "https://docs.powersync.com",
24+
"scripts": {
25+
"build": "sh build.sh",
26+
"build:prod": "pnpm build",
27+
"clean": "rm -rf dist sql.js"
28+
},
29+
"dependencies": {},
30+
"devDependencies": {}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
diff --git a/src/update.c b/src/update.c
2+
new file mode 100644
3+
index 0000000..a025f71
4+
--- /dev/null
5+
+++ b/src/update.c
6+
@@ -0,0 +1,24 @@
7+
+#include "sqlite3.h"
8+
+#include <emscripten.h>
9+
+
10+
+// Declare a JS function to be callable from C
11+
+// JS hook only receives type, db name, and table name
12+
+EM_JS(void, call_js_update_hook, (int type, const char* db_name, const char* table_name), {
13+
+ console.log('update from sqlite hook');
14+
+ if (typeof onSqliteUpdate === "function") {
15+
+ onSqliteUpdate(type, UTF8ToString(db_name), UTF8ToString(table_name));
16+
+ } else {
17+
+ console.error("onSqliteUpdate is not defined");
18+
+ }
19+
+});
20+
+
21+
+void EMSCRIPTEN_KEEPALIVE sqlite_update_callback(void* pArg, int type, const char* db, const char* table, sqlite3_int64 rowid) {
22+
+ EM_ASM({ console.log("got an update"); });
23+
+ call_js_update_hook(type, db, table);
24+
+}
25+
+
26+
+// Attach this to a sqlite3* connection:
27+
+void EMSCRIPTEN_KEEPALIVE register_sqlite_update_hook(sqlite3* db) {
28+
+ EM_ASM({ console.log("register_sqlite_update_hook: OK"); });
29+
+ sqlite3_update_hook(db, sqlite_update_callback, NULL);
30+
+}
31+
\ No newline at end of file
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
diff --git a/Makefile b/Makefile
2+
index 4530653..5ac01a0 100644
3+
--- a/Makefile
4+
+++ b/Makefile
5+
@@ -14,6 +14,8 @@ SQLITE_AMALGAMATION_ZIP_SHA3 = e7eb4cfb2d95626e782cfa748f534c74482f2c3c93f13ee82
6+
EXTENSION_FUNCTIONS = extension-functions.c
7+
EXTENSION_FUNCTIONS_URL = https://www.sqlite.org/contrib/download/extension-functions.c?get=25
8+
EXTENSION_FUNCTIONS_SHA1 = c68fa706d6d9ff98608044c00212473f9c14892f
9+
+POWERSYNC_STATIC_FILES = powersync-libs/libpowersync-wasm.a
10+
+
11+
12+
EMCC=emcc
13+
14+
@@ -59,7 +61,7 @@ EMFLAGS_DEBUG = \
15+
-s ASSERTIONS=2 \
16+
-O1
17+
18+
-BITCODE_FILES = out/sqlite3.o out/extension-functions.o
19+
+BITCODE_FILES = out/sqlite3.o out/extension-functions.o out/update.o
20+
21+
OUTPUT_WRAPPER_FILES = src/shell-pre.js src/shell-post.js
22+
23+
@@ -76,13 +78,13 @@ all: optimized debug worker
24+
debug: dist/sql-asm-debug.js dist/sql-wasm-debug.js
25+
26+
dist/sql-asm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
27+
- $(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
28+
+ $(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) $(POWERSYNC_STATIC_FILES) -o $@
29+
mv $@ out/tmp-raw.js
30+
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
31+
rm out/tmp-raw.js
32+
33+
dist/sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
34+
- $(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
35+
+ $(EMCC) $(EMFLAGS) $(EMFLAGS_DEBUG) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) $(POWERSYNC_STATIC_FILES) -o $@
36+
mv $@ out/tmp-raw.js
37+
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
38+
rm out/tmp-raw.js
39+
@@ -91,19 +93,19 @@ dist/sql-wasm-debug.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FI
40+
optimized: dist/sql-asm.js dist/sql-wasm.js dist/sql-asm-memory-growth.js
41+
42+
dist/sql-asm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
43+
- $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
44+
+ $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) $(POWERSYNC_STATIC_FILES) -o $@
45+
mv $@ out/tmp-raw.js
46+
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
47+
rm out/tmp-raw.js
48+
49+
dist/sql-wasm.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
50+
- $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
51+
+ $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_WASM) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) $(POWERSYNC_STATIC_FILES) -o $@
52+
mv $@ out/tmp-raw.js
53+
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
54+
rm out/tmp-raw.js
55+
56+
dist/sql-asm-memory-growth.js: $(BITCODE_FILES) $(OUTPUT_WRAPPER_FILES) $(SOURCE_API_FILES) $(EXPORTED_METHODS_JSON_FILES)
57+
- $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM_MEMORY_GROWTH) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) -o $@
58+
+ $(EMCC) $(EMFLAGS) $(EMFLAGS_OPTIMIZED) $(EMFLAGS_ASM_MEMORY_GROWTH) $(BITCODE_FILES) $(EMFLAGS_PRE_JS_FILES) $(POWERSYNC_STATIC_FILES) -o $@
59+
mv $@ out/tmp-raw.js
60+
cat src/shell-pre.js out/tmp-raw.js src/shell-post.js > $@
61+
rm out/tmp-raw.js
62+
@@ -153,6 +155,9 @@ out/extension-functions.o: sqlite-src/$(SQLITE_AMALGAMATION)
63+
# Generate llvm bitcode
64+
$(EMCC) $(SQLITE_COMPILATION_FLAGS) -c sqlite-src/$(SQLITE_AMALGAMATION)/extension-functions.c -o $@
65+
66+
+out/update.o: sqlite-src/$(SQLITE_AMALGAMATION)
67+
+ $(EMCC) $(SQLITE_COMPILATION_FLAGS) -c sqlite-src/$(SQLITE_AMALGAMATION)/update.c -o $@
68+
+
69+
# TODO: This target appears to be unused. If we re-instatate it, we'll need to add more files inside of the JS folder
70+
# module.tar.gz: test package.json AUTHORS README.md dist/sql-asm.js
71+
# tar --create --gzip $^ > $@
72+
@@ -182,11 +187,15 @@ sqlite-src/$(SQLITE_AMALGAMATION): cache/$(SQLITE_AMALGAMATION).zip sqlite-src/$
73+
74+
sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS): cache/$(EXTENSION_FUNCTIONS)
75+
mkdir -p sqlite-src/$(SQLITE_AMALGAMATION)
76+
+ cp 'src/update.c' sqlite-src/$(SQLITE_AMALGAMATION)/update.c
77+
echo '$(EXTENSION_FUNCTIONS_SHA1) ./cache/$(EXTENSION_FUNCTIONS)' > cache/check.txt
78+
sha1sum -c cache/check.txt
79+
cp 'cache/$(EXTENSION_FUNCTIONS)' $@
80+
81+
82+
+sqlite-src/$(SQLITE_AMALGAMATION)/update.c: sqlite-src
83+
+ cp 'src/update.c' $@
84+
+
85+
.PHONY: clean
86+
clean:
87+
rm -f out/* dist/* cache/*
88+
diff --git a/src/api.js b/src/api.js
89+
index c7f102b..df7e901 100644
90+
--- a/src/api.js
91+
+++ b/src/api.js
92+
@@ -72,6 +72,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
93+
var SQLITE_UPDATE = 23;
94+
var SQLITE_DELETE = 9;
95+
// var - cwrap function
96+
+ Module['ccall']('powersync_init_static', 'int', []);
97+
+
98+
var sqlite3_open = cwrap("sqlite3_open", "number", ["string", "number"]);
99+
var sqlite3_close_v2 = cwrap("sqlite3_close_v2", "number", ["number"]);
100+
var sqlite3_exec = cwrap(
101+
@@ -122,6 +124,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
102+
"number",
103+
["number", "string"]
104+
);
105+
+ var sqlite3_last_insert_rowid = cwrap("sqlite3_last_insert_rowid", "number", ["number"]);
106+
var sqlite3_step = cwrap("sqlite3_step", "number", ["number"]);
107+
var sqlite3_errmsg = cwrap("sqlite3_errmsg", "string", ["number"]);
108+
var sqlite3_column_count = cwrap(
109+
@@ -239,6 +242,12 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
110+
["number"]
111+
);
112+
113+
+ var registerUpdateHook = cwrap(
114+
+ "register_sqlite_update_hook",
115+
+ "void",
116+
+ ["number"]
117+
+ );
118+
+
119+
var sqlite3_update_hook = cwrap(
120+
"sqlite3_update_hook",
121+
"number",
122+
@@ -833,6 +842,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
123+
}
124+
this.handleError(sqlite3_open(this.filename, apiTemp));
125+
this.db = getValue(apiTemp, "i32");
126+
+ registerUpdateHook(this.db)
127+
+ // Module['ccall']('register_sqlite_update_hook', 'void', ['number'], [apiTemp]);
128+
registerExtensionFunctions(this.db);
129+
// A list of all prepared statements of the database
130+
this.statements = {};
131+
@@ -1155,6 +1166,13 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
132+
return sqlite3_changes(this.db);
133+
};
134+
135+
+ /** TODO
136+
+ @return {number} the last insert id
137+
+ */
138+
+ Database.prototype["lastInsertId"] = function lastInsertId() {
139+
+ return sqlite3_last_insert_rowid(this.db);
140+
+ };
141+
+
142+
var extract_blob = function extract_blob(ptr) {
143+
var size = sqlite3_value_bytes(ptr);
144+
var blob_ptr = sqlite3_value_blob(ptr);
145+
diff --git a/src/exported_functions.json b/src/exported_functions.json
146+
index 3be2595..d1c0037 100644
147+
--- a/src/exported_functions.json
148+
+++ b/src/exported_functions.json
149+
@@ -1,47 +1,50 @@
150+
[
151+
-"_malloc",
152+
-"_free",
153+
-"_sqlite3_open",
154+
-"_sqlite3_exec",
155+
-"_sqlite3_free",
156+
-"_sqlite3_errmsg",
157+
-"_sqlite3_changes",
158+
-"_sqlite3_prepare_v2",
159+
-"_sqlite3_sql",
160+
-"_sqlite3_normalized_sql",
161+
-"_sqlite3_bind_text",
162+
-"_sqlite3_bind_blob",
163+
-"_sqlite3_bind_double",
164+
-"_sqlite3_bind_int",
165+
-"_sqlite3_bind_parameter_index",
166+
-"_sqlite3_step",
167+
-"_sqlite3_column_count",
168+
-"_sqlite3_data_count",
169+
-"_sqlite3_column_double",
170+
-"_sqlite3_column_text",
171+
-"_sqlite3_column_blob",
172+
-"_sqlite3_column_bytes",
173+
-"_sqlite3_column_type",
174+
-"_sqlite3_column_name",
175+
-"_sqlite3_reset",
176+
-"_sqlite3_clear_bindings",
177+
-"_sqlite3_finalize",
178+
-"_sqlite3_close_v2",
179+
-"_sqlite3_create_function_v2",
180+
-"_sqlite3_value_bytes",
181+
-"_sqlite3_value_type",
182+
-"_sqlite3_value_text",
183+
-"_sqlite3_value_int",
184+
-"_sqlite3_value_blob",
185+
-"_sqlite3_value_double",
186+
-"_sqlite3_result_double",
187+
-"_sqlite3_result_null",
188+
-"_sqlite3_result_text",
189+
-"_sqlite3_result_blob",
190+
-"_sqlite3_result_int",
191+
-"_sqlite3_result_int64",
192+
-"_sqlite3_result_error",
193+
-"_sqlite3_aggregate_context",
194+
-"_RegisterExtensionFunctions",
195+
-"_sqlite3_update_hook"
196+
+ "_malloc",
197+
+ "_free",
198+
+ "_sqlite3_open",
199+
+ "_sqlite3_exec",
200+
+ "_sqlite3_free",
201+
+ "_sqlite3_errmsg",
202+
+ "_sqlite3_changes",
203+
+ "_sqlite3_prepare_v2",
204+
+ "_sqlite3_sql",
205+
+ "_sqlite3_normalized_sql",
206+
+ "_sqlite3_bind_text",
207+
+ "_sqlite3_bind_blob",
208+
+ "_sqlite3_bind_double",
209+
+ "_sqlite3_bind_int",
210+
+ "_sqlite3_bind_parameter_index",
211+
+ "_sqlite3_step",
212+
+ "_sqlite3_column_count",
213+
+ "_sqlite3_data_count",
214+
+ "_sqlite3_column_double",
215+
+ "_sqlite3_column_text",
216+
+ "_sqlite3_column_blob",
217+
+ "_sqlite3_column_bytes",
218+
+ "_sqlite3_column_type",
219+
+ "_sqlite3_column_name",
220+
+ "_sqlite3_reset",
221+
+ "_sqlite3_clear_bindings",
222+
+ "_sqlite3_finalize",
223+
+ "_sqlite3_close_v2",
224+
+ "_sqlite3_create_function_v2",
225+
+ "_sqlite3_value_bytes",
226+
+ "_sqlite3_value_type",
227+
+ "_sqlite3_value_text",
228+
+ "_sqlite3_value_int",
229+
+ "_sqlite3_value_blob",
230+
+ "_sqlite3_value_double",
231+
+ "_sqlite3_result_double",
232+
+ "_sqlite3_result_null",
233+
+ "_sqlite3_result_text",
234+
+ "_sqlite3_result_blob",
235+
+ "_sqlite3_result_int",
236+
+ "_sqlite3_result_int64",
237+
+ "_sqlite3_result_error",
238+
+ "_sqlite3_aggregate_context",
239+
+ "_RegisterExtensionFunctions",
240+
+ "_sqlite3_last_insert_rowid",
241+
+ "_sqlite3_update_hook",
242+
+ "_powersync_init_static",
243+
+ "_register_sqlite_update_hook"
244+
]
245+
diff --git a/src/exported_runtime_methods.json b/src/exported_runtime_methods.json
246+
index f099056..b1712fc 100644
247+
--- a/src/exported_runtime_methods.json
248+
+++ b/src/exported_runtime_methods.json
249+
@@ -1,5 +1,6 @@
250+
[
251+
"cwrap",
252+
+"ccall",
253+
"stackAlloc",
254+
"stackSave",
255+
"stackRestore",

0 commit comments

Comments
 (0)