mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-07 10:14: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,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user