Skip to content
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

Static + Dynamic PowerSync Builds #19

Merged
merged 362 commits into from
Nov 18, 2024
Merged

Static + Dynamic PowerSync Builds #19

merged 362 commits into from
Nov 18, 2024

Conversation

stevensJourney
Copy link
Collaborator

@stevensJourney stevensJourney commented Nov 7, 2024

Overview

This supersedes the following PRs:

Update

This PR updates our fork with the latest changes from upstream. Our PowerSync core Git submodule had build issues when just pulling in the latest changes. Build issues have been resolved by removing the use of Git submodules in this repository.

Noteable updates

  • Upstream now supports table change notifications. Our old implementation has been removed.
  • Upstream has removed prepare_v2 from SQLiteAPI. Our SDK now has to use a different method for batchExecute.
  • WA SQLite is now on version 1.0.4. This PR includes a major version bump to @journeyapps/[email protected]

PowerSync Core

The PowerSync core is no longer provided from a Git submodule. The libpowersync-wasm.a binary is now downloaded from the powersync-sqlite-core releases during compilation. The PowerSync core is statically linked in the dist/wa-sqlite.wasm and dist/wa-sqlite-async.wasm distributables.

The PowerSync specific source has been separated from the SQLite source.

The PowerSync extension can be loaded in the static build with

const { default: moduleFactory } = await import('@journeyapps/dist/wa-sqlite.mjs');
  const module = await moduleFactory();

  module.ccall('setup_powersync', 'int', []);
  const sqlite3 = SQLite.Factory(module);

Dynamic extension loading

See here and here for reference.

We now provide dist/wa-sqlite-dynamic-main.wasm and dist/wa-sqlite-async-dynamic-main.wasm distributables which support dynamic extension loading. These distributables are compiled as Emscripten main modules.

The PowerSync side module WASM files are configured to be downloaded in a postinstall script.

The PowerSync extension can be loaded with:

const { default: moduleFactory } = await import('@journeyapps/dist/wa-sqlite-dynamic-main.mjs');
  const module = await moduleFactory({
    // locateFile(path) {
    //   // (OPTIONAL) locateFile can be specified to change WASM module resolution.
    //   // The returned url is passed to fetch, and can be a relative path
    //   // or absolute URL.
    //   // This applies to both the main wasm file, and the extension.
    //   return `../dist/${path}`;
    // },
  });

  const sqlite3 = SQLite.Factory(module);
  // Load the extension library into this scope
  let extScope = {};
  await module.loadDynamicLibrary(
     'libpowersync.wasm',
      { loadAsync: true },
      extScope
    );

    // This calls sqlite3_auto_extension(sqlite3_powersync_init).
    // For generic extensions, we'd need to call sqlite3_auto_extension directly.
    extScope.powersync_init_static();

Runtime errors could occur if the main module does not export all the required symbols which side modules require. We currently don't have a full list of all the common/required symbols for our PowerSync side module. This list could also change if other SQLite extensions are loaded. In order to ensure stability, the main module is currently compiled with the option MAIN_MODULE=1. This has the unfortunate affect of a large file-size (3.7mb).

This was referenced Nov 7, 2024
@stevensJourney stevensJourney marked this pull request as ready for review November 7, 2024 17:30
rkistner
rkistner previously approved these changes Nov 11, 2024
@stevensJourney stevensJourney merged commit 86a2b16 into master Nov 18, 2024
2 checks passed
@stevensJourney stevensJourney deleted the powersync_builds branch November 18, 2024 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants