@evolu/common 8.7.0 → 8.8.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/dist/src/Array.d.ts +21 -21
- package/dist/src/Array.js +5 -5
- package/dist/src/Assert.d.ts +478 -141
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +644 -99
- package/dist/src/{Buffer.d.ts → Binary.d.ts} +85 -12
- package/dist/src/Binary.d.ts.map +1 -0
- package/dist/src/{Buffer.js → Binary.js} +206 -24
- package/dist/src/Console.d.ts +2 -10
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +6 -12
- package/dist/src/Crypto.d.ts +5 -5
- package/dist/src/Crypto.js +1 -1
- package/dist/src/Eq.d.ts +35 -40
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +51 -87
- package/dist/src/Function.d.ts +9 -14
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +7 -9
- package/dist/src/Http.d.ts +6 -7
- package/dist/src/Http.d.ts.map +1 -1
- package/dist/src/LeakDetector.d.ts.map +1 -1
- package/dist/src/LeakDetector.js +1 -0
- package/dist/src/LockManager.d.ts +1 -1
- package/dist/src/LockManager.js +1 -1
- package/dist/src/Lookup.d.ts +3 -2
- package/dist/src/Lookup.d.ts.map +1 -1
- package/dist/src/Lookup.js +4 -3
- package/dist/src/Number.d.ts +1 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +1 -1
- package/dist/src/Object.d.ts +4 -4
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +5 -4
- package/dist/src/Option.d.ts +1 -2
- package/dist/src/Option.d.ts.map +1 -1
- package/dist/src/Option.js +1 -2
- package/dist/src/Platform.js +2 -0
- package/dist/src/Polyfills.js +4 -1
- package/dist/src/Random.d.ts +1 -1
- package/dist/src/Result.d.ts +49 -54
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +17 -22
- package/dist/src/Set.d.ts +6 -6
- package/dist/src/Set.js +2 -2
- package/dist/src/Sqlite.js +1 -1
- package/dist/src/String.js +3 -2
- package/dist/src/Task.d.ts +32 -32
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +20 -22
- package/dist/src/Test.d.ts +31 -3
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +46 -3
- package/dist/src/Time.d.ts +3 -11
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +10 -18
- package/dist/src/Type.d.ts +87 -85
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +70 -63
- package/dist/src/Types.d.ts +12 -12
- package/dist/src/Types.js +1 -1
- package/dist/src/WebSocket.d.ts +4 -7
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/Worker.d.ts +1 -1
- package/dist/src/Worker.js +7 -7
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/intl/_en.d.ts.map +1 -1
- package/dist/src/intl/_en.js +1 -1
- package/dist/src/intl/ar.d.ts.map +1 -1
- package/dist/src/intl/ar.js +1 -1
- package/dist/src/intl/bn.d.ts.map +1 -1
- package/dist/src/intl/bn.js +1 -1
- package/dist/src/intl/ca.d.ts.map +1 -1
- package/dist/src/intl/ca.js +1 -1
- package/dist/src/intl/cs.d.ts.map +1 -1
- package/dist/src/intl/cs.js +1 -1
- package/dist/src/intl/da.d.ts.map +1 -1
- package/dist/src/intl/da.js +1 -1
- package/dist/src/intl/de.d.ts.map +1 -1
- package/dist/src/intl/de.js +1 -1
- package/dist/src/intl/el.d.ts.map +1 -1
- package/dist/src/intl/el.js +1 -1
- package/dist/src/intl/es.d.ts.map +1 -1
- package/dist/src/intl/es.js +1 -1
- package/dist/src/intl/fa.d.ts.map +1 -1
- package/dist/src/intl/fa.js +1 -1
- package/dist/src/intl/fi.d.ts.map +1 -1
- package/dist/src/intl/fi.js +1 -1
- package/dist/src/intl/fil.d.ts.map +1 -1
- package/dist/src/intl/fil.js +1 -1
- package/dist/src/intl/fr.d.ts.map +1 -1
- package/dist/src/intl/fr.js +1 -1
- package/dist/src/intl/he.d.ts.map +1 -1
- package/dist/src/intl/he.js +1 -1
- package/dist/src/intl/hi.d.ts.map +1 -1
- package/dist/src/intl/hi.js +1 -1
- package/dist/src/intl/hr.d.ts.map +1 -1
- package/dist/src/intl/hr.js +1 -1
- package/dist/src/intl/hu.d.ts.map +1 -1
- package/dist/src/intl/hu.js +1 -1
- package/dist/src/intl/id.d.ts.map +1 -1
- package/dist/src/intl/id.js +1 -1
- package/dist/src/intl/it.d.ts.map +1 -1
- package/dist/src/intl/it.js +1 -1
- package/dist/src/intl/ja.d.ts.map +1 -1
- package/dist/src/intl/ja.js +1 -1
- package/dist/src/intl/ko.d.ts.map +1 -1
- package/dist/src/intl/ko.js +1 -1
- package/dist/src/intl/ml.d.ts.map +1 -1
- package/dist/src/intl/ml.js +1 -1
- package/dist/src/intl/mr.d.ts.map +1 -1
- package/dist/src/intl/mr.js +1 -1
- package/dist/src/intl/ms.d.ts.map +1 -1
- package/dist/src/intl/ms.js +1 -1
- package/dist/src/intl/nb.d.ts.map +1 -1
- package/dist/src/intl/nb.js +1 -1
- package/dist/src/intl/nl.d.ts.map +1 -1
- package/dist/src/intl/nl.js +1 -1
- package/dist/src/intl/pa.d.ts.map +1 -1
- package/dist/src/intl/pa.js +1 -1
- package/dist/src/intl/pl.d.ts.map +1 -1
- package/dist/src/intl/pl.js +1 -1
- package/dist/src/intl/pt-BR.d.ts.map +1 -1
- package/dist/src/intl/pt-BR.js +1 -1
- package/dist/src/intl/pt.d.ts.map +1 -1
- package/dist/src/intl/pt.js +1 -1
- package/dist/src/intl/ro.d.ts.map +1 -1
- package/dist/src/intl/ro.js +1 -1
- package/dist/src/intl/sk.d.ts.map +1 -1
- package/dist/src/intl/sk.js +1 -1
- package/dist/src/intl/sl.d.ts.map +1 -1
- package/dist/src/intl/sl.js +1 -1
- package/dist/src/intl/sv.d.ts.map +1 -1
- package/dist/src/intl/sv.js +1 -1
- package/dist/src/intl/sw.d.ts.map +1 -1
- package/dist/src/intl/sw.js +1 -1
- package/dist/src/intl/ta.d.ts.map +1 -1
- package/dist/src/intl/ta.js +1 -1
- package/dist/src/intl/te.d.ts.map +1 -1
- package/dist/src/intl/te.js +1 -1
- package/dist/src/intl/th.d.ts.map +1 -1
- package/dist/src/intl/th.js +1 -1
- package/dist/src/intl/tr.d.ts.map +1 -1
- package/dist/src/intl/tr.js +1 -1
- package/dist/src/intl/uk.d.ts.map +1 -1
- package/dist/src/intl/uk.js +1 -1
- package/dist/src/intl/ur.d.ts.map +1 -1
- package/dist/src/intl/ur.js +1 -1
- package/dist/src/intl/vi.d.ts.map +1 -1
- package/dist/src/intl/vi.js +1 -1
- package/dist/src/intl/zh-CN.d.ts.map +1 -1
- package/dist/src/intl/zh-CN.js +1 -1
- package/dist/src/intl/zh-TW.d.ts.map +1 -1
- package/dist/src/intl/zh-TW.js +1 -1
- package/dist/src/local-first/Evolu.d.ts +12 -12
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +2 -2
- package/dist/src/local-first/Protocol.d.ts +14 -76
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +29 -206
- package/dist/src/local-first/Query.d.ts +11 -11
- package/dist/src/local-first/Query.js +6 -6
- package/dist/src/local-first/Relay.d.ts +2 -2
- package/dist/src/local-first/Schema.d.ts +3 -5
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +2 -4
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +5 -5
- package/dist/src/local-first/Storage.js +4 -4
- package/dist/src/local-first/Timestamp.d.ts +9 -0
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +13 -10
- package/package.json +7 -4
- package/src/Array.test.ts +809 -0
- package/src/Array.ts +21 -21
- package/src/Assert.test.ts +1181 -0
- package/src/Assert.ts +971 -227
- package/src/BigInt.test.ts +28 -0
- package/src/Binary.test.ts +1128 -0
- package/src/{Buffer.ts → Binary.ts} +273 -46
- package/src/Brand.test.ts +90 -0
- package/src/Cache.test.ts +183 -0
- package/src/Callbacks.test.ts +66 -0
- package/src/Console.test.ts +809 -0
- package/src/Console.ts +9 -17
- package/src/Crypto.test.ts +138 -0
- package/src/Crypto.ts +5 -5
- package/src/Eq.test.ts +333 -0
- package/src/Eq.ts +110 -176
- package/src/Error.test.ts +118 -0
- package/src/Function.test.ts +204 -0
- package/src/Function.ts +10 -15
- package/src/Http.test.ts +816 -0
- package/src/Http.ts +6 -7
- package/src/LeakDetector.test.ts +210 -0
- package/src/LeakDetector.ts +1 -0
- package/src/LockManager.ts +2 -2
- package/src/Lookup.test.ts +418 -0
- package/src/Lookup.ts +4 -3
- package/src/Microtask.test.ts +140 -0
- package/src/Number.test.ts +218 -0
- package/src/Number.ts +2 -2
- package/src/Object.test.ts +293 -0
- package/src/Object.ts +5 -4
- package/src/Option.test.ts +104 -0
- package/src/Option.ts +1 -2
- package/src/Order.test.ts +56 -0
- package/src/Platform.ts +2 -0
- package/src/Polyfills.ts +3 -4
- package/src/Random.test.ts +89 -0
- package/src/Random.ts +1 -1
- package/src/Ref.test.ts +139 -0
- package/src/RefCount.test.ts +184 -0
- package/src/Relation.test.ts +505 -0
- package/src/Resource.test.ts +4623 -0
- package/src/Result.test.ts +1829 -0
- package/src/Result.ts +49 -54
- package/src/Schedule.test.ts +1554 -0
- package/src/Set.test.ts +279 -0
- package/src/Set.ts +6 -6
- package/src/Sqlite.ts +1 -1
- package/src/Store.test.ts +288 -0
- package/src/String.test.ts +54 -0
- package/src/String.ts +3 -3
- package/src/Task.test.ts +10820 -0
- package/src/Task.ts +38 -43
- package/src/Test.test.ts +88 -0
- package/src/Test.ts +49 -3
- package/src/Time.test.ts +926 -0
- package/src/Time.ts +13 -21
- package/src/Type.base64.test.ts +88 -0
- package/src/Type.test.ts +20465 -0
- package/src/Type.ts +114 -111
- package/src/Types.test.ts +324 -0
- package/src/Types.ts +13 -13
- package/src/WebSocket.ts +4 -7
- package/src/Worker.test.ts +755 -0
- package/src/Worker.ts +13 -18
- package/src/index.ts +1 -1
- package/src/intl/_en.ts +1 -1
- package/src/intl/ar.ts +1 -1
- package/src/intl/bn.ts +1 -1
- package/src/intl/ca.ts +1 -1
- package/src/intl/cs.ts +1 -1
- package/src/intl/da.ts +1 -1
- package/src/intl/de.ts +1 -1
- package/src/intl/el.ts +1 -1
- package/src/intl/es.ts +1 -1
- package/src/intl/fa.ts +1 -1
- package/src/intl/fi.ts +1 -1
- package/src/intl/fil.ts +1 -1
- package/src/intl/fr.ts +1 -1
- package/src/intl/he.ts +1 -1
- package/src/intl/hi.ts +1 -1
- package/src/intl/hr.ts +1 -1
- package/src/intl/hu.ts +1 -1
- package/src/intl/id.ts +1 -1
- package/src/intl/intl.test.ts +3106 -0
- package/src/intl/it.ts +1 -1
- package/src/intl/ja.ts +1 -1
- package/src/intl/ko.ts +1 -1
- package/src/intl/ml.ts +1 -1
- package/src/intl/mr.ts +1 -1
- package/src/intl/ms.ts +1 -1
- package/src/intl/nb.ts +1 -1
- package/src/intl/nl.ts +1 -1
- package/src/intl/pa.ts +1 -1
- package/src/intl/pl.ts +1 -1
- package/src/intl/pt-BR.ts +1 -1
- package/src/intl/pt.ts +1 -1
- package/src/intl/ro.ts +1 -1
- package/src/intl/sk.ts +1 -1
- package/src/intl/sl.ts +1 -1
- package/src/intl/sv.ts +1 -1
- package/src/intl/sw.ts +1 -1
- package/src/intl/ta.ts +1 -1
- package/src/intl/te.ts +1 -1
- package/src/intl/th.ts +1 -1
- package/src/intl/tr.ts +1 -1
- package/src/intl/uk.ts +1 -1
- package/src/intl/ur.ts +1 -1
- package/src/intl/vi.ts +1 -1
- package/src/intl/zh-CN.ts +1 -1
- package/src/intl/zh-TW.ts +1 -1
- package/src/local-first/Evolu.test.ts +1921 -0
- package/src/local-first/Evolu.ts +18 -14
- package/src/local-first/Owner.test.ts +83 -0
- package/src/local-first/Protocol.test.ts +1037 -0
- package/src/local-first/Protocol.ts +58 -261
- package/src/local-first/Query.test.ts +349 -0
- package/src/local-first/Query.ts +11 -11
- package/src/local-first/Relay.ts +2 -2
- package/src/local-first/Schema.ts +3 -5
- package/src/local-first/Shared.test.ts +1828 -0
- package/src/local-first/Shared.ts +9 -8
- package/src/local-first/Storage.ts +4 -4
- package/src/local-first/Timestamp.test.ts +374 -0
- package/src/local-first/Timestamp.ts +22 -10
- package/dist/src/Buffer.d.ts.map +0 -1
|
@@ -175,16 +175,16 @@
|
|
|
175
175
|
*/
|
|
176
176
|
import { type NonEmptyReadonlyArray } from "../Array.ts";
|
|
177
177
|
import type { Brand } from "../Brand.ts";
|
|
178
|
-
import { type Buffer } from "../
|
|
178
|
+
import { type Buffer } from "../Binary.ts";
|
|
179
179
|
import type { ConsoleDep } from "../Console.ts";
|
|
180
180
|
import { type DecryptWithXChaCha20Poly1305Error, EncryptionKey, type RandomBytesDep } from "../Crypto.ts";
|
|
181
181
|
import { type Result } from "../Result.ts";
|
|
182
|
-
import { SqliteValue } from "../Sqlite.ts";
|
|
182
|
+
import type { SqliteValue } from "../Sqlite.ts";
|
|
183
183
|
import { type Task } from "../Task.ts";
|
|
184
|
-
import {
|
|
184
|
+
import { NonNegativeInt, PositiveInt, type Typed } from "../Type.ts";
|
|
185
185
|
import { type Owner, type OwnerError, OwnerId, OwnerWriteKey } from "./Owner.ts";
|
|
186
186
|
import { type BaseRange, type CrdtMessage, DbChange, type EncryptedCrdtMessage, type EncryptedDbChange, type FingerprintRange, RangeType, type SkipRange, type StorageDep } from "./Storage.ts";
|
|
187
|
-
import {
|
|
187
|
+
import { Timestamp } from "./Timestamp.ts";
|
|
188
188
|
/**
|
|
189
189
|
* Protocol message maximum size.
|
|
190
190
|
*
|
|
@@ -289,7 +289,7 @@ export interface ProtocolVersionError extends OwnerError, Typed<"ProtocolVersion
|
|
|
289
289
|
}
|
|
290
290
|
/** Error for invalid or corrupted protocol message data. */
|
|
291
291
|
export interface ProtocolInvalidDataError extends Typed<"ProtocolInvalidDataError"> {
|
|
292
|
-
readonly data:
|
|
292
|
+
readonly data: Uint8Array;
|
|
293
293
|
readonly error: unknown;
|
|
294
294
|
}
|
|
295
295
|
/** Error when a {@link OwnerWriteKey} is invalid, missing, or fails validation. */
|
|
@@ -425,49 +425,6 @@ export interface ApplyProtocolMessageAsRelayResult extends Typed<"Response"> {
|
|
|
425
425
|
export declare const applyProtocolMessageAsRelay: (inputMessage: Uint8Array, options?: ApplyProtocolMessageAsRelayOptions,
|
|
426
426
|
/** For tests only. */
|
|
427
427
|
version?: NonNegativeInt) => Task<ApplyProtocolMessageAsRelayResult, ProtocolInvalidDataError, StorageDep>;
|
|
428
|
-
export declare const decodeRle: <T>(buffer: Buffer, length: NonNegativeInt, decodeValue: () => T) => ReadonlyArray<T>;
|
|
429
|
-
/**
|
|
430
|
-
* Evolu uses MessagePack to handle finite numbers except for NonNegativeInt.
|
|
431
|
-
* For NonNegativeInt, Evolu provides more efficient encoding.
|
|
432
|
-
*/
|
|
433
|
-
export declare const encodeNumber: (buffer: Buffer, number: FiniteNumber) => void;
|
|
434
|
-
export declare const decodeNumber: (buffer: Buffer) => FiniteNumber;
|
|
435
|
-
/**
|
|
436
|
-
* Encodes an array of boolean flags into a single byte.
|
|
437
|
-
*
|
|
438
|
-
* Each element in the array corresponds to a bit (0-7). Array can have 0-8
|
|
439
|
-
* elements.
|
|
440
|
-
*
|
|
441
|
-
* ### Example
|
|
442
|
-
*
|
|
443
|
-
* ```ts
|
|
444
|
-
* import { assertEqual, createBuffer } from "@evolu/common";
|
|
445
|
-
* import { encodeFlags } from "@evolu/common/local-first";
|
|
446
|
-
*
|
|
447
|
-
* const buffer = createBuffer();
|
|
448
|
-
* encodeFlags(buffer, [true, false, true]);
|
|
449
|
-
*
|
|
450
|
-
* assertEqual(buffer.unwrap(), new Uint8Array([0b101]));
|
|
451
|
-
* ```
|
|
452
|
-
*/
|
|
453
|
-
export declare const encodeFlags: (buffer: Buffer, flags: ReadonlyArray<boolean>) => void;
|
|
454
|
-
/**
|
|
455
|
-
* Decodes a byte into an array of boolean flags.
|
|
456
|
-
*
|
|
457
|
-
* ### Example
|
|
458
|
-
*
|
|
459
|
-
* ```ts
|
|
460
|
-
* import { assertEqual, createBuffer, PositiveInt } from "@evolu/common";
|
|
461
|
-
* import { decodeFlags } from "@evolu/common/local-first";
|
|
462
|
-
*
|
|
463
|
-
* const buffer = createBuffer([0b101]);
|
|
464
|
-
* const flags = decodeFlags(buffer, PositiveInt.orThrow(3));
|
|
465
|
-
*
|
|
466
|
-
* assertEqual(flags, [true, false, true]);
|
|
467
|
-
* assertEqual(buffer.getLength(), 0);
|
|
468
|
-
* ```
|
|
469
|
-
*/
|
|
470
|
-
export declare const decodeFlags: (buffer: Buffer, count: PositiveInt) => ReadonlyArray<boolean>;
|
|
471
428
|
/**
|
|
472
429
|
* Encodes and encrypts a {@link DbChange} using the provided owner's encryption
|
|
473
430
|
* key. Returns an encrypted binary representation as {@link EncryptedDbChange}.
|
|
@@ -484,24 +441,18 @@ export declare const encodeAndEncryptDbChange: (deps: RandomBytesDep) => (messag
|
|
|
484
441
|
*/
|
|
485
442
|
export declare const decryptAndDecodeDbChange: (message: EncryptedCrdtMessage, key: EncryptionKey) => Result<DbChange, DecryptWithXChaCha20Poly1305Error | ProtocolInvalidDataError | ProtocolTimestampMismatchError>;
|
|
486
443
|
/**
|
|
487
|
-
*
|
|
488
|
-
* more efficient than encoding via {@link encodeNumber}.
|
|
444
|
+
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
489
445
|
*
|
|
490
|
-
*
|
|
491
|
-
*/
|
|
492
|
-
export declare const encodeNonNegativeInt: (buffer: Buffer, int: NonNegativeInt) => void;
|
|
493
|
-
/**
|
|
494
|
-
* Decodes a non-negative integer from a variable-length integer format.
|
|
446
|
+
* Note: This is a stub for future implementation. It should use:
|
|
495
447
|
*
|
|
496
|
-
*
|
|
448
|
+
* - DecodeVersionAndOwner
|
|
449
|
+
* - DecodeError or decodeWriteKeys (depending on context)
|
|
450
|
+
* - DecodeMessages
|
|
451
|
+
* - DecodeRanges
|
|
452
|
+
*
|
|
453
|
+
* If you want to help, please contribute to this function.
|
|
497
454
|
*/
|
|
498
|
-
export declare const
|
|
499
|
-
export declare const encodeLength: (buffer: Buffer, value: ArrayLike<any>) => void;
|
|
500
|
-
export declare const decodeLength: typeof decodeNonNegativeInt;
|
|
501
|
-
export declare const encodeString: (buffer: Buffer, value: string) => void;
|
|
502
|
-
export declare const decodeString: (buffer: Buffer) => string;
|
|
503
|
-
export declare const encodeNodeId: (buffer: Buffer, nodeId: NodeId) => void;
|
|
504
|
-
export declare const decodeNodeId: (buffer: Buffer) => NodeId;
|
|
455
|
+
export declare const decodeProtocolMessageToJson: (_protocolMessage: ProtocolMessage, _isInitiator: boolean) => unknown;
|
|
505
456
|
export declare const ProtocolValueType: {
|
|
506
457
|
readonly String: number & Brand<"NonNaN"> & Brand<"Finite"> & Brand<"Int"> & Brand<"NonNegative">;
|
|
507
458
|
readonly Number: number & Brand<"NonNaN"> & Brand<"Finite"> & Brand<"Int"> & Brand<"NonNegative">;
|
|
@@ -517,18 +468,5 @@ export declare const ProtocolValueType: {
|
|
|
517
468
|
};
|
|
518
469
|
export declare const encodeSqliteValue: (buffer: Buffer, value: SqliteValue) => void;
|
|
519
470
|
export declare const decodeSqliteValue: (buffer: Buffer) => SqliteValue;
|
|
520
|
-
/**
|
|
521
|
-
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
522
|
-
*
|
|
523
|
-
* Note: This is a stub for future implementation. It should use:
|
|
524
|
-
*
|
|
525
|
-
* - DecodeVersionAndOwner
|
|
526
|
-
* - DecodeError or decodeWriteKeys (depending on context)
|
|
527
|
-
* - DecodeMessages
|
|
528
|
-
* - DecodeRanges
|
|
529
|
-
*
|
|
530
|
-
* If you want to help, please contribute to this function.
|
|
531
|
-
*/
|
|
532
|
-
export declare const decodeProtocolMessageToJson: (_protocolMessage: ProtocolMessage, _isInitiator: boolean) => unknown;
|
|
533
471
|
export {};
|
|
534
472
|
//# sourceMappingURL=Protocol.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Protocol.d.ts","sourceRoot":"","sources":["../../../src/local-first/Protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgKG;AAEH;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"Protocol.d.ts","sourceRoot":"","sources":["../../../src/local-first/Protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgKG;AAEH;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAOrB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,KAAK,MAAM,EAgBZ,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAGL,KAAK,iCAAiC,EACtC,aAAa,EAGb,KAAK,cAAc,EAGpB,MAAM,cAAc,CAAC;AAItB,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,cAAc,C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S,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IAEjE,YAAY,CAAC,EAAE,sBAAsB,CAAC;IACtC,aAAa,CAAC,EAAE,4BAA4B,CAAC;CAC9C;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,iCAAkC,SAAQ,KAAK,CAAC,UAAU,CAAC;IAC1E,QAAQ,CAAC,OAAO,EAAE,eAAe,CAAC;CACnC;AAED,eAAO,MAAM,2BAA2B,iBAEtB,UAAU,YACf,kCAAkC;AAC3C,sBAAsB;UACb,cAAc,KACtB,IAAI,CACL,iCAAiC,EACjC,wBAAwB,EACxB,UAAU,CA4JX,CAAC;AA8dJ;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,SAC5B,cAAc,eACX,WAAW,OAAO,aAAa,KAAG,iBA0C3C,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,YAC1B,oBAAoB,OACxB,aAAa,KACjB,MAAM,CACP,QAAQ,EACN,iCAAiC,GACjC,wBAAwB,GACxB,8BAA8B,CA8DjC,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,2BAA2B,qBACpB,eAAe,gBACnB,OAAO,KACpB,OAOF,CAAC;AAGF,eAAO,MAAM,iBAAiB;aAI5B,MAAM;aACN,MAAM;aACN,IAAI;aACJ,KAAK;aAIL,cAAc;aAId,WAAW;aACX,SAAS;aACT,EAAE;aACF,IAAI;aAKJ,0BAA0B;aAE1B,uBAAuB;CAIf,CAAC;AAEX,eAAO,MAAM,iBAAiB,WAAY,MAAM,SAAS,WAAW,KAAG,IA6FtE,CAAC;AAEF,eAAO,MAAM,iBAAiB,WAAY,MAAM,KAAG,WA6DlD,CAAC"}
|
|
@@ -174,21 +174,19 @@
|
|
|
174
174
|
* indicators) to be forwarded by relays without storage overhead.
|
|
175
175
|
*/
|
|
176
176
|
import { createMutableArray, isNonEmptyArray, } from "../Array.js";
|
|
177
|
-
import { assert, assertNonNullable, assertNotUndefined } from "../Assert.js";
|
|
178
|
-
import {
|
|
177
|
+
import { assert, assertNonNullable, assertNotUndefined, assertSame, } from "../Assert.js";
|
|
178
|
+
import { createBuffer, createRunLengthEncoder, decodeFlags, decodeJsonValue, decodeLength, decodeNonNegativeInt, decodeNumber, decodeRle, decodeString, encodeFlags, encodeJsonValue, encodeLength, encodeNonNegativeInt, encodeNumber, encodeString, } from "../Binary.js";
|
|
179
179
|
import { createPadmePadding, decryptWithXChaCha20Poly1305, EncryptionKey, encryptWithXChaCha20Poly1305, Entropy24, XChaCha20Poly1305Ciphertext, xChaCha20Poly1305NonceLength, } from "../Crypto.js";
|
|
180
180
|
import { eqArrayNumber } from "../Eq.js";
|
|
181
181
|
import { computeBalancedBuckets } from "../Number.js";
|
|
182
182
|
import { createMutableRecord, objectToEntries } from "../Object.js";
|
|
183
183
|
import { err, ok } from "../Result.js";
|
|
184
|
-
import { SqliteValue } from "../Sqlite.js";
|
|
185
184
|
import { AbortError } from "../Task.js";
|
|
186
185
|
import { Millis } from "../Time.js";
|
|
187
|
-
import { Base64Url, base64UrlToUint8Array, between, DateIso, FiniteNumber, Id, IdBytes, idBytesToId, idBytesTypeValueLength, idToIdBytes, Int, Json, jsonToJsonValue, NonNegativeInt, onePositiveInt, PositiveInt, uint8ArrayToBase64Url, zeroNonNegativeInt, } from "../Type.js";
|
|
186
|
+
import { assertType, Base64Url, base64UrlToUint8Array, between, DateIso, FiniteNumber, Id, IdBytes, idBytesToId, idBytesTypeValueLength, idToIdBytes, Int, Json, jsonToJsonValue, NonNegativeInt, onePositiveInt, PositiveInt, uint8ArrayToBase64Url, zeroNonNegativeInt, } from "../Type.js";
|
|
188
187
|
import { OwnerId, OwnerIdBytes, ownerIdToOwnerIdBytes, OwnerWriteKey, ownerWriteKeyLength, } from "./Owner.js";
|
|
189
188
|
import { DbChange, fingerprintSize, InfiniteUpperBound, RangeType, } from "./Storage.js";
|
|
190
|
-
import { Counter, eqTimestamp, NodeId, Timestamp, TimestampBytes, timestampBytesLength, timestampBytesToTimestamp, timestampToTimestampBytes, } from "./Timestamp.js";
|
|
191
|
-
const jsonBuffer = createBuffer();
|
|
189
|
+
import { Counter, eqTimestamp, NodeId, nodeIdBytesLength, nodeIdBytesToNodeId, nodeIdToNodeIdBytes, Timestamp, TimestampBytes, timestampBytesLength, timestampBytesToTimestamp, timestampToTimestampBytes, } from "./Timestamp.js";
|
|
192
190
|
const minProtocolMessageMaxSize = 1_000_000;
|
|
193
191
|
const maxProtocolMessageMaxSize = 100_000_000;
|
|
194
192
|
/**
|
|
@@ -497,12 +495,12 @@ export const createTimestampsBuffer = () => {
|
|
|
497
495
|
encodeNonNegativeInt(buffer, value);
|
|
498
496
|
});
|
|
499
497
|
const nodeIdEncoder = createRunLengthEncoder((buffer, value) => {
|
|
500
|
-
|
|
498
|
+
buffer.extend(nodeIdToNodeIdBytes(value));
|
|
501
499
|
});
|
|
502
500
|
return {
|
|
503
501
|
add: (timestamp) => {
|
|
504
502
|
const delta = timestamp.millis - previousMillis;
|
|
505
|
-
|
|
503
|
+
assertType(NonNegativeInt, delta);
|
|
506
504
|
count++;
|
|
507
505
|
syncCount();
|
|
508
506
|
previousMillis = timestamp.millis;
|
|
@@ -527,29 +525,6 @@ export const createTimestampsBuffer = () => {
|
|
|
527
525
|
},
|
|
528
526
|
};
|
|
529
527
|
};
|
|
530
|
-
const createRunLengthEncoder = (encodeValue) => {
|
|
531
|
-
const buffer = createBuffer();
|
|
532
|
-
let previousLength = zeroNonNegativeInt;
|
|
533
|
-
let previousValue = null;
|
|
534
|
-
let runLength = zeroNonNegativeInt;
|
|
535
|
-
return {
|
|
536
|
-
add: (value) => {
|
|
537
|
-
if (value === previousValue) {
|
|
538
|
-
runLength++;
|
|
539
|
-
buffer.truncate(previousLength);
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
previousValue = value;
|
|
543
|
-
runLength = NonNegativeInt.orThrow(1);
|
|
544
|
-
}
|
|
545
|
-
previousLength = buffer.getLength();
|
|
546
|
-
encodeValue(buffer, value);
|
|
547
|
-
encodeNonNegativeInt(buffer, runLength);
|
|
548
|
-
},
|
|
549
|
-
getLength: () => buffer.getLength(),
|
|
550
|
-
unwrap: () => buffer.unwrap(),
|
|
551
|
-
};
|
|
552
|
-
};
|
|
553
528
|
export const applyProtocolMessageAsClient = (inputMessage, options = {}) => async (run) => {
|
|
554
529
|
const { storage } = run.deps;
|
|
555
530
|
try {
|
|
@@ -669,7 +644,7 @@ version = protocolVersion) => async (run) => {
|
|
|
669
644
|
});
|
|
670
645
|
}
|
|
671
646
|
const messageType = input.shift();
|
|
672
|
-
|
|
647
|
+
assertSame(messageType, MessageType.Request);
|
|
673
648
|
const hasWriteKey = input.shift();
|
|
674
649
|
let writeKey;
|
|
675
650
|
if (hasWriteKey === 1) {
|
|
@@ -1130,7 +1105,7 @@ const decodeTimestamps = (buffer, length) => {
|
|
|
1130
1105
|
throw new ProtocolDecodeError(counter.error.type);
|
|
1131
1106
|
return counter.value;
|
|
1132
1107
|
});
|
|
1133
|
-
const nodeIds = decodeRle(buffer, length, () =>
|
|
1108
|
+
const nodeIds = decodeRle(buffer, length, () => nodeIdBytesToNodeId(buffer.shiftN(nodeIdBytesLength)));
|
|
1134
1109
|
const timestamps = createMutableArray(length);
|
|
1135
1110
|
for (let i = 0; i < length; i++) {
|
|
1136
1111
|
timestamps[i] = {
|
|
@@ -1141,97 +1116,10 @@ const decodeTimestamps = (buffer, length) => {
|
|
|
1141
1116
|
}
|
|
1142
1117
|
return timestamps;
|
|
1143
1118
|
};
|
|
1144
|
-
export const decodeRle = (buffer, length, decodeValue) => {
|
|
1145
|
-
const values = createMutableArray(length);
|
|
1146
|
-
let index = 0;
|
|
1147
|
-
while (index < length) {
|
|
1148
|
-
const value = decodeValue();
|
|
1149
|
-
const runLength = decodeNonNegativeInt(buffer);
|
|
1150
|
-
// Prevent infinite loop on malformed input.
|
|
1151
|
-
if (runLength === 0) {
|
|
1152
|
-
throw new ProtocolDecodeError("Invalid RLE encoding: runLength must be positive");
|
|
1153
|
-
}
|
|
1154
|
-
const remaining = length - index;
|
|
1155
|
-
// Prevent CPU/memory amplification via oversized runLength.
|
|
1156
|
-
if (runLength > remaining) {
|
|
1157
|
-
throw new ProtocolDecodeError(`Invalid RLE encoding: runLength ${runLength} exceeds remaining ${remaining}`);
|
|
1158
|
-
}
|
|
1159
|
-
for (let i = 0; i < runLength; i++) {
|
|
1160
|
-
values[index] = value;
|
|
1161
|
-
index++;
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
return values;
|
|
1165
|
-
};
|
|
1166
1119
|
const decodeId = (buffer) => {
|
|
1167
1120
|
const bytes = buffer.shiftN(idBytesTypeValueLength);
|
|
1168
1121
|
return idBytesToId(bytes);
|
|
1169
1122
|
};
|
|
1170
|
-
/**
|
|
1171
|
-
* Evolu uses MessagePack to handle finite numbers except for NonNegativeInt.
|
|
1172
|
-
* For NonNegativeInt, Evolu provides more efficient encoding.
|
|
1173
|
-
*/
|
|
1174
|
-
export const encodeNumber = (buffer, number) => {
|
|
1175
|
-
encodeJsonValue(buffer, number);
|
|
1176
|
-
};
|
|
1177
|
-
export const decodeNumber = (buffer) => {
|
|
1178
|
-
const numberResult = FiniteNumber.fromUnknown(decodeJsonValue(buffer));
|
|
1179
|
-
if (!numberResult.ok)
|
|
1180
|
-
throw new ProtocolDecodeError(numberResult.error.type);
|
|
1181
|
-
return numberResult.value;
|
|
1182
|
-
};
|
|
1183
|
-
/**
|
|
1184
|
-
* Encodes an array of boolean flags into a single byte.
|
|
1185
|
-
*
|
|
1186
|
-
* Each element in the array corresponds to a bit (0-7). Array can have 0-8
|
|
1187
|
-
* elements.
|
|
1188
|
-
*
|
|
1189
|
-
* ### Example
|
|
1190
|
-
*
|
|
1191
|
-
* ```ts
|
|
1192
|
-
* import { assertEqual, createBuffer } from "@evolu/common";
|
|
1193
|
-
* import { encodeFlags } from "@evolu/common/local-first";
|
|
1194
|
-
*
|
|
1195
|
-
* const buffer = createBuffer();
|
|
1196
|
-
* encodeFlags(buffer, [true, false, true]);
|
|
1197
|
-
*
|
|
1198
|
-
* assertEqual(buffer.unwrap(), new Uint8Array([0b101]));
|
|
1199
|
-
* ```
|
|
1200
|
-
*/
|
|
1201
|
-
export const encodeFlags = (buffer, flags) => {
|
|
1202
|
-
let byte = 0;
|
|
1203
|
-
for (let i = 0; i < flags.length && i < 8; i++) {
|
|
1204
|
-
if (flags[i]) {
|
|
1205
|
-
byte |= 1 << i;
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
buffer.extend([byte]);
|
|
1209
|
-
};
|
|
1210
|
-
/**
|
|
1211
|
-
* Decodes a byte into an array of boolean flags.
|
|
1212
|
-
*
|
|
1213
|
-
* ### Example
|
|
1214
|
-
*
|
|
1215
|
-
* ```ts
|
|
1216
|
-
* import { assertEqual, createBuffer, PositiveInt } from "@evolu/common";
|
|
1217
|
-
* import { decodeFlags } from "@evolu/common/local-first";
|
|
1218
|
-
*
|
|
1219
|
-
* const buffer = createBuffer([0b101]);
|
|
1220
|
-
* const flags = decodeFlags(buffer, PositiveInt.orThrow(3));
|
|
1221
|
-
*
|
|
1222
|
-
* assertEqual(flags, [true, false, true]);
|
|
1223
|
-
* assertEqual(buffer.getLength(), 0);
|
|
1224
|
-
* ```
|
|
1225
|
-
*/
|
|
1226
|
-
export const decodeFlags = (buffer, count) => {
|
|
1227
|
-
const byte = buffer.shift();
|
|
1228
|
-
const length = globalThis.Math.min(count, 8);
|
|
1229
|
-
const flags = createMutableArray(length);
|
|
1230
|
-
for (let i = 0; i < length; i++) {
|
|
1231
|
-
flags[i] = (byte & (1 << i)) !== 0;
|
|
1232
|
-
}
|
|
1233
|
-
return flags;
|
|
1234
|
-
};
|
|
1235
1123
|
/**
|
|
1236
1124
|
* Encodes and encrypts a {@link DbChange} using the provided owner's encryption
|
|
1237
1125
|
* key. Returns an encrypted binary representation as {@link EncryptedDbChange}.
|
|
@@ -1323,73 +1211,26 @@ export const decryptAndDecodeDbChange = (message, key) => {
|
|
|
1323
1211
|
}
|
|
1324
1212
|
};
|
|
1325
1213
|
/**
|
|
1326
|
-
*
|
|
1327
|
-
* more efficient than encoding via {@link encodeNumber}.
|
|
1214
|
+
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
1328
1215
|
*
|
|
1329
|
-
*
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
}
|
|
1336
|
-
let remaining = BigInt(int);
|
|
1337
|
-
const bytes = [];
|
|
1338
|
-
while (remaining !== 0n) {
|
|
1339
|
-
const byte = globalThis.Number(remaining & 127n);
|
|
1340
|
-
bytes.push(byte);
|
|
1341
|
-
remaining >>= 7n;
|
|
1342
|
-
}
|
|
1343
|
-
for (let i = 0; i < bytes.length - 1; i++) {
|
|
1344
|
-
bytes[i] |= 128;
|
|
1345
|
-
}
|
|
1346
|
-
buffer.extend(bytes);
|
|
1347
|
-
};
|
|
1348
|
-
/**
|
|
1349
|
-
* Decodes a non-negative integer from a variable-length integer format.
|
|
1216
|
+
* Note: This is a stub for future implementation. It should use:
|
|
1217
|
+
*
|
|
1218
|
+
* - DecodeVersionAndOwner
|
|
1219
|
+
* - DecodeError or decodeWriteKeys (depending on context)
|
|
1220
|
+
* - DecodeMessages
|
|
1221
|
+
* - DecodeRanges
|
|
1350
1222
|
*
|
|
1351
|
-
*
|
|
1223
|
+
* If you want to help, please contribute to this function.
|
|
1352
1224
|
*/
|
|
1353
|
-
export const
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
//
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
result |= BigInt(byte & 127) << shift;
|
|
1361
|
-
if ((byte & 128) === 0)
|
|
1362
|
-
break;
|
|
1363
|
-
shift += 7n;
|
|
1364
|
-
}
|
|
1365
|
-
const int = NonNegativeInt.fromUnknown(globalThis.Number(result));
|
|
1366
|
-
if (!int.ok)
|
|
1367
|
-
throw new ProtocolDecodeError(int.error.type);
|
|
1368
|
-
return int.value;
|
|
1369
|
-
};
|
|
1370
|
-
export const encodeLength = (buffer, value) => {
|
|
1371
|
-
encodeNonNegativeInt(buffer, NonNegativeInt.orThrow(value.length));
|
|
1372
|
-
};
|
|
1373
|
-
export const decodeLength = decodeNonNegativeInt;
|
|
1374
|
-
export const encodeString = (buffer, value) => {
|
|
1375
|
-
const bytes = utf8ToBytes(value);
|
|
1376
|
-
encodeLength(buffer, bytes);
|
|
1377
|
-
buffer.extend(bytes);
|
|
1378
|
-
};
|
|
1379
|
-
export const decodeString = (buffer) => {
|
|
1380
|
-
const length = decodeLength(buffer);
|
|
1381
|
-
const bytes = buffer.shiftN(length);
|
|
1382
|
-
return bytesToUtf8(bytes);
|
|
1383
|
-
};
|
|
1384
|
-
export const encodeNodeId = (buffer, nodeId) => {
|
|
1385
|
-
buffer.extend(hexToBytes(nodeId));
|
|
1386
|
-
};
|
|
1387
|
-
export const decodeNodeId = (buffer) => {
|
|
1388
|
-
const bytes = buffer.shiftN(NonNegativeInt.orThrow(8));
|
|
1389
|
-
return bytesToHex(bytes);
|
|
1225
|
+
export const decodeProtocolMessageToJson = (_protocolMessage, _isInitiator) => {
|
|
1226
|
+
// TODO: Implement using
|
|
1227
|
+
// - decodeVersionAndOwner
|
|
1228
|
+
// -- decodeError or decodeWriteKeys (should be refactored out),
|
|
1229
|
+
// -- decodeMessages, and decodeRanges.
|
|
1230
|
+
// This is a stub for PRs and community contributions.
|
|
1231
|
+
throw new Error("decodeProtocolMessageToJson is not implemented yet.");
|
|
1390
1232
|
};
|
|
1391
1233
|
// Small ints are encoded into ProtocolValueType, saving one byte per int.
|
|
1392
|
-
const isSmallInt = (value) => value >= 0 && value < 20;
|
|
1393
1234
|
export const ProtocolValueType = {
|
|
1394
1235
|
// 0-19 small ints
|
|
1395
1236
|
// SQLite types
|
|
@@ -1479,7 +1320,7 @@ export const encodeSqliteValue = (buffer, value) => {
|
|
|
1479
1320
|
case "number": {
|
|
1480
1321
|
// Negative zero is a non-negative integer in JavaScript, but integer
|
|
1481
1322
|
// encoding would lose its observable sign.
|
|
1482
|
-
if (!
|
|
1323
|
+
if (!Object.is(value, -0) && NonNegativeInt.is(value)) {
|
|
1483
1324
|
if (isSmallInt(value)) {
|
|
1484
1325
|
encodeNonNegativeInt(buffer, value);
|
|
1485
1326
|
return;
|
|
@@ -1519,9 +1360,9 @@ export const decodeSqliteValue = (buffer) => {
|
|
|
1519
1360
|
return decodeNonNegativeInt(buffer);
|
|
1520
1361
|
case ProtocolValueType.Json: {
|
|
1521
1362
|
const length = decodeLength(buffer);
|
|
1522
|
-
const
|
|
1523
|
-
const value = decodeJsonValue(
|
|
1524
|
-
if (
|
|
1363
|
+
const jsonValueBuffer = createBuffer(buffer.shiftN(length));
|
|
1364
|
+
const value = decodeJsonValue(jsonValueBuffer);
|
|
1365
|
+
if (jsonValueBuffer.getLength() !== 0) {
|
|
1525
1366
|
throw new ProtocolDecodeError("Invalid JSON MessagePack length");
|
|
1526
1367
|
}
|
|
1527
1368
|
return JSON.stringify(value);
|
|
@@ -1547,23 +1388,5 @@ export const decodeSqliteValue = (buffer) => {
|
|
|
1547
1388
|
throw new ProtocolDecodeError("invalid ProtocolValueType");
|
|
1548
1389
|
}
|
|
1549
1390
|
};
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
*
|
|
1553
|
-
* Note: This is a stub for future implementation. It should use:
|
|
1554
|
-
*
|
|
1555
|
-
* - DecodeVersionAndOwner
|
|
1556
|
-
* - DecodeError or decodeWriteKeys (depending on context)
|
|
1557
|
-
* - DecodeMessages
|
|
1558
|
-
* - DecodeRanges
|
|
1559
|
-
*
|
|
1560
|
-
* If you want to help, please contribute to this function.
|
|
1561
|
-
*/
|
|
1562
|
-
export const decodeProtocolMessageToJson = (_protocolMessage, _isInitiator) => {
|
|
1563
|
-
// TODO: Implement using
|
|
1564
|
-
// - decodeVersionAndOwner
|
|
1565
|
-
// -- decodeError or decodeWriteKeys (should be refactored out),
|
|
1566
|
-
// -- decodeMessages, and decodeRanges.
|
|
1567
|
-
// This is a stub for PRs and community contributions.
|
|
1568
|
-
throw new Error("decodeProtocolMessageToJson is not implemented yet.");
|
|
1569
|
-
};
|
|
1391
|
+
const jsonBuffer = createBuffer();
|
|
1392
|
+
const isSmallInt = (value) => value >= 0 && value < 20;
|
|
@@ -33,11 +33,11 @@ export type { NotNull as KyselyNotNull } from "kysely";
|
|
|
33
33
|
*
|
|
34
34
|
* type AllTodosRow = typeof allTodos.Row;
|
|
35
35
|
* assertType<
|
|
36
|
-
* true,
|
|
37
|
-
*
|
|
36
|
+
* typeof allTodos extends Query<typeof Schema> ? true : false,
|
|
37
|
+
* true
|
|
38
38
|
* >();
|
|
39
|
-
* assertType<
|
|
40
|
-
* assertType<
|
|
39
|
+
* assertType<AllTodosRow["id"], TodoId>();
|
|
40
|
+
* assertType<AllTodosRow["title"], NonEmptyTrimmedString100 | null>();
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
43
|
export type Query<S extends EvoluSchema = EvoluSchema, R extends Row = Row> = SqliteQueryString & Brand<"Query"> & {
|
|
@@ -66,7 +66,7 @@ export type Query<S extends EvoluSchema = EvoluSchema, R extends Row = Row> = Sq
|
|
|
66
66
|
* );
|
|
67
67
|
*
|
|
68
68
|
* type AllTodosRow = typeof allTodos.Row;
|
|
69
|
-
* assertType<InferRow<typeof allTodos
|
|
69
|
+
* assertType<AllTodosRow, InferRow<typeof allTodos>>();
|
|
70
70
|
* ```
|
|
71
71
|
*/
|
|
72
72
|
Row: R;
|
|
@@ -121,11 +121,11 @@ export interface Row {
|
|
|
121
121
|
* );
|
|
122
122
|
*
|
|
123
123
|
* assertType<
|
|
124
|
+
* (typeof people.Row.pets)[number],
|
|
124
125
|
* {
|
|
125
126
|
* petId: typeof Schema.pet.id.Output;
|
|
126
127
|
* name: NonEmptyTrimmedString100 | null;
|
|
127
|
-
* }
|
|
128
|
-
* (typeof people.Row.pets)[number]
|
|
128
|
+
* }
|
|
129
129
|
* >();
|
|
130
130
|
* ```
|
|
131
131
|
*/
|
|
@@ -180,11 +180,11 @@ export declare const evoluJsonArrayFrom: <O>(expr: SelectQueryBuilderExpression<
|
|
|
180
180
|
* );
|
|
181
181
|
*
|
|
182
182
|
* assertType<
|
|
183
|
+
* typeof people.Row.favoritePet,
|
|
183
184
|
* {
|
|
184
185
|
* petId: typeof Schema.pet.id.Output;
|
|
185
186
|
* name: NonEmptyTrimmedString100 | null;
|
|
186
|
-
* } | null
|
|
187
|
-
* typeof people.Row.favoritePet
|
|
187
|
+
* } | null
|
|
188
188
|
* >();
|
|
189
189
|
* ```
|
|
190
190
|
*/
|
|
@@ -234,12 +234,12 @@ export declare const evoluJsonObjectFrom: <O>(expr: SelectQueryBuilderExpression
|
|
|
234
234
|
* );
|
|
235
235
|
*
|
|
236
236
|
* assertType<
|
|
237
|
+
* typeof people.Row.name,
|
|
237
238
|
* {
|
|
238
239
|
* first: NonEmptyTrimmedString100 | null;
|
|
239
240
|
* last: NonEmptyTrimmedString100 | null;
|
|
240
241
|
* full: string;
|
|
241
|
-
* }
|
|
242
|
-
* typeof people.Row.name
|
|
242
|
+
* }
|
|
243
243
|
* >();
|
|
244
244
|
* ```
|
|
245
245
|
*/
|
|
@@ -56,11 +56,11 @@ export { sql as kyselySql } from "kysely";
|
|
|
56
56
|
* );
|
|
57
57
|
*
|
|
58
58
|
* assertType<
|
|
59
|
+
* (typeof people.Row.pets)[number],
|
|
59
60
|
* {
|
|
60
61
|
* petId: typeof Schema.pet.id.Output;
|
|
61
62
|
* name: NonEmptyTrimmedString100 | null;
|
|
62
|
-
* }
|
|
63
|
-
* (typeof people.Row.pets)[number]
|
|
63
|
+
* }
|
|
64
64
|
* >();
|
|
65
65
|
* ```
|
|
66
66
|
*/
|
|
@@ -115,11 +115,11 @@ export const evoluJsonArrayFrom = (expr) => kyselySqlBuilder `(select ${kyselySq
|
|
|
115
115
|
* );
|
|
116
116
|
*
|
|
117
117
|
* assertType<
|
|
118
|
+
* typeof people.Row.favoritePet,
|
|
118
119
|
* {
|
|
119
120
|
* petId: typeof Schema.pet.id.Output;
|
|
120
121
|
* name: NonEmptyTrimmedString100 | null;
|
|
121
|
-
* } | null
|
|
122
|
-
* typeof people.Row.favoritePet
|
|
122
|
+
* } | null
|
|
123
123
|
* >();
|
|
124
124
|
* ```
|
|
125
125
|
*/
|
|
@@ -169,12 +169,12 @@ export const evoluJsonObjectFrom = (expr) => kyselySqlBuilder `(select ${kyselyS
|
|
|
169
169
|
* );
|
|
170
170
|
*
|
|
171
171
|
* assertType<
|
|
172
|
+
* typeof people.Row.name,
|
|
172
173
|
* {
|
|
173
174
|
* first: NonEmptyTrimmedString100 | null;
|
|
174
175
|
* last: NonEmptyTrimmedString100 | null;
|
|
175
176
|
* full: string;
|
|
176
|
-
* }
|
|
177
|
-
* typeof people.Row.name
|
|
177
|
+
* }
|
|
178
178
|
* >();
|
|
179
179
|
* ```
|
|
180
180
|
*/
|
|
@@ -79,8 +79,8 @@ export interface RelayConfig extends StorageConfig {
|
|
|
79
79
|
* },
|
|
80
80
|
* );
|
|
81
81
|
* assertType<
|
|
82
|
-
* true,
|
|
83
|
-
*
|
|
82
|
+
* typeof createTodoEvolu extends AnyTask ? true : false,
|
|
83
|
+
* true
|
|
84
84
|
* >();
|
|
85
85
|
*
|
|
86
86
|
* // Relay: accept owners allowed by the app's access policy.
|
|
@@ -62,7 +62,7 @@ export type AnyStandardSchemaV1 = StandardSchemaV1<any, any>;
|
|
|
62
62
|
* const ZodFiniteNumber = z
|
|
63
63
|
* .number()
|
|
64
64
|
* .transform((value): FiniteNumber => value as FiniteNumber);
|
|
65
|
-
* assertType<
|
|
65
|
+
* assertType<z.output<typeof ZodFiniteNumber>, FiniteNumber>();
|
|
66
66
|
*
|
|
67
67
|
* const ZodSchema = {
|
|
68
68
|
* todo: {
|
|
@@ -289,7 +289,6 @@ export declare const evoluSchemaToSqliteSchema: <S extends EvoluSchema>(schema:
|
|
|
289
289
|
*
|
|
290
290
|
* ```ts
|
|
291
291
|
* import {
|
|
292
|
-
* assertTrue,
|
|
293
292
|
* assertType,
|
|
294
293
|
* createQueryBuilder,
|
|
295
294
|
* id,
|
|
@@ -315,14 +314,13 @@ export declare const evoluSchemaToSqliteSchema: <S extends EvoluSchema>(schema:
|
|
|
315
314
|
* );
|
|
316
315
|
*
|
|
317
316
|
* assertType<
|
|
317
|
+
* typeof todosQuery.Row,
|
|
318
318
|
* {
|
|
319
319
|
* id: TodoId;
|
|
320
320
|
* title: NonEmptyTrimmedString100 | null;
|
|
321
321
|
* isCompleted: SqliteBoolean | null;
|
|
322
|
-
* }
|
|
323
|
-
* typeof todosQuery.Row
|
|
322
|
+
* }
|
|
324
323
|
* >();
|
|
325
|
-
* assertTrue(typeof todosQuery === "string");
|
|
326
324
|
* ```
|
|
327
325
|
*/
|
|
328
326
|
export declare const createQueryBuilder: <S extends EvoluSchema>(_schema: S) => CreateQuery<S>;
|