@evolu/common 6.0.1-preview.19 → 6.0.1-preview.20
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/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +1 -1
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +1 -1
- package/dist/src/CallbackRegistry.d.ts +53 -0
- package/dist/src/CallbackRegistry.d.ts.map +1 -0
- package/dist/src/CallbackRegistry.js +25 -0
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +48 -37
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +27 -50
- package/dist/src/Evolu/Db.d.ts +138 -66
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +248 -645
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +79 -116
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +275 -132
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +144 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +171 -0
- package/dist/src/Evolu/Owner.d.ts +129 -83
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +80 -89
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +114 -191
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +409 -416
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.js +3 -3
- package/dist/src/Evolu/Relay.d.ts +1 -2
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +11 -9
- package/dist/src/Evolu/Schema.d.ts +88 -27
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +141 -24
- package/dist/src/Evolu/Storage.d.ts +158 -14
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +32 -32
- package/dist/src/Evolu/Sync.d.ts +77 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +453 -20
- package/dist/src/Evolu/Timestamp.d.ts +29 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +20 -18
- package/dist/src/ManyToManyMap.d.ts +74 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/RefCountedResourceManager.d.ts +119 -0
- package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
- package/dist/src/RefCountedResourceManager.js +197 -0
- package/dist/src/Result.d.ts +144 -22
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +5 -2
- package/dist/src/Sqlite.d.ts +20 -4
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +50 -8
- package/dist/src/Task.d.ts +511 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +410 -0
- package/dist/src/Time.d.ts +59 -0
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +87 -4
- package/dist/src/Type.d.ts +431 -341
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +458 -466
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -13
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +2 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -3
- package/package.json +7 -7
- package/src/Assert.ts +2 -4
- package/src/Buffer.ts +1 -1
- package/src/CallbackRegistry.ts +84 -0
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +78 -91
- package/src/Evolu/Db.ts +455 -947
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +545 -307
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +422 -0
- package/src/Evolu/Owner.ts +191 -131
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +536 -653
- package/src/Evolu/Public.ts +7 -9
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Relay.ts +17 -12
- package/src/Evolu/Schema.ts +271 -66
- package/src/Evolu/Storage.ts +263 -55
- package/src/Evolu/Sync.ts +758 -37
- package/src/Evolu/Timestamp.ts +30 -35
- package/src/ManyToManyMap.ts +127 -24
- package/src/Random.ts +3 -2
- package/src/RefCountedResourceManager.ts +368 -0
- package/src/Result.ts +149 -23
- package/src/Sqlite.ts +59 -24
- package/src/Task.ts +779 -0
- package/src/Time.ts +168 -4
- package/src/Type.ts +657 -695
- package/src/WebSocket.ts +23 -17
- package/src/Worker.ts +72 -23
- package/src/index.ts +2 -3
- package/dist/src/Callbacks.d.ts +0 -20
- package/dist/src/Callbacks.d.ts.map +0 -1
- package/dist/src/Callbacks.js +0 -18
- package/dist/src/Evolu/Config.d.ts +0 -82
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Callbacks.ts +0 -43
- package/src/Evolu/Config.ts +0 -97
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
package/dist/src/Result.d.ts
CHANGED
|
@@ -4,14 +4,16 @@
|
|
|
4
4
|
* The problem with throwing an exception in JavaScript is that the caught error
|
|
5
5
|
* is always of an unknown type. The unknown type is a problem because we can't
|
|
6
6
|
* be sure all errors have been handled because the TypeScript compiler can't
|
|
7
|
-
* tell us.
|
|
8
|
-
* approach to error handling, where errors are explicitly represented as part
|
|
9
|
-
* of the return type, such as Result or Either, allowing the developer to
|
|
10
|
-
* handle all errors safely. ✅
|
|
7
|
+
* tell us.
|
|
11
8
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
9
|
+
* Languages like Rust 🦀 or Haskell 📚 use a type-safe approach to error
|
|
10
|
+
* handling, where errors are explicitly represented as part of the return type,
|
|
11
|
+
* such as Result or Either, allowing the developer to handle errors safely.
|
|
12
|
+
* TypeScript can have this too via the `Result` type.
|
|
13
|
+
*
|
|
14
|
+
* The `Result` type can be either {@link Ok} (success) or {@link Err} (error).
|
|
15
|
+
* Use {@link ok} to create a successful result and {@link err} to create an error
|
|
16
|
+
* result.
|
|
15
17
|
*
|
|
16
18
|
* Now let's look at how `Result` can be used for safe JSON parsing:
|
|
17
19
|
*
|
|
@@ -58,12 +60,10 @@
|
|
|
58
60
|
*
|
|
59
61
|
* Let's summarize it:
|
|
60
62
|
*
|
|
61
|
-
* - For
|
|
63
|
+
* - For safe code, use `ok` and `err`.
|
|
62
64
|
* - For unsafe code, use `trySync` or `tryAsync`.
|
|
63
|
-
* - For asynchronous safe code, use `Promise` with {@link Result}.
|
|
64
65
|
*
|
|
65
|
-
* Asynchronous safe (because of a Promise using Result) code
|
|
66
|
-
* straightforward:
|
|
66
|
+
* Asynchronous safe (because of a Promise using Result) code:
|
|
67
67
|
*
|
|
68
68
|
* ```ts
|
|
69
69
|
* const fetchUser = async (
|
|
@@ -84,9 +84,33 @@
|
|
|
84
84
|
* };
|
|
85
85
|
* ```
|
|
86
86
|
*
|
|
87
|
-
*
|
|
87
|
+
* ### Naming Convention
|
|
88
|
+
*
|
|
89
|
+
* - For values: `const user = getUser()`
|
|
90
|
+
* - For void operations: `const result = foo()` (unless it would clash)
|
|
91
|
+
* - For clashes, suffix the name: `const saveResult = save()`
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* const processUser = () => {
|
|
95
|
+
* // we have a value
|
|
96
|
+
* const user = getUser();
|
|
97
|
+
* if (!user.ok) return user;
|
|
98
|
+
*
|
|
99
|
+
* // void operation
|
|
100
|
+
* const result = saveToDatabase(user.value);
|
|
101
|
+
* if (!result.ok) return result;
|
|
102
|
+
*
|
|
103
|
+
* // avoiding clash
|
|
104
|
+
* const deleteFromCacheResult = deleteFromCache();
|
|
105
|
+
* if (!deleteFromCacheResult.ok) return deleteFromCacheResult;
|
|
106
|
+
*
|
|
107
|
+
* return ok();
|
|
108
|
+
* };
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* ### Examples
|
|
88
112
|
*
|
|
89
|
-
*
|
|
113
|
+
* #### Sequential Operations with Short-Circuiting
|
|
90
114
|
*
|
|
91
115
|
* When performing a sequence of operations where any failure should stop
|
|
92
116
|
* further processing, use the `Result` type with early returns.
|
|
@@ -136,30 +160,114 @@
|
|
|
136
160
|
* This approach ensures type-safe error handling, avoids nested try/catch
|
|
137
161
|
* blocks, and clearly communicates the control flow.
|
|
138
162
|
*
|
|
139
|
-
*
|
|
163
|
+
* #### A function with two different errors:
|
|
140
164
|
*
|
|
141
165
|
* ```ts
|
|
142
166
|
* const example = (value: string): Result<number, FooError | BarError> => {
|
|
143
167
|
* const foo = getFoo(value);
|
|
144
168
|
* if (!foo.ok) return foo;
|
|
145
169
|
*
|
|
146
|
-
* const bar =
|
|
170
|
+
* const bar = getBar(foo.value);
|
|
147
171
|
* if (!bar.ok) return bar;
|
|
148
172
|
*
|
|
149
173
|
* return ok(barToNumber(bar.value));
|
|
150
174
|
* };
|
|
151
175
|
* ```
|
|
152
176
|
*
|
|
153
|
-
*
|
|
177
|
+
* ### Handling Unexpected Errors
|
|
178
|
+
*
|
|
179
|
+
* Even with disciplined use of `trySync` and `tryAsync`, unexpected errors can
|
|
180
|
+
* still occur due to programming mistakes, third-party library bugs, or edge
|
|
181
|
+
* cases. These should be logged for debugging, but **unexpected errors are not
|
|
182
|
+
* recoverable** - they represent bugs that must be fixed.
|
|
183
|
+
*
|
|
184
|
+
* **Important**: "Graceful shutdown" and error recovery can only come from
|
|
185
|
+
* expected errors handled via the `Result` type. Unexpected errors should fail
|
|
186
|
+
* fast - the operation fails immediately and the error bubbles up.
|
|
187
|
+
*
|
|
188
|
+
* #### In Browser Environments
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* // Global error handler for unexpected errors
|
|
192
|
+
* window.addEventListener("error", (event) => {
|
|
193
|
+
* console.error("Uncaught error:", event.error);
|
|
194
|
+
* // Send to error reporting service
|
|
195
|
+
* errorReportingService.report(event.error);
|
|
196
|
+
* });
|
|
197
|
+
*
|
|
198
|
+
* // For unhandled promise rejections
|
|
199
|
+
* window.addEventListener("unhandledrejection", (event) => {
|
|
200
|
+
* console.error("Unhandled promise rejection:", event.reason);
|
|
201
|
+
* errorReportingService.report(event.reason);
|
|
202
|
+
* });
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* #### In Node.js Environments
|
|
206
|
+
*
|
|
207
|
+
* ```ts
|
|
208
|
+
* // Handle uncaught exceptions - log and fail fast
|
|
209
|
+
* process.on("uncaughtException", (error) => {
|
|
210
|
+
* console.error("Uncaught exception:", error);
|
|
211
|
+
* errorReportingService.report(error);
|
|
212
|
+
* // Exit immediately - unexpected errors are not recoverable
|
|
213
|
+
* process.exit(1);
|
|
214
|
+
* });
|
|
215
|
+
*
|
|
216
|
+
* // Handle unhandled promise rejections
|
|
217
|
+
* process.on("unhandledRejection", (reason) => {
|
|
218
|
+
* console.error("Unhandled promise rejection:", reason);
|
|
219
|
+
* errorReportingService.report(reason);
|
|
220
|
+
* });
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* These global handlers serve as a safety net to log and report unexpected
|
|
224
|
+
* errors for debugging purposes. They do not attempt recovery - unexpected
|
|
225
|
+
* errors represent bugs that must be fixed. The discipline of explicit error
|
|
226
|
+
* handling through the `Result` pattern remains the primary approach for all
|
|
227
|
+
* recoverable scenarios.
|
|
154
228
|
*
|
|
155
|
-
* ###
|
|
229
|
+
* ### FAQ
|
|
230
|
+
*
|
|
231
|
+
* #### What if my function doesn't return a value on success?
|
|
156
232
|
*
|
|
157
233
|
* If your function performs an operation but doesn't need to return a value on
|
|
158
234
|
* success, you can use `Result<void, E>`. Using `Result<void, E>` is clearer
|
|
159
235
|
* than using `Result<true, E>` or `Result<null, E>` because it communicates
|
|
160
236
|
* that the function doesn't produce a value but can produce errors.
|
|
161
237
|
*
|
|
162
|
-
*
|
|
238
|
+
* #### When can a function return `void` instead of `Result<void, E>`?
|
|
239
|
+
*
|
|
240
|
+
* A function can safely return `void` (instead of `Result<void, E>`) when all
|
|
241
|
+
* unsafe code within it is properly wrapped with `trySync` or `tryAsync`. If
|
|
242
|
+
* developers consistently wrap all potentially throwing operations, then any
|
|
243
|
+
* function returning `void` is guaranteed not to throw and can be called
|
|
244
|
+
* without error handling.
|
|
245
|
+
*
|
|
246
|
+
* ```ts
|
|
247
|
+
* // ✅ Safe to return void - all unsafe code is wrapped
|
|
248
|
+
* const processData = (data: string): void => {
|
|
249
|
+
* const parseResult = trySync(
|
|
250
|
+
* () => JSON.parse(data),
|
|
251
|
+
* (error) => ({ type: "ParseError", message: String(error) }),
|
|
252
|
+
* );
|
|
253
|
+
*
|
|
254
|
+
* if (!parseResult.ok) {
|
|
255
|
+
* logError(parseResult.error); // Handle error appropriately
|
|
256
|
+
* return;
|
|
257
|
+
* }
|
|
258
|
+
*
|
|
259
|
+
* // Continue with safe operations...
|
|
260
|
+
* };
|
|
261
|
+
*
|
|
262
|
+
* // ✅ Can call without try-catch since it returns void
|
|
263
|
+
* processData(jsonString);
|
|
264
|
+
* ```
|
|
265
|
+
*
|
|
266
|
+
* This approach creates a clear contract: functions returning `void` are safe
|
|
267
|
+
* to call, while functions returning `Result<T, E>` require explicit error
|
|
268
|
+
* handling.
|
|
269
|
+
*
|
|
270
|
+
* #### How do I short-circuit processing of an array on the first error?
|
|
163
271
|
*
|
|
164
272
|
* If you want to stop processing as soon as an error occurs (short-circuit),
|
|
165
273
|
* you should produce and check each `Result` inside a loop:
|
|
@@ -175,7 +283,7 @@
|
|
|
175
283
|
* // All queries succeeded
|
|
176
284
|
* ```
|
|
177
285
|
*
|
|
178
|
-
*
|
|
286
|
+
* #### How do I handle an array of operations and short-circuit on the first error?
|
|
179
287
|
*
|
|
180
288
|
* If you have an array of operations (not results), you should make them
|
|
181
289
|
* _lazy_—that is, represent each operation as a function. This way, you only
|
|
@@ -212,8 +320,7 @@
|
|
|
212
320
|
* above) over monadic helpers. Imperative code is generally more readable,
|
|
213
321
|
* easier to debug, and more familiar to most JavaScript and TypeScript
|
|
214
322
|
* developers. While monads and functional helpers can be powerful, they often
|
|
215
|
-
* obscure control flow and make debugging harder.
|
|
216
|
-
* handling explicit and straightforward.
|
|
323
|
+
* obscure control flow and make debugging harder.
|
|
217
324
|
*/
|
|
218
325
|
export type Result<T, E> = Ok<T> | Err<E>;
|
|
219
326
|
/** A successful {@link Result}. */
|
|
@@ -269,6 +376,18 @@ export interface Err<E> {
|
|
|
269
376
|
readonly ok: false;
|
|
270
377
|
readonly error: E;
|
|
271
378
|
}
|
|
379
|
+
/**
|
|
380
|
+
* Extracts the value type from a {@link Result}.
|
|
381
|
+
*
|
|
382
|
+
* @category Utilities
|
|
383
|
+
*/
|
|
384
|
+
export type InferOk<R extends Result<any, any>> = R extends Ok<infer T> ? T : never;
|
|
385
|
+
/**
|
|
386
|
+
* Extracts the error type from a {@link Result}.
|
|
387
|
+
*
|
|
388
|
+
* @category Utilities
|
|
389
|
+
*/
|
|
390
|
+
export type InferErr<R extends Result<any, any>> = R extends Err<infer E> ? E : never;
|
|
272
391
|
/**
|
|
273
392
|
* Creates an {@link Ok} result.
|
|
274
393
|
*
|
|
@@ -342,7 +461,10 @@ export declare const getOrThrow: <T, E>(result: Result<T, E>) => T;
|
|
|
342
461
|
* const parseJson = (value: string): Result<unknown, ParseJsonError> =>
|
|
343
462
|
* trySync(
|
|
344
463
|
* () => JSON.parse(value) as unknown,
|
|
345
|
-
* (error)
|
|
464
|
+
* (error): ParseJsonError => ({
|
|
465
|
+
* type: "ParseJsonError",
|
|
466
|
+
* message: String(error),
|
|
467
|
+
* }),
|
|
346
468
|
* );
|
|
347
469
|
* ```
|
|
348
470
|
*/
|
package/dist/src/Result.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Result.d.ts","sourceRoot":"","sources":["../../src/Result.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Result.d.ts","sourceRoot":"","sources":["../../src/Result.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,EAAE,QAAQ,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,CAMvD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,CAAC,EAC1B,IAAI,MAAM,CAAC,EACX,UAAU,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,KAC9B,MAAM,CAAC,CAAC,EAAE,CAAC,CAMb,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAAE,CAAC,EACjC,WAAW,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,UAAU,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,KAC9B,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAIpB,CAAC"}
|
package/dist/src/Result.js
CHANGED
|
@@ -38,7 +38,7 @@ export const getOrThrow = (result) => {
|
|
|
38
38
|
return result.value;
|
|
39
39
|
}
|
|
40
40
|
else {
|
|
41
|
-
throw new Error("getOrThrow
|
|
41
|
+
throw new Error("getOrThrow", { cause: result.error });
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
@@ -61,7 +61,10 @@ export const getOrThrow = (result) => {
|
|
|
61
61
|
* const parseJson = (value: string): Result<unknown, ParseJsonError> =>
|
|
62
62
|
* trySync(
|
|
63
63
|
* () => JSON.parse(value) as unknown,
|
|
64
|
-
* (error)
|
|
64
|
+
* (error): ParseJsonError => ({
|
|
65
|
+
* type: "ParseJsonError",
|
|
66
|
+
* message: String(error),
|
|
67
|
+
* }),
|
|
65
68
|
* );
|
|
66
69
|
* ```
|
|
67
70
|
*/
|
package/dist/src/Sqlite.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { Brand } from "./Brand.js";
|
|
1
2
|
import { ConsoleDep } from "./Console.js";
|
|
3
|
+
import { EncryptionKey } from "./Crypto.js";
|
|
2
4
|
import { TransferableError } from "./Error.js";
|
|
3
5
|
import { Result } from "./Result.js";
|
|
4
6
|
import { SimpleName } from "./Type.js";
|
|
5
7
|
import { Predicate } from "./Types.js";
|
|
6
|
-
import { Brand } from "./Brand.js";
|
|
7
8
|
/**
|
|
8
9
|
* SQLite driver interface. This is the minimal interface that platform-specific
|
|
9
10
|
* drivers must implement.
|
|
@@ -18,6 +19,7 @@ export interface CreateSqliteDriverDep {
|
|
|
18
19
|
}
|
|
19
20
|
export interface SqliteDriverOptions {
|
|
20
21
|
memory?: boolean;
|
|
22
|
+
encryptionKey?: EncryptionKey | undefined;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* Cross-platform SQLite abstraction.
|
|
@@ -125,11 +127,25 @@ export interface SqliteQueryPlanRow {
|
|
|
125
127
|
export declare const explainSqliteQueryPlan: (deps: SqliteDep) => (query: SqliteQuery) => Result<void, SqliteError>;
|
|
126
128
|
/**
|
|
127
129
|
* SQLite represents boolean values using `0` (false) and `1` (true) instead of
|
|
128
|
-
* a dedicated boolean type.
|
|
129
|
-
*
|
|
130
|
+
* a dedicated boolean type.
|
|
131
|
+
*
|
|
132
|
+
* Use {@link sqliteTrue} and {@link sqliteFalse} constants for better
|
|
133
|
+
* readability.
|
|
130
134
|
*
|
|
131
135
|
* See: https://www.sqlite.org/quirks.html#no_separate_boolean_datatype
|
|
132
136
|
*/
|
|
133
|
-
export declare const SqliteBoolean: import("./Type.js").
|
|
137
|
+
export declare const SqliteBoolean: import("./Type.js").UnionType<[import("./Type.js").LiteralType<0>, import("./Type.js").LiteralType<1>]>;
|
|
134
138
|
export type SqliteBoolean = typeof SqliteBoolean.Type;
|
|
139
|
+
/**
|
|
140
|
+
* Represents the {@link SqliteBoolean} value for `true`.
|
|
141
|
+
*
|
|
142
|
+
* See {@link SqliteBoolean}.
|
|
143
|
+
*/
|
|
144
|
+
export declare const sqliteTrue = 1;
|
|
145
|
+
/**
|
|
146
|
+
* Represents the {@link SqliteBoolean} value for `false`.
|
|
147
|
+
*
|
|
148
|
+
* See {@link SqliteBoolean}.
|
|
149
|
+
*/
|
|
150
|
+
export declare const sqliteFalse = 0;
|
|
135
151
|
//# sourceMappingURL=Sqlite.d.ts.map
|
package/dist/src/Sqlite.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sqlite.d.ts","sourceRoot":"","sources":["../../src/Sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAA2B,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAW,MAAM,EAAqB,MAAM,aAAa,CAAC;AACjE,OAAO,
|
|
1
|
+
{"version":3,"file":"Sqlite.d.ts","sourceRoot":"","sources":["../../src/Sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAA2B,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAW,MAAM,EAAqB,MAAM,aAAa,CAAC;AACjE,OAAO,EAAgB,UAAU,EAA6B,MAAM,WAAW,CAAC;AAChF,OAAO,EAAoB,SAAS,EAAE,MAAM,YAAY,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,YAAa,SAAQ,UAAU;IAC9C,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,KAAK,gBAAgB,CAAC;IAC7E,QAAQ,CAAC,MAAM,EAAE,MAAM,UAAU,CAAC;CACnC;AAED,MAAM,MAAM,kBAAkB,GAAG,CAC/B,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C,EAC5B,WAAW,CAAC,SAAS,EAAE,CAAC,KAAK,IAAI,KAChC,kBAAkB,CAAC,CAAC,CAuBtB,CAAC;AAEF,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,eAAe,CAAC;IACtB,GAAG,EAAE,OAAO,CAAC;CACd;AAED,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,aAAa,GAAG,MAAM,CAAC;AAEpE,kBAAkB;AAClB,eAAO,MAAM,GAAG;cACL,oBAAoB,iBACd,KAAK,CAAC,gBAAgB,CAAC,GACrC,WAAW;2BAoBgB,MAAM,GAAG,aAAa;aAcpC,MAAM,GAAG,MAAM;sBAGpB,oBAAoB,iBACd,KAAK,CAAC,gBAAgB,CAAC,GACrC,WAAW;CArBb,CAAC;AA0EF,eAAO,MAAM,aAAa,EAAE,SAAS,CAAC,MAAM,CACK,CAAC;AAElD,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,sBAAsB,GAChC,MAAM,SAAS,MACf,OAAO,WAAW,KAAG,MAAM,CAAC,IAAI,EAAE,WAAW,CAiB7C,CAAC;AAoBJ;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,yGAAc,CAAC;AACzC,MAAM,MAAM,aAAa,GAAG,OAAO,aAAa,CAAC,IAAI,CAAC;AAEtD;;;;GAIG;AACH,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B;;;;GAIG;AACH,eAAO,MAAM,WAAW,IAAI,CAAC"}
|
package/dist/src/Sqlite.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createTransferableError } from "./Error.js";
|
|
2
2
|
import { err, ok, tryAsync, trySync } from "./Result.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Null, Number, String, Uint8Array, union } from "./Type.js";
|
|
4
4
|
/**
|
|
5
5
|
* A value that can be stored in Sqlite.
|
|
6
6
|
*
|
|
@@ -21,9 +21,9 @@ export const createSqlite = (deps) => async (name, options) => tryAsync(async ()
|
|
|
21
21
|
}, createSqliteError);
|
|
22
22
|
const sqlite = {
|
|
23
23
|
exec: (query) => trySync(() => {
|
|
24
|
-
deps.console?.log("[sql]", query);
|
|
24
|
+
deps.console?.log("[sql]", { query });
|
|
25
25
|
const result = maybeLogSqliteQueryExecutionTime(query, () => driver.exec(query, isSqlMutation(query.sql)));
|
|
26
|
-
deps.console?.log("[sql]", result);
|
|
26
|
+
deps.console?.log("[sql]", { result });
|
|
27
27
|
return result;
|
|
28
28
|
}, (error) => ({
|
|
29
29
|
type: "SqliteError",
|
|
@@ -172,7 +172,35 @@ const isSqlMutationRegEx = new RegExp(`\\b(${[
|
|
|
172
172
|
"pragma",
|
|
173
173
|
"vacuum",
|
|
174
174
|
].join("|")})\\b`, "i");
|
|
175
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Removes SQL line comments (--) from a SQL string without using regex to avoid
|
|
177
|
+
* ReDoS vulnerabilities.
|
|
178
|
+
*/
|
|
179
|
+
const removeSqlComments = (sql) => {
|
|
180
|
+
let result = "";
|
|
181
|
+
let i = 0;
|
|
182
|
+
while (i < sql.length) {
|
|
183
|
+
// Check for comment start
|
|
184
|
+
if (i < sql.length - 1 && sql[i] === "-" && sql[i + 1] === "-") {
|
|
185
|
+
// Skip until end of line or end of string
|
|
186
|
+
i += 2;
|
|
187
|
+
while (i < sql.length && sql[i] !== "\n") {
|
|
188
|
+
i++;
|
|
189
|
+
}
|
|
190
|
+
// Keep the newline if present
|
|
191
|
+
if (i < sql.length && sql[i] === "\n") {
|
|
192
|
+
result += "\n";
|
|
193
|
+
i++;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
result += sql[i];
|
|
198
|
+
i++;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
};
|
|
203
|
+
export const isSqlMutation = (sql) => isSqlMutationRegEx.test(removeSqlComments(sql));
|
|
176
204
|
export const explainSqliteQueryPlan = (deps) => (query) => {
|
|
177
205
|
const result = deps.sqlite.exec({
|
|
178
206
|
...query,
|
|
@@ -181,7 +209,7 @@ export const explainSqliteQueryPlan = (deps) => (query) => {
|
|
|
181
209
|
if (!result.ok)
|
|
182
210
|
return result;
|
|
183
211
|
// eslint-disable-next-line no-console
|
|
184
|
-
console.log("
|
|
212
|
+
console.log("[explainSqliteQueryPlan]", query);
|
|
185
213
|
// eslint-disable-next-line no-console
|
|
186
214
|
console.log(drawSqliteQueryPlan(result.value.rows));
|
|
187
215
|
return ok();
|
|
@@ -203,9 +231,23 @@ const drawSqliteQueryPlan = (rows) => rows
|
|
|
203
231
|
.join("\n");
|
|
204
232
|
/**
|
|
205
233
|
* SQLite represents boolean values using `0` (false) and `1` (true) instead of
|
|
206
|
-
* a dedicated boolean type.
|
|
207
|
-
*
|
|
234
|
+
* a dedicated boolean type.
|
|
235
|
+
*
|
|
236
|
+
* Use {@link sqliteTrue} and {@link sqliteFalse} constants for better
|
|
237
|
+
* readability.
|
|
208
238
|
*
|
|
209
239
|
* See: https://www.sqlite.org/quirks.html#no_separate_boolean_datatype
|
|
210
240
|
*/
|
|
211
|
-
export const SqliteBoolean =
|
|
241
|
+
export const SqliteBoolean = union(0, 1);
|
|
242
|
+
/**
|
|
243
|
+
* Represents the {@link SqliteBoolean} value for `true`.
|
|
244
|
+
*
|
|
245
|
+
* See {@link SqliteBoolean}.
|
|
246
|
+
*/
|
|
247
|
+
export const sqliteTrue = 1;
|
|
248
|
+
/**
|
|
249
|
+
* Represents the {@link SqliteBoolean} value for `false`.
|
|
250
|
+
*
|
|
251
|
+
* See {@link SqliteBoolean}.
|
|
252
|
+
*/
|
|
253
|
+
export const sqliteFalse = 0;
|