mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-07 02:04:21 +01:00
Merge 'Sync engine defered sync' from Nikita Sivukhin
This PR makes sync client completely autonomous as now it can defer initial sync. This can open possibility to asynchronously create DB in the Turso Cloud while giving user ability to interact with local DB straight away. Closes #3531
This commit is contained in:
@@ -22,15 +22,25 @@ async function process(opts: RunOpts, io: ProtocolIo, request: any) {
|
||||
const requestType = request.request();
|
||||
const completion = request.completion();
|
||||
if (requestType.type == 'Http') {
|
||||
let url: string | null = null;
|
||||
if (typeof opts.url == "function") {
|
||||
url = opts.url();
|
||||
} else {
|
||||
url = opts.url;
|
||||
}
|
||||
if (url == null) {
|
||||
completion.poison(`url is empty - sync is paused`);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
let headers = typeof opts.headers === "function" ? opts.headers() : opts.headers;
|
||||
let headers = typeof opts.headers === "function" ? await opts.headers() : opts.headers;
|
||||
if (requestType.headers != null && requestType.headers.length > 0) {
|
||||
headers = { ...opts.headers };
|
||||
for (let header of requestType.headers) {
|
||||
headers[header[0]] = header[1];
|
||||
}
|
||||
}
|
||||
const response = await fetch(`${opts.url}${requestType.path}`, {
|
||||
const response = await fetch(`${url}${requestType.path}`, {
|
||||
method: requestType.method,
|
||||
headers: headers,
|
||||
body: requestType.body != null ? new Uint8Array(requestType.body) : null,
|
||||
|
||||
@@ -61,7 +61,7 @@ export interface EncryptionOpts {
|
||||
// base64 encoded encryption key (must be either 16 or 32 bytes depending on the cipher)
|
||||
key: string,
|
||||
// encryption cipher algorithm
|
||||
cipher: 'aes256gcm' | 'aes128gcm' | 'chacha20poly1305'
|
||||
cipher: 'aes256gcm' | 'aes128gcm' | 'chacha20poly1305' | 'aegis256'
|
||||
}
|
||||
export interface DatabaseOpts {
|
||||
/**
|
||||
@@ -73,22 +73,26 @@ export interface DatabaseOpts {
|
||||
/**
|
||||
* optional url of the remote database (e.g. libsql://db-org.turso.io)
|
||||
* (if omitted - local-only database will be created)
|
||||
*
|
||||
* you can also promide function which will return URL or null
|
||||
* in this case local database will be created and sync will be "switched-on" whenever the url will return non-empty value
|
||||
* note, that all other parameters (like encryption) must be set in advance in order for the "deferred" sync to work properly
|
||||
*/
|
||||
url?: string;
|
||||
url?: string | (() => string | null);
|
||||
/**
|
||||
* auth token for the remote database
|
||||
* (can be either static string or function which will provide short-lived credentials for every new request)
|
||||
*/
|
||||
authToken?: string | (() => string);
|
||||
authToken?: string | (() => Promise<string>);
|
||||
/**
|
||||
* arbitrary client name which can be used to distinguish clients internally
|
||||
* the library will gurantee uniquiness of the clientId by appending unique suffix to the clientName
|
||||
*/
|
||||
clientName?: string;
|
||||
/**
|
||||
* optional encryption parameters if cloud database were encrypted by default
|
||||
* optional remote encryption parameters if cloud database were encrypted by default
|
||||
*/
|
||||
encryption?: EncryptionOpts;
|
||||
remoteEncryption?: EncryptionOpts;
|
||||
/**
|
||||
* optional callback which will be called for every mutation before sending it to the remote
|
||||
* this callback can transform the update in order to support complex conflict resolution strategy
|
||||
@@ -136,8 +140,8 @@ export interface DatabaseStats {
|
||||
|
||||
export interface RunOpts {
|
||||
preemptionMs: number,
|
||||
url: string,
|
||||
headers: { [K: string]: string } | (() => { [K: string]: string })
|
||||
url: string | (() => string | null),
|
||||
headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>)
|
||||
transform?: Transform,
|
||||
}
|
||||
|
||||
|
||||
@@ -220,7 +220,7 @@ export type DatabaseRowTransformResultJs =
|
||||
export type GeneratorResponse =
|
||||
| { type: 'IO' }
|
||||
| { type: 'Done' }
|
||||
| { type: 'SyncEngineStats', operations: number, mainWal: number, revertWal: number, lastPullUnixTime: number, lastPushUnixTime?: number, revision?: string }
|
||||
| { type: 'SyncEngineStats', operations: number, mainWal: number, revertWal: number, lastPullUnixTime?: number, lastPushUnixTime?: number, revision?: string }
|
||||
| { type: 'SyncEngineChanges', changes: SyncEngineChanges }
|
||||
|
||||
export type JsProtocolRequest =
|
||||
@@ -238,6 +238,8 @@ export interface SyncEngineOpts {
|
||||
tablesIgnore?: Array<string>
|
||||
useTransform: boolean
|
||||
protocolVersion?: SyncEngineProtocolVersion
|
||||
bootstrapIfEmpty: boolean
|
||||
remoteEncryption?: string
|
||||
}
|
||||
|
||||
export declare const enum SyncEngineProtocolVersion {
|
||||
|
||||
@@ -81,8 +81,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-android-arm64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-android-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -97,8 +97,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-android-arm-eabi')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-android-arm-eabi/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -117,8 +117,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-win32-x64-msvc')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-win32-x64-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -133,8 +133,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-win32-ia32-msvc')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-win32-ia32-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -149,8 +149,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-win32-arm64-msvc')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-win32-arm64-msvc/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -168,8 +168,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-darwin-universal')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-darwin-universal/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -184,8 +184,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-darwin-x64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-darwin-x64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -200,8 +200,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-darwin-arm64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-darwin-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -220,8 +220,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-freebsd-x64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-freebsd-x64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -236,8 +236,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-freebsd-arm64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-freebsd-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -257,8 +257,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-x64-musl')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -273,8 +273,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-x64-gnu')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-x64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -291,8 +291,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-arm64-musl')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -307,8 +307,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-arm64-gnu')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -325,8 +325,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-arm-musleabihf')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-musleabihf/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -341,8 +341,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-arm-gnueabihf')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-arm-gnueabihf/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -359,8 +359,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-riscv64-musl')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-musl/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -375,8 +375,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-riscv64-gnu')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-riscv64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -392,8 +392,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-ppc64-gnu')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-ppc64-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -408,8 +408,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-linux-s390x-gnu')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-linux-s390x-gnu/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -428,8 +428,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-openharmony-arm64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -444,8 +444,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-openharmony-x64')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-x64/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
@@ -460,8 +460,8 @@ function requireNative() {
|
||||
try {
|
||||
const binding = require('@tursodatabase/sync-openharmony-arm')
|
||||
const bindingPackageVersion = require('@tursodatabase/sync-openharmony-arm/package.json').version
|
||||
if (bindingPackageVersion !== '0.2.0-pre.11' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.11 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
if (bindingPackageVersion !== '0.2.0-pre.13' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
|
||||
throw new Error(`Native binding package version mismatch, expected 0.2.0-pre.13 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
|
||||
}
|
||||
return binding
|
||||
} catch (e) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { expect, test } from 'vitest'
|
||||
import { connect, Database, DatabaseRowMutation, DatabaseRowTransformResult } from './promise.js'
|
||||
|
||||
const localeCompare = (a, b) => a.x.localeCompare(b.x);
|
||||
const intCompare = (a, b) => a.x - b.x;
|
||||
|
||||
function cleanup(path) {
|
||||
unlinkSync(path);
|
||||
@@ -12,6 +13,15 @@ function cleanup(path) {
|
||||
try { unlinkSync(`${path}-wal-revert`) } catch (e) { }
|
||||
}
|
||||
|
||||
test('simple-db', async () => {
|
||||
const db = new Database({ path: ':memory:' });
|
||||
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }])
|
||||
await db.exec("CREATE TABLE t(x)");
|
||||
await db.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }])
|
||||
await expect(async () => await db.pull()).rejects.toThrowError(/sync is disabled as database was opened without sync support/);
|
||||
})
|
||||
|
||||
test('implicit connect', async () => {
|
||||
const db = new Database({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
const defer = db.prepare("SELECT * FROM not_found");
|
||||
@@ -20,6 +30,74 @@ test('implicit connect', async () => {
|
||||
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
|
||||
})
|
||||
|
||||
test('defered sync', async () => {
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.exec("INSERT INTO t VALUES (100)");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
|
||||
let url = null;
|
||||
const db = new Database({ path: ':memory:', url: () => url });
|
||||
await db.prepare("CREATE TABLE t(x)").run();
|
||||
await db.prepare("INSERT INTO t VALUES (1), (2), (3), (42)").run();
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 42 }]);
|
||||
await expect(async () => await db.pull()).rejects.toThrow(/url is empty - sync is paused/);
|
||||
url = process.env.VITE_TURSO_DB_URL;
|
||||
await db.pull();
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 100 }, { x: 1 }, { x: 2 }, { x: 3 }, { x: 42 }]);
|
||||
})
|
||||
|
||||
test('encryption sync', async () => {
|
||||
const KEY = 'l/FWopMfZisTLgBX4A42AergrCrYKjiO3BfkJUwv83I=';
|
||||
const URL = 'http://encrypted--a--a.localhost:10000';
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
const db1 = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
const db2 = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db1.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
await db2.exec("INSERT INTO t VALUES (4), (5), (6)");
|
||||
expect(await db1.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }]);
|
||||
expect(await db2.prepare("SELECT * FROM t").all()).toEqual([{ x: 4 }, { x: 5 }, { x: 6 }]);
|
||||
await Promise.all([db1.push(), db2.push()]);
|
||||
await Promise.all([db1.pull(), db2.pull()]);
|
||||
const expected = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }, { x: 6 }];
|
||||
expect((await db1.prepare("SELECT * FROM t").all()).sort(intCompare)).toEqual(expected.sort(intCompare));
|
||||
expect((await db2.prepare("SELECT * FROM t").all()).sort(intCompare)).toEqual(expected.sort(intCompare));
|
||||
});
|
||||
|
||||
test('defered encryption sync', async () => {
|
||||
const URL = 'http://encrypted--a--a.localhost:10000';
|
||||
const KEY = 'l/FWopMfZisTLgBX4A42AergrCrYKjiO3BfkJUwv83I=';
|
||||
let url = null;
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.exec("INSERT INTO t VALUES (100)");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
const db = await connect({ path: ':memory:', url: () => url, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }]);
|
||||
|
||||
url = URL;
|
||||
await db.pull();
|
||||
|
||||
const expected = [{ x: 100 }, { x: 1 }, { x: 2 }, { x: 3 }];
|
||||
expect((await db.prepare("SELECT * FROM t").all())).toEqual(expected);
|
||||
});
|
||||
|
||||
test('select-after-push', async () => {
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { DatabasePromise } from "@tursodatabase/database-common"
|
||||
import { ProtocolIo, run, DatabaseOpts, EncryptionOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
|
||||
import { SyncEngine, SyncEngineProtocolVersion } from "#index";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, Database as NativeDatabase } from "#index";
|
||||
import { promises } from "node:fs";
|
||||
|
||||
let NodeIO: ProtocolIo = {
|
||||
@@ -45,6 +45,12 @@ class Database extends DatabasePromise {
|
||||
#io: ProtocolIo;
|
||||
#guards: SyncEngineGuards
|
||||
constructor(opts: DatabaseOpts) {
|
||||
if (opts.url == null) {
|
||||
super(new NativeDatabase(opts.path, { tracing: opts.tracing }) as any);
|
||||
this.#engine = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = new SyncEngine({
|
||||
path: opts.path,
|
||||
clientName: opts.clientName,
|
||||
@@ -52,23 +58,31 @@ class Database extends DatabasePromise {
|
||||
protocolVersion: SyncEngineProtocolVersion.V1,
|
||||
longPollTimeoutMs: opts.longPollTimeoutMs,
|
||||
tracing: opts.tracing,
|
||||
bootstrapIfEmpty: typeof opts.url != "function" || opts.url() != null,
|
||||
remoteEncryption: opts.remoteEncryption?.cipher,
|
||||
});
|
||||
super(engine.db() as unknown as any);
|
||||
|
||||
|
||||
let headers = typeof opts.authToken === "function" ? () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
}) : {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
};
|
||||
let headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>);
|
||||
if (typeof opts.authToken == "function") {
|
||||
const authToken = opts.authToken;
|
||||
headers = async () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${await authToken()}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
const authToken = opts.authToken;
|
||||
headers = {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${authToken}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
};
|
||||
}
|
||||
this.#runOpts = {
|
||||
url: opts.url,
|
||||
headers: headers,
|
||||
@@ -83,8 +97,13 @@ class Database extends DatabasePromise {
|
||||
* connect database and initialize it in case of clean start
|
||||
*/
|
||||
override async connect() {
|
||||
if (this.connected) { return; }
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
if (this.connected) {
|
||||
return;
|
||||
} else if (this.#engine == null) {
|
||||
await super.connect();
|
||||
} else {
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
}
|
||||
this.connected = true;
|
||||
}
|
||||
/**
|
||||
@@ -93,6 +112,9 @@ class Database extends DatabasePromise {
|
||||
* @returns true if new changes were pulled from the remote
|
||||
*/
|
||||
async pull() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
|
||||
if (changes.empty()) {
|
||||
return false;
|
||||
@@ -105,25 +127,37 @@ class Database extends DatabasePromise {
|
||||
* if {@link DatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
|
||||
*/
|
||||
async push() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
|
||||
}
|
||||
/**
|
||||
* checkpoint WAL for local database
|
||||
*/
|
||||
async checkpoint() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
|
||||
}
|
||||
/**
|
||||
* @returns statistic of current local database
|
||||
*/
|
||||
async stats(): Promise<DatabaseStats> {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
|
||||
}
|
||||
/**
|
||||
* close the database
|
||||
*/
|
||||
override async close(): Promise<void> {
|
||||
this.#engine.close();
|
||||
await super.close();
|
||||
if (this.#engine != null) {
|
||||
this.#engine.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
3
bindings/javascript/sync/packages/wasm/cp-entrypoint.sh
Normal file
3
bindings/javascript/sync/packages/wasm/cp-entrypoint.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
sed 's/index-default.js/index-bundle.js/g' promise-default.ts > promise-bundle.ts
|
||||
sed 's/index-default.js/index-turbopack-hack.js/g' promise-default.ts > promise-turbopack-hack.ts
|
||||
sed 's/index-default.js/index-vite-dev-hack.js/g' promise-default.ts > promise-vite-dev-hack.ts
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common"
|
||||
import { DatabasePromise } from "@tursodatabase/database-common"
|
||||
import { ProtocolIo, run, DatabaseOpts, EncryptionOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker } from "./index-bundle.js";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker, Database as NativeDatabase } from "./index-bundle.js";
|
||||
|
||||
let BrowserIO: ProtocolIo = {
|
||||
async read(path: string): Promise<Buffer | Uint8Array | null> {
|
||||
@@ -45,6 +45,12 @@ class Database extends DatabasePromise {
|
||||
#guards: SyncEngineGuards;
|
||||
#worker: Worker | null;
|
||||
constructor(opts: DatabaseOpts) {
|
||||
if (opts.url == null) {
|
||||
super(new NativeDatabase(opts.path, { tracing: opts.tracing }) as any);
|
||||
this.#engine = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = new SyncEngine({
|
||||
path: opts.path,
|
||||
clientName: opts.clientName,
|
||||
@@ -52,23 +58,31 @@ class Database extends DatabasePromise {
|
||||
protocolVersion: SyncEngineProtocolVersion.V1,
|
||||
longPollTimeoutMs: opts.longPollTimeoutMs,
|
||||
tracing: opts.tracing,
|
||||
bootstrapIfEmpty: typeof opts.url != "function" || opts.url() != null,
|
||||
remoteEncryption: opts.remoteEncryption?.cipher,
|
||||
});
|
||||
super(engine.db() as unknown as any);
|
||||
|
||||
|
||||
let headers = typeof opts.authToken === "function" ? () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
}) : {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
};
|
||||
let headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>);
|
||||
if (typeof opts.authToken == "function") {
|
||||
const authToken = opts.authToken;
|
||||
headers = async () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${await authToken()}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
const authToken = opts.authToken;
|
||||
headers = {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${authToken}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
};
|
||||
}
|
||||
this.#runOpts = {
|
||||
url: opts.url,
|
||||
headers: headers,
|
||||
@@ -83,18 +97,23 @@ class Database extends DatabasePromise {
|
||||
* connect database and initialize it in case of clean start
|
||||
*/
|
||||
override async connect() {
|
||||
if (this.connected) { return; }
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.connected) {
|
||||
return;
|
||||
} else if (this.#engine == null) {
|
||||
await super.connect();
|
||||
} else {
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
this.connected = true;
|
||||
}
|
||||
/**
|
||||
@@ -103,6 +122,9 @@ class Database extends DatabasePromise {
|
||||
* @returns true if new changes were pulled from the remote
|
||||
*/
|
||||
async pull() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
|
||||
if (changes.empty()) {
|
||||
return false;
|
||||
@@ -115,35 +137,46 @@ class Database extends DatabasePromise {
|
||||
* if {@link DatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
|
||||
*/
|
||||
async push() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
|
||||
}
|
||||
/**
|
||||
* checkpoint WAL for local database
|
||||
*/
|
||||
async checkpoint() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
|
||||
}
|
||||
/**
|
||||
* @returns statistic of current local database
|
||||
*/
|
||||
async stats(): Promise<DatabaseStats> {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
|
||||
}
|
||||
/**
|
||||
* close the database and relevant files
|
||||
*/
|
||||
async close() {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.#engine != null) {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
this.#engine.close();
|
||||
}
|
||||
await super.close();
|
||||
this.#engine.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common"
|
||||
import { DatabasePromise } from "@tursodatabase/database-common"
|
||||
import { ProtocolIo, run, DatabaseOpts, EncryptionOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker } from "./index-default.js";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker, Database as NativeDatabase } from "./index-default.js";
|
||||
|
||||
let BrowserIO: ProtocolIo = {
|
||||
async read(path: string): Promise<Buffer | Uint8Array | null> {
|
||||
@@ -45,6 +45,12 @@ class Database extends DatabasePromise {
|
||||
#guards: SyncEngineGuards;
|
||||
#worker: Worker | null;
|
||||
constructor(opts: DatabaseOpts) {
|
||||
if (opts.url == null) {
|
||||
super(new NativeDatabase(opts.path, { tracing: opts.tracing }) as any);
|
||||
this.#engine = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = new SyncEngine({
|
||||
path: opts.path,
|
||||
clientName: opts.clientName,
|
||||
@@ -52,23 +58,31 @@ class Database extends DatabasePromise {
|
||||
protocolVersion: SyncEngineProtocolVersion.V1,
|
||||
longPollTimeoutMs: opts.longPollTimeoutMs,
|
||||
tracing: opts.tracing,
|
||||
bootstrapIfEmpty: typeof opts.url != "function" || opts.url() != null,
|
||||
remoteEncryption: opts.remoteEncryption?.cipher,
|
||||
});
|
||||
super(engine.db() as unknown as any);
|
||||
|
||||
|
||||
let headers = typeof opts.authToken === "function" ? () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
}) : {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
};
|
||||
let headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>);
|
||||
if (typeof opts.authToken == "function") {
|
||||
const authToken = opts.authToken;
|
||||
headers = async () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${await authToken()}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
const authToken = opts.authToken;
|
||||
headers = {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${authToken}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
};
|
||||
}
|
||||
this.#runOpts = {
|
||||
url: opts.url,
|
||||
headers: headers,
|
||||
@@ -83,18 +97,23 @@ class Database extends DatabasePromise {
|
||||
* connect database and initialize it in case of clean start
|
||||
*/
|
||||
override async connect() {
|
||||
if (this.connected) { return; }
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.connected) {
|
||||
return;
|
||||
} else if (this.#engine == null) {
|
||||
await super.connect();
|
||||
} else {
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
this.connected = true;
|
||||
}
|
||||
/**
|
||||
@@ -103,6 +122,9 @@ class Database extends DatabasePromise {
|
||||
* @returns true if new changes were pulled from the remote
|
||||
*/
|
||||
async pull() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
|
||||
if (changes.empty()) {
|
||||
return false;
|
||||
@@ -115,35 +137,46 @@ class Database extends DatabasePromise {
|
||||
* if {@link DatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
|
||||
*/
|
||||
async push() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
|
||||
}
|
||||
/**
|
||||
* checkpoint WAL for local database
|
||||
*/
|
||||
async checkpoint() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
|
||||
}
|
||||
/**
|
||||
* @returns statistic of current local database
|
||||
*/
|
||||
async stats(): Promise<DatabaseStats> {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
|
||||
}
|
||||
/**
|
||||
* close the database and relevant files
|
||||
*/
|
||||
async close() {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.#engine != null) {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
this.#engine.close();
|
||||
}
|
||||
await super.close();
|
||||
this.#engine.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common"
|
||||
import { DatabasePromise } from "@tursodatabase/database-common"
|
||||
import { ProtocolIo, run, DatabaseOpts, EncryptionOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker } from "./index-turbopack-hack.js";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker, Database as NativeDatabase } from "./index-turbopack-hack.js";
|
||||
|
||||
let BrowserIO: ProtocolIo = {
|
||||
async read(path: string): Promise<Buffer | Uint8Array | null> {
|
||||
@@ -45,6 +45,12 @@ class Database extends DatabasePromise {
|
||||
#guards: SyncEngineGuards;
|
||||
#worker: Worker | null;
|
||||
constructor(opts: DatabaseOpts) {
|
||||
if (opts.url == null) {
|
||||
super(new NativeDatabase(opts.path, { tracing: opts.tracing }) as any);
|
||||
this.#engine = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = new SyncEngine({
|
||||
path: opts.path,
|
||||
clientName: opts.clientName,
|
||||
@@ -52,23 +58,31 @@ class Database extends DatabasePromise {
|
||||
protocolVersion: SyncEngineProtocolVersion.V1,
|
||||
longPollTimeoutMs: opts.longPollTimeoutMs,
|
||||
tracing: opts.tracing,
|
||||
bootstrapIfEmpty: typeof opts.url != "function" || opts.url() != null,
|
||||
remoteEncryption: opts.remoteEncryption?.cipher,
|
||||
});
|
||||
super(engine.db() as unknown as any);
|
||||
|
||||
|
||||
let headers = typeof opts.authToken === "function" ? () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
}) : {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
};
|
||||
let headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>);
|
||||
if (typeof opts.authToken == "function") {
|
||||
const authToken = opts.authToken;
|
||||
headers = async () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${await authToken()}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
const authToken = opts.authToken;
|
||||
headers = {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${authToken}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
};
|
||||
}
|
||||
this.#runOpts = {
|
||||
url: opts.url,
|
||||
headers: headers,
|
||||
@@ -83,18 +97,23 @@ class Database extends DatabasePromise {
|
||||
* connect database and initialize it in case of clean start
|
||||
*/
|
||||
override async connect() {
|
||||
if (this.connected) { return; }
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.connected) {
|
||||
return;
|
||||
} else if (this.#engine == null) {
|
||||
await super.connect();
|
||||
} else {
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
this.connected = true;
|
||||
}
|
||||
/**
|
||||
@@ -103,6 +122,9 @@ class Database extends DatabasePromise {
|
||||
* @returns true if new changes were pulled from the remote
|
||||
*/
|
||||
async pull() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
|
||||
if (changes.empty()) {
|
||||
return false;
|
||||
@@ -115,35 +137,46 @@ class Database extends DatabasePromise {
|
||||
* if {@link DatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
|
||||
*/
|
||||
async push() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
|
||||
}
|
||||
/**
|
||||
* checkpoint WAL for local database
|
||||
*/
|
||||
async checkpoint() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
|
||||
}
|
||||
/**
|
||||
* @returns statistic of current local database
|
||||
*/
|
||||
async stats(): Promise<DatabaseStats> {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
|
||||
}
|
||||
/**
|
||||
* close the database and relevant files
|
||||
*/
|
||||
async close() {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.#engine != null) {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
this.#engine.close();
|
||||
}
|
||||
await super.close();
|
||||
this.#engine.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { registerFileAtWorker, unregisterFileAtWorker } from "@tursodatabase/database-wasm-common"
|
||||
import { DatabasePromise } from "@tursodatabase/database-common"
|
||||
import { ProtocolIo, run, DatabaseOpts, EncryptionOpts, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, SyncEngineGuards } from "@tursodatabase/sync-common";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker } from "./index-vite-dev-hack.js";
|
||||
import { SyncEngine, SyncEngineProtocolVersion, initThreadPool, MainWorker, Database as NativeDatabase } from "./index-vite-dev-hack.js";
|
||||
|
||||
let BrowserIO: ProtocolIo = {
|
||||
async read(path: string): Promise<Buffer | Uint8Array | null> {
|
||||
@@ -45,6 +45,12 @@ class Database extends DatabasePromise {
|
||||
#guards: SyncEngineGuards;
|
||||
#worker: Worker | null;
|
||||
constructor(opts: DatabaseOpts) {
|
||||
if (opts.url == null) {
|
||||
super(new NativeDatabase(opts.path, { tracing: opts.tracing }) as any);
|
||||
this.#engine = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = new SyncEngine({
|
||||
path: opts.path,
|
||||
clientName: opts.clientName,
|
||||
@@ -52,23 +58,31 @@ class Database extends DatabasePromise {
|
||||
protocolVersion: SyncEngineProtocolVersion.V1,
|
||||
longPollTimeoutMs: opts.longPollTimeoutMs,
|
||||
tracing: opts.tracing,
|
||||
bootstrapIfEmpty: typeof opts.url != "function" || opts.url() != null,
|
||||
remoteEncryption: opts.remoteEncryption?.cipher,
|
||||
});
|
||||
super(engine.db() as unknown as any);
|
||||
|
||||
|
||||
let headers = typeof opts.authToken === "function" ? () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${(opts.authToken as any)()}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
}) : {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${opts.authToken}` }),
|
||||
...(opts.encryption != null && {
|
||||
"x-turso-encryption-key": opts.encryption.key,
|
||||
"x-turso-encryption-cipher": opts.encryption.cipher,
|
||||
})
|
||||
};
|
||||
let headers: { [K: string]: string } | (() => Promise<{ [K: string]: string }>);
|
||||
if (typeof opts.authToken == "function") {
|
||||
const authToken = opts.authToken;
|
||||
headers = async () => ({
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${await authToken()}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
const authToken = opts.authToken;
|
||||
headers = {
|
||||
...(opts.authToken != null && { "Authorization": `Bearer ${authToken}` }),
|
||||
...(opts.remoteEncryption != null && {
|
||||
"x-turso-encryption-key": opts.remoteEncryption.key,
|
||||
"x-turso-encryption-cipher": opts.remoteEncryption.cipher,
|
||||
})
|
||||
};
|
||||
}
|
||||
this.#runOpts = {
|
||||
url: opts.url,
|
||||
headers: headers,
|
||||
@@ -83,18 +97,23 @@ class Database extends DatabasePromise {
|
||||
* connect database and initialize it in case of clean start
|
||||
*/
|
||||
override async connect() {
|
||||
if (this.connected) { return; }
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.connected) {
|
||||
return;
|
||||
} else if (this.#engine == null) {
|
||||
await super.connect();
|
||||
} else {
|
||||
if (!this.memory) {
|
||||
this.#worker = await init();
|
||||
await Promise.all([
|
||||
registerFileAtWorker(this.#worker, this.name),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
registerFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
}
|
||||
await run(this.#runOpts, this.#io, this.#engine, this.#engine.connect());
|
||||
this.connected = true;
|
||||
}
|
||||
/**
|
||||
@@ -103,6 +122,9 @@ class Database extends DatabasePromise {
|
||||
* @returns true if new changes were pulled from the remote
|
||||
*/
|
||||
async pull() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
const changes = await this.#guards.wait(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.wait()));
|
||||
if (changes.empty()) {
|
||||
return false;
|
||||
@@ -115,35 +137,46 @@ class Database extends DatabasePromise {
|
||||
* if {@link DatabaseOpts.transform} is set - then provided callback will be called for every mutation before sending it to the remote
|
||||
*/
|
||||
async push() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.push(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.push()));
|
||||
}
|
||||
/**
|
||||
* checkpoint WAL for local database
|
||||
*/
|
||||
async checkpoint() {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
await this.#guards.checkpoint(async () => await run(this.#runOpts, this.#io, this.#engine, this.#engine.checkpoint()));
|
||||
}
|
||||
/**
|
||||
* @returns statistic of current local database
|
||||
*/
|
||||
async stats(): Promise<DatabaseStats> {
|
||||
if (this.#engine == null) {
|
||||
throw new Error("sync is disabled as database was opened without sync support")
|
||||
}
|
||||
return (await run(this.#runOpts, this.#io, this.#engine, this.#engine.stats()));
|
||||
}
|
||||
/**
|
||||
* close the database and relevant files
|
||||
*/
|
||||
async close() {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
if (this.#engine != null) {
|
||||
if (this.name != null && this.#worker != null) {
|
||||
await Promise.all([
|
||||
unregisterFileAtWorker(this.#worker, this.name),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-wal-revert`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-info`),
|
||||
unregisterFileAtWorker(this.#worker, `${this.name}-changes`),
|
||||
]);
|
||||
}
|
||||
this.#engine.close();
|
||||
}
|
||||
await super.close();
|
||||
this.#engine.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { expect, test } from 'vitest'
|
||||
import { Database, connect, DatabaseRowMutation, DatabaseRowTransformResult } from './promise-default.js'
|
||||
|
||||
const localeCompare = (a, b) => a.x.localeCompare(b.x);
|
||||
const intCompare = (a, b) => a.x - b.x;
|
||||
|
||||
test('implicit connect', async () => {
|
||||
const db = new Database({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
@@ -11,6 +12,91 @@ test('implicit connect', async () => {
|
||||
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
|
||||
})
|
||||
|
||||
test('simple-db', async () => {
|
||||
const db = new Database({ path: ':memory:' });
|
||||
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }])
|
||||
await db.exec("CREATE TABLE t(x)");
|
||||
await db.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }])
|
||||
await expect(async () => await db.pull()).rejects.toThrowError(/sync is disabled as database was opened without sync support/);
|
||||
})
|
||||
|
||||
test('implicit connect', async () => {
|
||||
const db = new Database({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
const defer = db.prepare("SELECT * FROM not_found");
|
||||
await expect(async () => await defer.all()).rejects.toThrowError(/no such table: not_found/);
|
||||
expect(() => db.prepare("SELECT * FROM not_found")).toThrowError(/no such table: not_found/);
|
||||
expect(await db.prepare("SELECT 1 as x").all()).toEqual([{ x: 1 }]);
|
||||
})
|
||||
|
||||
test('defered sync', async () => {
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.exec("INSERT INTO t VALUES (100)");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
|
||||
let url = null;
|
||||
const db = new Database({ path: ':memory:', url: () => url });
|
||||
await db.prepare("CREATE TABLE t(x)").run();
|
||||
await db.prepare("INSERT INTO t VALUES (1), (2), (3), (42)").run();
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 42 }]);
|
||||
await expect(async () => await db.pull()).rejects.toThrow(/url is empty - sync is paused/);
|
||||
url = process.env.VITE_TURSO_DB_URL;
|
||||
await db.pull();
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 100 }, { x: 1 }, { x: 2 }, { x: 3 }, { x: 42 }]);
|
||||
})
|
||||
|
||||
test('encryption sync', async () => {
|
||||
const KEY = 'l/FWopMfZisTLgBX4A42AergrCrYKjiO3BfkJUwv83I=';
|
||||
const URL = 'http://encrypted--a--a.localhost:10000';
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
const db1 = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
const db2 = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db1.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
await db2.exec("INSERT INTO t VALUES (4), (5), (6)");
|
||||
expect(await db1.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }]);
|
||||
expect(await db2.prepare("SELECT * FROM t").all()).toEqual([{ x: 4 }, { x: 5 }, { x: 6 }]);
|
||||
await Promise.all([db1.push(), db2.push()]);
|
||||
await Promise.all([db1.pull(), db2.pull()]);
|
||||
const expected = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }, { x: 6 }];
|
||||
expect((await db1.prepare("SELECT * FROM t").all()).sort(intCompare)).toEqual(expected.sort(intCompare));
|
||||
expect((await db2.prepare("SELECT * FROM t").all()).sort(intCompare)).toEqual(expected.sort(intCompare));
|
||||
});
|
||||
|
||||
test('defered encryption sync', async () => {
|
||||
const URL = 'http://encrypted--a--a.localhost:10000';
|
||||
const KEY = 'l/FWopMfZisTLgBX4A42AergrCrYKjiO3BfkJUwv83I=';
|
||||
let url = null;
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: URL, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("DELETE FROM t");
|
||||
await db.exec("INSERT INTO t VALUES (100)");
|
||||
await db.push();
|
||||
await db.close();
|
||||
}
|
||||
const db = await connect({ path: ':memory:', url: () => url, remoteEncryption: { key: KEY, cipher: 'aes256gcm' } });
|
||||
await db.exec("CREATE TABLE IF NOT EXISTS t(x)");
|
||||
await db.exec("INSERT INTO t VALUES (1), (2), (3)");
|
||||
expect(await db.prepare("SELECT * FROM t").all()).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }]);
|
||||
|
||||
url = URL;
|
||||
await db.pull();
|
||||
|
||||
const expected = [{ x: 100 }, { x: 1 }, { x: 2 }, { x: 3 }];
|
||||
expect((await db.prepare("SELECT * FROM t").all())).toEqual(expected);
|
||||
});
|
||||
|
||||
test('select-after-push', async () => {
|
||||
{
|
||||
const db = await connect({ path: ':memory:', url: process.env.VITE_TURSO_DB_URL });
|
||||
|
||||
Reference in New Issue
Block a user