@tursodatabase/sync-common 0.2.0-pre.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +14 -6
- package/dist/types.d.ts +124 -32
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
This package is the Turso Sync common JS library which is shared between final builds for Node and Browser.
|
|
4
4
|
|
|
5
|
-
Do not use this package directly - instead you must use `@tursodatabase/sync` or `@tursodatabase/sync-
|
|
5
|
+
Do not use this package directly - instead you must use `@tursodatabase/sync` or `@tursodatabase/sync-wasm`.
|
|
6
6
|
|
|
7
|
-
> **⚠️ Warning:** This software is
|
|
7
|
+
> **⚠️ Warning:** This software is in BETA. It may still contain bugs and unexpected behavior. Use caution with production data and ensure you have backups.
|
|
8
8
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { run, memoryIO, SyncEngineGuards } from "./run.js";
|
|
2
|
-
import {
|
|
2
|
+
import { DatabaseOpts, ProtocolIo, RunOpts, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, DatabaseStats, DatabaseChangeType, EncryptionOpts } from "./types.js";
|
|
3
3
|
export { run, memoryIO, SyncEngineGuards };
|
|
4
|
-
export type {
|
|
4
|
+
export type { DatabaseStats, DatabaseOpts, DatabaseChangeType, DatabaseRowMutation, DatabaseRowStatement, DatabaseRowTransformResult, EncryptionOpts, ProtocolIo, RunOpts, };
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAC1D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,aAAa,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAExL,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAA;AAC1C,YAAY,EACR,aAAa,EACb,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,0BAA0B,EAC1B,cAAc,EAEd,UAAU,EACV,OAAO,GACV,CAAA"}
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../run.ts"],"names":[],"mappings":"AAEA,OAAO,EAAqB,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../run.ts"],"names":[],"mappings":"AAEA,OAAO,EAAqB,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAkG3D,wBAAgB,QAAQ,IAAI,UAAU,CAUrC;AAGD,wBAAsB,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAuBlG;AAED,qBAAa,gBAAgB;IACzB,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,SAAS,CAAC;IACpB,cAAc,EAAE,SAAS,CAAC;;IAOpB,IAAI,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC;IAQzC,IAAI,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAY3B,KAAK,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAc5B,UAAU,CAAC,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;CAc1C"}
|
package/dist/run.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { AsyncLock } from "@tursodatabase/database-common";
|
|
3
|
-
const GENERATOR_RESUME_IO = 0;
|
|
4
|
-
const GENERATOR_RESUME_DONE = 1;
|
|
5
3
|
function trackPromise(p) {
|
|
6
4
|
let status = { promise: null, finished: false };
|
|
7
5
|
status.promise = p.finally(() => status.finished = true);
|
|
@@ -14,15 +12,26 @@ async function process(opts, io, request) {
|
|
|
14
12
|
const requestType = request.request();
|
|
15
13
|
const completion = request.completion();
|
|
16
14
|
if (requestType.type == 'Http') {
|
|
15
|
+
let url = null;
|
|
16
|
+
if (typeof opts.url == "function") {
|
|
17
|
+
url = opts.url();
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
url = opts.url;
|
|
21
|
+
}
|
|
22
|
+
if (url == null) {
|
|
23
|
+
completion.poison(`url is empty - sync is paused`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
17
26
|
try {
|
|
18
|
-
let headers = opts.headers;
|
|
27
|
+
let headers = typeof opts.headers === "function" ? await opts.headers() : opts.headers;
|
|
19
28
|
if (requestType.headers != null && requestType.headers.length > 0) {
|
|
20
|
-
headers = { ...
|
|
29
|
+
headers = { ...headers };
|
|
21
30
|
for (let header of requestType.headers) {
|
|
22
31
|
headers[header[0]] = header[1];
|
|
23
32
|
}
|
|
24
33
|
}
|
|
25
|
-
const response = await fetch(`${
|
|
34
|
+
const response = await fetch(`${url}${requestType.path}`, {
|
|
26
35
|
method: requestType.method,
|
|
27
36
|
headers: headers,
|
|
28
37
|
body: requestType.body != null ? new Uint8Array(requestType.body) : null,
|
|
@@ -122,7 +131,6 @@ export async function run(opts, io, engine, generator) {
|
|
|
122
131
|
await Promise.all([engine.ioLoopAsync(), tasksRace]);
|
|
123
132
|
tasks = tasks.filter(t => !t.finished);
|
|
124
133
|
}
|
|
125
|
-
return generator.take();
|
|
126
134
|
}
|
|
127
135
|
export class SyncEngineGuards {
|
|
128
136
|
waitLock;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,66 +1,158 @@
|
|
|
1
1
|
export declare const enum DatabaseChangeType {
|
|
2
|
-
Insert =
|
|
3
|
-
Update =
|
|
4
|
-
Delete =
|
|
2
|
+
Insert = "insert",
|
|
3
|
+
Update = "update",
|
|
4
|
+
Delete = "delete"
|
|
5
5
|
}
|
|
6
|
+
export interface DatabaseRowStatement {
|
|
7
|
+
/**
|
|
8
|
+
* SQL statements with positional placeholders (?)
|
|
9
|
+
*/
|
|
10
|
+
sql: string;
|
|
11
|
+
/**
|
|
12
|
+
* values to substitute placeholders
|
|
13
|
+
*/
|
|
14
|
+
values: Array<any>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* transformation result:
|
|
18
|
+
* - skip: ignore the mutation completely and do not apply it
|
|
19
|
+
* - rewrite: replace mutation with the provided statement
|
|
20
|
+
* - null: do not change mutation and keep it as is
|
|
21
|
+
*/
|
|
22
|
+
export type DatabaseRowTransformResult = {
|
|
23
|
+
operation: 'skip';
|
|
24
|
+
} | {
|
|
25
|
+
operation: 'rewrite';
|
|
26
|
+
stmt: DatabaseRowStatement;
|
|
27
|
+
} | null;
|
|
6
28
|
export interface DatabaseRowMutation {
|
|
29
|
+
/**
|
|
30
|
+
* unix seconds timestamp of the change
|
|
31
|
+
*/
|
|
7
32
|
changeTime: number;
|
|
33
|
+
/**
|
|
34
|
+
* table name of the change
|
|
35
|
+
*/
|
|
8
36
|
tableName: string;
|
|
37
|
+
/**
|
|
38
|
+
* rowid of the change
|
|
39
|
+
*/
|
|
9
40
|
id: number;
|
|
41
|
+
/**
|
|
42
|
+
* type of the change (insert/delete/update)
|
|
43
|
+
*/
|
|
10
44
|
changeType: DatabaseChangeType;
|
|
45
|
+
/**
|
|
46
|
+
* columns of the row before the change
|
|
47
|
+
*/
|
|
11
48
|
before?: Record<string, any>;
|
|
49
|
+
/**
|
|
50
|
+
* columns of the row after the change
|
|
51
|
+
*/
|
|
12
52
|
after?: Record<string, any>;
|
|
53
|
+
/**
|
|
54
|
+
* only updated columns of the row after the change
|
|
55
|
+
*/
|
|
13
56
|
updates?: Record<string, any>;
|
|
14
57
|
}
|
|
15
|
-
export type DatabaseRowTransformResult = {
|
|
16
|
-
operation: 'skip';
|
|
17
|
-
} | {
|
|
18
|
-
operation: 'rewrite';
|
|
19
|
-
stmt: DatabaseRowStatement;
|
|
20
|
-
} | null;
|
|
21
58
|
export type Transform = (arg: DatabaseRowMutation) => DatabaseRowTransformResult;
|
|
22
|
-
export interface
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
headers: {
|
|
26
|
-
[K: string]: string;
|
|
27
|
-
};
|
|
28
|
-
transform?: Transform;
|
|
29
|
-
}
|
|
30
|
-
export interface ProtocolIo {
|
|
31
|
-
read(path: string): Promise<Buffer | Uint8Array | null>;
|
|
32
|
-
write(path: string, content: Buffer | Uint8Array): Promise<void>;
|
|
59
|
+
export interface EncryptionOpts {
|
|
60
|
+
key: string;
|
|
61
|
+
cipher: 'aes256gcm' | 'aes128gcm' | 'chacha20poly1305' | 'aegis256';
|
|
33
62
|
}
|
|
34
|
-
export interface
|
|
63
|
+
export interface DatabaseOpts {
|
|
64
|
+
/**
|
|
65
|
+
* local path where to store all synced database files (e.g. local.db)
|
|
66
|
+
* note, that synced database will write several files with that prefix
|
|
67
|
+
* (e.g. local.db-info, local.db-wal, etc)
|
|
68
|
+
* */
|
|
35
69
|
path: string;
|
|
70
|
+
/**
|
|
71
|
+
* optional url of the remote database (e.g. libsql://db-org.turso.io)
|
|
72
|
+
* (if omitted - local-only database will be created)
|
|
73
|
+
*
|
|
74
|
+
* you can also promide function which will return URL or null
|
|
75
|
+
* in this case local database will be created and sync will be "switched-on" whenever the url will return non-empty value
|
|
76
|
+
* note, that all other parameters (like encryption) must be set in advance in order for the "deferred" sync to work properly
|
|
77
|
+
*/
|
|
78
|
+
url?: string | (() => string | null);
|
|
79
|
+
/**
|
|
80
|
+
* auth token for the remote database
|
|
81
|
+
* (can be either static string or function which will provide short-lived credentials for every new request)
|
|
82
|
+
*/
|
|
83
|
+
authToken?: string | (() => Promise<string>);
|
|
84
|
+
/**
|
|
85
|
+
* arbitrary client name which can be used to distinguish clients internally
|
|
86
|
+
* the library will gurantee uniquiness of the clientId by appending unique suffix to the clientName
|
|
87
|
+
*/
|
|
36
88
|
clientName?: string;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
89
|
+
/**
|
|
90
|
+
* optional remote encryption parameters if cloud database were encrypted by default
|
|
91
|
+
*/
|
|
92
|
+
remoteEncryption?: EncryptionOpts;
|
|
93
|
+
/**
|
|
94
|
+
* optional callback which will be called for every mutation before sending it to the remote
|
|
95
|
+
* this callback can transform the update in order to support complex conflict resolution strategy
|
|
96
|
+
*/
|
|
41
97
|
transform?: Transform;
|
|
98
|
+
/**
|
|
99
|
+
* optional long-polling timeout for pull operation
|
|
100
|
+
* if not set - no timeout is applied
|
|
101
|
+
*/
|
|
42
102
|
longPollTimeoutMs?: number;
|
|
43
|
-
|
|
103
|
+
/**
|
|
104
|
+
* optional parameter to enable internal logging for the database
|
|
105
|
+
*/
|
|
106
|
+
tracing?: 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
|
44
107
|
}
|
|
45
|
-
export interface
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
export interface SyncEngineStats {
|
|
108
|
+
export interface DatabaseStats {
|
|
109
|
+
/**
|
|
110
|
+
* amount of local changes not sent to the remote
|
|
111
|
+
*/
|
|
50
112
|
operations: number;
|
|
113
|
+
/**
|
|
114
|
+
* size of the main WAL file in bytes
|
|
115
|
+
*/
|
|
51
116
|
mainWal: number;
|
|
117
|
+
/**
|
|
118
|
+
* size of the revert WAL file in bytes
|
|
119
|
+
*/
|
|
52
120
|
revertWal: number;
|
|
121
|
+
/**
|
|
122
|
+
* unix timestamp of last successful pull time
|
|
123
|
+
*/
|
|
53
124
|
lastPullUnixTime: number;
|
|
125
|
+
/**
|
|
126
|
+
* unix timestamp of last successful push time
|
|
127
|
+
*/
|
|
54
128
|
lastPushUnixTime: number | null;
|
|
129
|
+
/**
|
|
130
|
+
* opaque revision of the changes pulled locally from remote
|
|
131
|
+
* (can be used as e-tag, but string must not be interpreted in any way and must be used as opaque value)
|
|
132
|
+
*/
|
|
55
133
|
revision: string | null;
|
|
56
134
|
}
|
|
135
|
+
export interface RunOpts {
|
|
136
|
+
preemptionMs: number;
|
|
137
|
+
url: string | (() => string | null);
|
|
138
|
+
headers: {
|
|
139
|
+
[K: string]: string;
|
|
140
|
+
} | (() => Promise<{
|
|
141
|
+
[K: string]: string;
|
|
142
|
+
}>);
|
|
143
|
+
transform?: Transform;
|
|
144
|
+
}
|
|
145
|
+
export interface ProtocolIo {
|
|
146
|
+
read(path: string): Promise<Buffer | Uint8Array | null>;
|
|
147
|
+
write(path: string, content: Buffer | Uint8Array): Promise<void>;
|
|
148
|
+
}
|
|
57
149
|
export type GeneratorResponse = {
|
|
58
150
|
type: 'IO';
|
|
59
151
|
} | {
|
|
60
152
|
type: 'Done';
|
|
61
153
|
} | ({
|
|
62
154
|
type: 'SyncEngineStats';
|
|
63
|
-
} &
|
|
155
|
+
} & DatabaseStats) | {
|
|
64
156
|
type: 'SyncEngineChanges';
|
|
65
157
|
changes: any;
|
|
66
158
|
};
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,CAAC,KAAK,MAAM,kBAAkB;IACxC,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,CAAC,KAAK,MAAM,kBAAkB;IACxC,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,MAAM,WAAW;CACpB;AAED,MAAM,WAAW,oBAAoB;IACjC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;CACrB;AAGD;;;;;GAKG;AACH,MAAM,MAAM,0BAA0B,GAAG;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,oBAAoB,CAAA;CAAE,GAAG,IAAI,CAAC;AAE7H,MAAM,WAAW,mBAAmB;IAChC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IACV;;OAEG;IACH,UAAU,EAAE,kBAAkB,CAAA;IAC9B;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC5B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC3B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAChC;AAED,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,0BAA0B,CAAC;AAEjF,MAAM,WAAW,cAAc;IAE3B,GAAG,EA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AAG,MAAM,CAAA;KAAE,CAAC,CAAC,CAAA;IAC3E,SAAS,CAAC,EAAE,SAAS,CAAC;CACzB;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,UAAU,GAAG,IAAI,CAAC,CAAC;IACxD,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpE;AACD,MAAM,MAAM,iBAAiB,GAAG;IAAE,IAAI,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,CAAC;IAAE,IAAI,EAAE,iBAAiB,CAAA;CAAE,GAAG,aAAa,CAAC,GAAG;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tursodatabase/sync-common",
|
|
3
|
-
"version": "0.2.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/tursodatabase/turso"
|
|
@@ -23,6 +23,6 @@
|
|
|
23
23
|
"test": "echo 'no tests'"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tursodatabase/database-common": "^0.2.0
|
|
26
|
+
"@tursodatabase/database-common": "^0.2.0"
|
|
27
27
|
}
|
|
28
28
|
}
|