-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathweb-sql-flags.ts
135 lines (115 loc) · 3.64 KB
/
web-sql-flags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { type ILogger, SQLOpenOptions } from '@powersync/common';
/**
* Common settings used when creating SQL connections on web.
*/
export interface WebSQLFlags {
/**
* Broadcast logs from shared workers, such as the shared sync worker,
* to individual tabs. This defaults to true.
*/
broadcastLogs?: boolean;
/**
* SQLite operations are currently not supported in SSR mode.
* A warning will be logged if attempting to use SQLite in SSR.
* Setting this to `true` will disabled the warning above.
*/
disableSSRWarning?: boolean;
/**
* Enables multi tab support
*/
enableMultiTabs?: boolean;
/**
* The SQLite connection is often executed through a web worker
* in order to offload computation. This can be used to manually
* disable the use of web workers in environments where web workers
* might be unstable.
*/
useWebWorker?: boolean;
/**
* Open in SSR placeholder mode. DB operations and Sync operations will be a No-op
*/
ssrMode?: boolean;
}
export type ResolvedWebSQLFlags = Required<WebSQLFlags>;
export interface ResolvedWebSQLOpenOptions extends SQLOpenOptions {
flags: ResolvedWebSQLFlags;
/**
* Where to store SQLite temporary files. Defaults to 'MEMORY'.
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
*/
temporaryStorage: TemporaryStorageOption;
cacheSizeKb: number;
/**
* Encryption key for the database.
* If set, the database will be encrypted using ChaCha20.
*/
encryptionKey?: string;
}
export enum TemporaryStorageOption {
MEMORY = 'memory',
FILESYSTEM = 'file'
}
export const DEFAULT_CACHE_SIZE_KB = 50 * 1024;
/**
* Options for opening a Web SQL connection
*/
export interface WebSQLOpenFactoryOptions extends SQLOpenOptions {
flags?: WebSQLFlags;
/**
* Allows you to override the default wasqlite db worker.
*
* You can either provide a path to the worker script
* or a factory method that returns a worker.
*/
worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);
logger?: ILogger;
/**
* Where to store SQLite temporary files. Defaults to 'MEMORY'.
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
*
* For details, see: https://www.sqlite.org/pragma.html#pragma_temp_store
*/
temporaryStorage?: TemporaryStorageOption;
/**
* Maximum SQLite cache size. Defaults to 50MB.
*
* For details, see: https://www.sqlite.org/pragma.html#pragma_cache_size
*/
cacheSizeKb?: number;
/**
* Encryption key for the database.
* If set, the database will be encrypted using ChaCha20.
*/
encryptionKey?: string;
}
export function isServerSide() {
return typeof window == 'undefined';
}
export const DEFAULT_WEB_SQL_FLAGS: ResolvedWebSQLFlags = {
broadcastLogs: true,
disableSSRWarning: false,
ssrMode: isServerSide(),
/**
* Multiple tabs are by default not supported on Android, iOS and Safari.
* Other platforms will have multiple tabs enabled by default.
*/
enableMultiTabs:
typeof globalThis.navigator !== 'undefined' && // For SSR purposes
typeof SharedWorker !== 'undefined' &&
!navigator.userAgent.match(/(Android|iPhone|iPod|iPad)/i) &&
!(window as any).safari,
useWebWorker: true
};
export function resolveWebSQLFlags(flags?: WebSQLFlags): ResolvedWebSQLFlags {
const resolvedFlags = {
...DEFAULT_WEB_SQL_FLAGS,
...(flags ?? {})
};
if (typeof flags?.enableMultiTabs != 'undefined') {
resolvedFlags.enableMultiTabs = flags.enableMultiTabs;
}
if (flags?.useWebWorker === false) {
resolvedFlags.enableMultiTabs = false;
}
return resolvedFlags;
}