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

[Fix] SSR Mode Detection #296

Merged
merged 2 commits into from
Sep 9, 2024
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
6 changes: 6 additions & 0 deletions .changeset/four-birds-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@powersync/common': minor
'@powersync/web': minor
---

Fixed SSR Mode detection for DB adapters. Removed the potential for SSR Web Streamining implementations from to perform syncing operations.
3 changes: 1 addition & 2 deletions packages/common/src/client/AbstractPowerSyncDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CrudBatch } from './sync/bucket/CrudBatch';
import { CrudEntry, CrudEntryJSON } from './sync/bucket/CrudEntry';
import { CrudTransaction } from './sync/bucket/CrudTransaction';
import {
AbstractStreamingSyncImplementation,
DEFAULT_CRUD_UPLOAD_THROTTLE_MS,
PowerSyncConnectionOptions,
StreamingSyncImplementation,
Expand Down Expand Up @@ -239,7 +238,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB

protected abstract generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
): AbstractStreamingSyncImplementation;
): StreamingSyncImplementation;

protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;

Expand Down
8 changes: 3 additions & 5 deletions packages/web/src/db/PowerSyncDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
type AbstractStreamingSyncImplementation,
type BucketStorageAdapter,
type PowerSyncBackendConnector,
type PowerSyncCloseOptions,
Expand All @@ -11,7 +10,8 @@ import {
PowerSyncDatabaseOptionsWithDBAdapter,
PowerSyncDatabaseOptionsWithOpenFactory,
PowerSyncDatabaseOptionsWithSettings,
SqliteBucketStorage
SqliteBucketStorage,
StreamingSyncImplementation
} from '@powersync/common';
import { Mutex } from 'async-mutex';
import { WASQLiteOpenFactory } from './adapters/wa-sqlite/WASQLiteOpenFactory';
Expand Down Expand Up @@ -138,9 +138,7 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
return navigator.locks.request(`lock-${this.database.name}`, cb);
}

protected generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
): AbstractStreamingSyncImplementation {
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector): StreamingSyncImplementation {
const remote = new WebRemote(connector);

const syncOptions: WebStreamingSyncImplementationOptions = {
Expand Down
10 changes: 6 additions & 4 deletions packages/web/src/db/adapters/AbstractWebSQLOpenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
* A SSR implementation is loaded if SSR mode is detected.
*/
openDB() {
const isSSR = isServerSide();
if (isSSR && !this.resolvedFlags.disableSSRWarning) {
const {
resolvedFlags: { disableSSRWarning, enableMultiTabs, ssrMode = isServerSide() }
} = this;
if (ssrMode && !disableSSRWarning) {
console.warn(
`
Running PowerSync in SSR mode.
Expand All @@ -29,13 +31,13 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
);
}

if (!this.resolvedFlags.enableMultiTabs) {
if (!enableMultiTabs) {
console.warn(
'Multiple tab support is not enabled. Using this site across multiple tabs may not function correctly.'
);
}

if (isSSR) {
if (ssrMode) {
return new SSRDBAdapter();
}

Expand Down
61 changes: 57 additions & 4 deletions packages/web/src/db/sync/SSRWebStreamingSyncImplementation.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,76 @@
import {
AbstractStreamingSyncImplementation,
AbstractStreamingSyncImplementationOptions,
BaseObserver,
LockOptions,
LockType
LockType,
PowerSyncConnectionOptions,
StreamingSyncImplementation,
SyncStatus,
SyncStatusOptions
} from '@powersync/common';
import { Mutex } from 'async-mutex';

export class SSRStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
export class SSRStreamingSyncImplementation extends BaseObserver implements StreamingSyncImplementation {
syncMutex: Mutex;
crudMutex: Mutex;

isConnected: boolean;
lastSyncedAt?: Date | undefined;
syncStatus: SyncStatus;

constructor(options: AbstractStreamingSyncImplementationOptions) {
super(options);
super();
this.syncMutex = new Mutex();
this.crudMutex = new Mutex();
this.syncStatus = new SyncStatus({});
this.isConnected = false;
}

obtainLock<T>(lockOptions: LockOptions<T>): Promise<T> {
const mutex = lockOptions.type == LockType.CRUD ? this.crudMutex : this.syncMutex;
return mutex.runExclusive(lockOptions.callback);
}

/**
* This is a no-op in SSR mode
*/
async connect(options?: PowerSyncConnectionOptions): Promise<void> {}

async dispose() {}

/**
* This is a no-op in SSR mode
*/
async disconnect(): Promise<void> {}

/**
* This SSR Mode implementation is immediately ready.
*/
async waitForReady() {}

/**
* This will never resolve in SSR Mode.
*/
async waitForStatus(status: SyncStatusOptions) {
return new Promise<void>((r) => {});
}

/**
* Returns a placeholder checkpoint. This should not be used.
*/
async getWriteCheckpoint() {
return '1';
}

/**
* The SSR mode adapter will never complete syncing.
*/
async hasCompletedSync() {
return false;
}

/**
* This is a no-op in SSR mode.
*/
triggerCrudUpload() {}
}
Loading