Skip to content

feat: Initial fork and patching of sql.js #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
5 changes: 5 additions & 0 deletions .changeset/angry-days-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@powersync/sql-js": patch
---

Initial release of sql.js fork with PowerSync.
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
55 changes: 55 additions & 0 deletions .github/workflows/build-package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Ensures package builds correctly
name: Build Package

on:
push:

jobs:
build:
name: Build Packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- name: Setup NodeJS
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-

- name: Install dependencies
run: pnpm install

- name: Install sha3sum
run: |
sudo apt-get update
sudo apt-get install -y libdigest-sha3-perl

- name: Setup Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: "latest"
actions-cache-folder: "emsdk-cache"

- name: Build
run: pnpm build
57 changes: 57 additions & 0 deletions .github/workflows/dev-packages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Action to publish packages under the `next` tag for testing
# Packages are versioned as `0.0.0-{tag}-DATETIMESTAMP`
name: Create Dev Release

on: workflow_dispatch

jobs:
publish:
name: Publish Dev Packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Setup NodeJS
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 9
run_install: false
- name: Add NPM auth
run: echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_TOKEN}}" >> ~/.npmrc
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Install sha3sum
run: |
sudo apt-get update
sudo apt-get install -y libdigest-sha3-perl

- name: Setup Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: "latest"
actions-cache-folder: "emsdk-cache"

- name: Build
run: pnpm build

- name: Publish
run: |
pnpm changeset version --no-git-tag --snapshot dev
pnpm changeset publish --tag dev
63 changes: 63 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Release

on:
push:
branches:
- main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
- uses: pnpm/action-setup@v2
name: Install pnpm
with:
# Pnpm 9.4 introduces this https://github.com/pnpm/pnpm/pull/7633
# which causes workspace:^1.2.0 to be converted to 1.2.0^1.2.0
version: 9.3
run_install: false
- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Install sha3sum
run: |
sudo apt-get update
sudo apt-get install -y libdigest-sha3-perl

- name: Setup Emscripten
uses: mymindstorm/setup-emsdk@v14
with:
version: "latest"
actions-cache-folder: "emsdk-cache"

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
# Update the monorepo lockfile after versioning
version: pnpm changeset:version
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sql.js
dist
.vscode
.DS_STORE
.idea
.fleet
node_modules
*.tmp
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.9.0
42 changes: 42 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# MIT license

Copyright (c) 2017 sql.js authors (see AUTHORS)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

# Some portions of the Makefile taken from:

Copyright 2017 Ryusei Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh
set -e

SQLITE_VERSION="2.7.6"
POWERSYNC_CORE_VERSION="0.4.2"
SQLITE_PATH="sql.js"

if [ -d "$SQLITE_PATH" ]; then
echo "Deleting existing clone"
rm -rf $SQLITE_PATH
fi

git clone --filter=blob:none https://github.com/sql-js/sql.js.git $SQLITE_PATH
cd $SQLITE_PATH

# Main version as of 2024-10-01
git checkout 52e5649

git apply ../patches/*
mkdir -p powersync-libs
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"

make

cd ../
mkdir -p dist
cp -r $SQLITE_PATH/dist/* dist/
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@powersync/sql-js",
"version": "0.0.0",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
},
"description": "SQL.js fork for for JourneyApps PowerSync",
"type": "module",
"main": "sql-asm.js",
"author": "JOURNEYAPPS",
"license": "MIT",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/powersync-ja/powersync-sql-js.git"
},
"bugs": {
"url": "https://github.com/powersync-ja/powersync-js/issues"
},
"homepage": "https://docs.powersync.com",
"scripts": {
"build": "sh build.sh",
"ci:publish": "pnpm publish -r",
"changeset:version": "pnpm changeset version && pnpm install --lockfile-only --frozen-lockfile=false",
"clean": "rm -rf dist sql.js",
"release": "pnpm build && pnpm changeset publish"
},
"devDependencies": {
"@changesets/cli": "^2.29.5"
}
}
34 changes: 34 additions & 0 deletions patches/powersync-update.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
diff --git a/src/update.c b/src/update.c
new file mode 100644
index 0000000..c7e8a23
--- /dev/null
+++ b/src/update.c
@@ -0,0 +1,28 @@
+#include "sqlite3ext.h"
+SQLITE_EXTENSION_INIT1
+
+#include <emscripten.h>
+#include <stdio.h>
+#include <stdint.h>
+
+EM_JS(void, call_js_update_hook, (int db_p, int type, const char* db_name, const char* table_name), {
+ if (typeof onSqliteUpdate === "function") {
+ onSqliteUpdate(db_p, type, UTF8ToString(db_name), UTF8ToString(table_name));
+ } else {
+ console.error("onSqliteUpdate is not defined");
+ }
+});
+
+static void sqlite_update_callback(void* pArg, int type, const char* db, const char* table, sqlite3_int64 rowid) {
+ call_js_update_hook((int)(intptr_t)pArg, type, db, table);
+}
+
+static int register_update_hook_on_open(sqlite3* db, const char** pzErrMsg, const sqlite3_api_routines* pApi) {
+ SQLITE_EXTENSION_INIT2(pApi);
+ sqlite3_update_hook(db, sqlite_update_callback, db);
+ return SQLITE_OK;
+}
+
+void register_update_hook_extension(void) {
+ sqlite3_auto_extension((void(*)(void))register_update_hook_on_open);
+}
Loading
Loading