@evolu/common 8.6.2 → 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/Binary.d.ts +254 -0
- package/dist/src/Binary.d.ts.map +1 -0
- package/dist/src/Binary.js +958 -0
- 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 +28 -76
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +45 -229
- 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 -5
- 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/Binary.ts +1377 -0
- 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 +75 -290
- 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 +0 -122
- package/dist/src/Buffer.d.ts.map +0 -1
- package/dist/src/Buffer.js +0 -69
- package/src/Buffer.ts +0 -206
|
@@ -159,18 +159,32 @@
|
|
|
159
159
|
*
|
|
160
160
|
* @module
|
|
161
161
|
*/
|
|
162
|
+
/**
|
|
163
|
+
* TODO:
|
|
164
|
+
*
|
|
165
|
+
* - The client-relay naming convention in functions like
|
|
166
|
+
* `applyProtocolMessageAsClient` and `applyProtocolMessageAsRelay` is not
|
|
167
|
+
* ideal. In the future, clients will be able to sync directly with each other
|
|
168
|
+
* (P2P), making the current naming misleading. Consider using
|
|
169
|
+
* initiator/non-initiator terminology instead, and consolidate into a single
|
|
170
|
+
* `applyProtocolMessage` function with conditional arguments to reduce code
|
|
171
|
+
* duplication.
|
|
172
|
+
* - Allow clients to broadcast messages that are not persisted by relays. This
|
|
173
|
+
* would enable real-time ephemeral data (like cursor positions, typing
|
|
174
|
+
* indicators) to be forwarded by relays without storage overhead.
|
|
175
|
+
*/
|
|
162
176
|
import { type NonEmptyReadonlyArray } from "../Array.ts";
|
|
163
177
|
import type { Brand } from "../Brand.ts";
|
|
164
|
-
import { type Buffer } from "../
|
|
178
|
+
import { type Buffer } from "../Binary.ts";
|
|
165
179
|
import type { ConsoleDep } from "../Console.ts";
|
|
166
180
|
import { type DecryptWithXChaCha20Poly1305Error, EncryptionKey, type RandomBytesDep } from "../Crypto.ts";
|
|
167
181
|
import { type Result } from "../Result.ts";
|
|
168
|
-
import { SqliteValue } from "../Sqlite.ts";
|
|
182
|
+
import type { SqliteValue } from "../Sqlite.ts";
|
|
169
183
|
import { type Task } from "../Task.ts";
|
|
170
|
-
import {
|
|
184
|
+
import { NonNegativeInt, PositiveInt, type Typed } from "../Type.ts";
|
|
171
185
|
import { type Owner, type OwnerError, OwnerId, OwnerWriteKey } from "./Owner.ts";
|
|
172
186
|
import { type BaseRange, type CrdtMessage, DbChange, type EncryptedCrdtMessage, type EncryptedDbChange, type FingerprintRange, RangeType, type SkipRange, type StorageDep } from "./Storage.ts";
|
|
173
|
-
import {
|
|
187
|
+
import { Timestamp } from "./Timestamp.ts";
|
|
174
188
|
/**
|
|
175
189
|
* Protocol message maximum size.
|
|
176
190
|
*
|
|
@@ -275,7 +289,7 @@ export interface ProtocolVersionError extends OwnerError, Typed<"ProtocolVersion
|
|
|
275
289
|
}
|
|
276
290
|
/** Error for invalid or corrupted protocol message data. */
|
|
277
291
|
export interface ProtocolInvalidDataError extends Typed<"ProtocolInvalidDataError"> {
|
|
278
|
-
readonly data:
|
|
292
|
+
readonly data: Uint8Array;
|
|
279
293
|
readonly error: unknown;
|
|
280
294
|
}
|
|
281
295
|
/** Error when a {@link OwnerWriteKey} is invalid, missing, or fails validation. */
|
|
@@ -411,49 +425,6 @@ export interface ApplyProtocolMessageAsRelayResult extends Typed<"Response"> {
|
|
|
411
425
|
export declare const applyProtocolMessageAsRelay: (inputMessage: Uint8Array, options?: ApplyProtocolMessageAsRelayOptions,
|
|
412
426
|
/** For tests only. */
|
|
413
427
|
version?: NonNegativeInt) => Task<ApplyProtocolMessageAsRelayResult, ProtocolInvalidDataError, StorageDep>;
|
|
414
|
-
export declare const decodeRle: <T>(buffer: Buffer, length: NonNegativeInt, decodeValue: () => T) => ReadonlyArray<T>;
|
|
415
|
-
/**
|
|
416
|
-
* Evolu uses MessagePack to handle finite numbers except for NonNegativeInt.
|
|
417
|
-
* For NonNegativeInt, Evolu provides more efficient encoding.
|
|
418
|
-
*/
|
|
419
|
-
export declare const encodeNumber: (buffer: Buffer, number: FiniteNumber) => void;
|
|
420
|
-
export declare const decodeNumber: (buffer: Buffer) => FiniteNumber;
|
|
421
|
-
/**
|
|
422
|
-
* Encodes an array of boolean flags into a single byte.
|
|
423
|
-
*
|
|
424
|
-
* Each element in the array corresponds to a bit (0-7). Array can have 0-8
|
|
425
|
-
* elements.
|
|
426
|
-
*
|
|
427
|
-
* ### Example
|
|
428
|
-
*
|
|
429
|
-
* ```ts
|
|
430
|
-
* import { assertEqual, createBuffer } from "@evolu/common";
|
|
431
|
-
* import { encodeFlags } from "@evolu/common/local-first";
|
|
432
|
-
*
|
|
433
|
-
* const buffer = createBuffer();
|
|
434
|
-
* encodeFlags(buffer, [true, false, true]);
|
|
435
|
-
*
|
|
436
|
-
* assertEqual(buffer.unwrap(), new Uint8Array([0b101]));
|
|
437
|
-
* ```
|
|
438
|
-
*/
|
|
439
|
-
export declare const encodeFlags: (buffer: Buffer, flags: ReadonlyArray<boolean>) => void;
|
|
440
|
-
/**
|
|
441
|
-
* Decodes a byte into an array of boolean flags.
|
|
442
|
-
*
|
|
443
|
-
* ### Example
|
|
444
|
-
*
|
|
445
|
-
* ```ts
|
|
446
|
-
* import { assertEqual, createBuffer, PositiveInt } from "@evolu/common";
|
|
447
|
-
* import { decodeFlags } from "@evolu/common/local-first";
|
|
448
|
-
*
|
|
449
|
-
* const buffer = createBuffer([0b101]);
|
|
450
|
-
* const flags = decodeFlags(buffer, PositiveInt.orThrow(3));
|
|
451
|
-
*
|
|
452
|
-
* assertEqual(flags, [true, false, true]);
|
|
453
|
-
* assertEqual(buffer.getLength(), 0);
|
|
454
|
-
* ```
|
|
455
|
-
*/
|
|
456
|
-
export declare const decodeFlags: (buffer: Buffer, count: PositiveInt) => ReadonlyArray<boolean>;
|
|
457
428
|
/**
|
|
458
429
|
* Encodes and encrypts a {@link DbChange} using the provided owner's encryption
|
|
459
430
|
* key. Returns an encrypted binary representation as {@link EncryptedDbChange}.
|
|
@@ -470,24 +441,18 @@ export declare const encodeAndEncryptDbChange: (deps: RandomBytesDep) => (messag
|
|
|
470
441
|
*/
|
|
471
442
|
export declare const decryptAndDecodeDbChange: (message: EncryptedCrdtMessage, key: EncryptionKey) => Result<DbChange, DecryptWithXChaCha20Poly1305Error | ProtocolInvalidDataError | ProtocolTimestampMismatchError>;
|
|
472
443
|
/**
|
|
473
|
-
*
|
|
474
|
-
* more efficient than encoding via {@link encodeNumber}.
|
|
444
|
+
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
475
445
|
*
|
|
476
|
-
*
|
|
477
|
-
*/
|
|
478
|
-
export declare const encodeNonNegativeInt: (buffer: Buffer, int: NonNegativeInt) => void;
|
|
479
|
-
/**
|
|
480
|
-
* Decodes a non-negative integer from a variable-length integer format.
|
|
446
|
+
* Note: This is a stub for future implementation. It should use:
|
|
481
447
|
*
|
|
482
|
-
*
|
|
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.
|
|
483
454
|
*/
|
|
484
|
-
export declare const
|
|
485
|
-
export declare const encodeLength: (buffer: Buffer, value: ArrayLike<any>) => void;
|
|
486
|
-
export declare const decodeLength: typeof decodeNonNegativeInt;
|
|
487
|
-
export declare const encodeString: (buffer: Buffer, value: string) => void;
|
|
488
|
-
export declare const decodeString: (buffer: Buffer) => string;
|
|
489
|
-
export declare const encodeNodeId: (buffer: Buffer, nodeId: NodeId) => void;
|
|
490
|
-
export declare const decodeNodeId: (buffer: Buffer) => NodeId;
|
|
455
|
+
export declare const decodeProtocolMessageToJson: (_protocolMessage: ProtocolMessage, _isInitiator: boolean) => unknown;
|
|
491
456
|
export declare const ProtocolValueType: {
|
|
492
457
|
readonly String: number & Brand<"NonNaN"> & Brand<"Finite"> & Brand<"Int"> & Brand<"NonNegative">;
|
|
493
458
|
readonly Number: number & Brand<"NonNaN"> & Brand<"Finite"> & Brand<"Int"> & Brand<"NonNegative">;
|
|
@@ -503,18 +468,5 @@ export declare const ProtocolValueType: {
|
|
|
503
468
|
};
|
|
504
469
|
export declare const encodeSqliteValue: (buffer: Buffer, value: SqliteValue) => void;
|
|
505
470
|
export declare const decodeSqliteValue: (buffer: Buffer) => SqliteValue;
|
|
506
|
-
/**
|
|
507
|
-
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
508
|
-
*
|
|
509
|
-
* Note: This is a stub for future implementation. It should use:
|
|
510
|
-
*
|
|
511
|
-
* - DecodeVersionAndOwner
|
|
512
|
-
* - DecodeError or decodeWriteKeys (depending on context)
|
|
513
|
-
* - DecodeMessages
|
|
514
|
-
* - DecodeRanges
|
|
515
|
-
*
|
|
516
|
-
* If you want to help, please contribute to this function.
|
|
517
|
-
*/
|
|
518
|
-
export declare const decodeProtocolMessageToJson: (_protocolMessage: ProtocolMessage, _isInitiator: boolean) => unknown;
|
|
519
471
|
export {};
|
|
520
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;
|
|
1
|
+
{"version":3,"file":"Protocol.d.ts","sourceRoot":"","sources":["../../../src/local-first/Protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAO,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"}
|
|
@@ -169,35 +169,24 @@
|
|
|
169
169
|
* initiator/non-initiator terminology instead, and consolidate into a single
|
|
170
170
|
* `applyProtocolMessage` function with conditional arguments to reduce code
|
|
171
171
|
* duplication.
|
|
172
|
-
* - Replace try-catch with Result + new Error (to preserve stacktraces). Measure
|
|
173
|
-
* Result overhead, it should be super small.
|
|
174
172
|
* - Allow clients to broadcast messages that are not persisted by relays. This
|
|
175
173
|
* would enable real-time ephemeral data (like cursor positions, typing
|
|
176
174
|
* indicators) to be forwarded by relays without storage overhead.
|
|
177
175
|
*/
|
|
178
|
-
import { Packr } from "msgpackr";
|
|
179
176
|
import { createMutableArray, isNonEmptyArray, } from "../Array.js";
|
|
180
|
-
import { assert, assertNonNullable, assertNotUndefined } from "../Assert.js";
|
|
181
|
-
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";
|
|
182
179
|
import { createPadmePadding, decryptWithXChaCha20Poly1305, EncryptionKey, encryptWithXChaCha20Poly1305, Entropy24, XChaCha20Poly1305Ciphertext, xChaCha20Poly1305NonceLength, } from "../Crypto.js";
|
|
183
180
|
import { eqArrayNumber } from "../Eq.js";
|
|
184
181
|
import { computeBalancedBuckets } from "../Number.js";
|
|
185
182
|
import { createMutableRecord, objectToEntries } from "../Object.js";
|
|
186
183
|
import { err, ok } from "../Result.js";
|
|
187
|
-
import { SqliteValue } from "../Sqlite.js";
|
|
188
184
|
import { AbortError } from "../Task.js";
|
|
189
185
|
import { Millis } from "../Time.js";
|
|
190
|
-
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";
|
|
191
187
|
import { OwnerId, OwnerIdBytes, ownerIdToOwnerIdBytes, OwnerWriteKey, ownerWriteKeyLength, } from "./Owner.js";
|
|
192
188
|
import { DbChange, fingerprintSize, InfiniteUpperBound, RangeType, } from "./Storage.js";
|
|
193
|
-
import { Counter, eqTimestamp, NodeId, Timestamp, TimestampBytes, timestampBytesLength, timestampBytesToTimestamp, timestampToTimestampBytes, } from "./Timestamp.js";
|
|
194
|
-
/**
|
|
195
|
-
* Evolu uses MessagePack for numbers and JSONs.
|
|
196
|
-
*
|
|
197
|
-
* - `variableMapSize: true` - More compact maps, ~5-10% slower encoding
|
|
198
|
-
* - `useRecords: false` - Standard MessagePack without extensions
|
|
199
|
-
*/
|
|
200
|
-
const packr = new Packr({ variableMapSize: true, useRecords: false });
|
|
189
|
+
import { Counter, eqTimestamp, NodeId, nodeIdBytesLength, nodeIdBytesToNodeId, nodeIdToNodeIdBytes, Timestamp, TimestampBytes, timestampBytesLength, timestampBytesToTimestamp, timestampToTimestampBytes, } from "./Timestamp.js";
|
|
201
190
|
const minProtocolMessageMaxSize = 1_000_000;
|
|
202
191
|
const maxProtocolMessageMaxSize = 100_000_000;
|
|
203
192
|
/**
|
|
@@ -506,12 +495,12 @@ export const createTimestampsBuffer = () => {
|
|
|
506
495
|
encodeNonNegativeInt(buffer, value);
|
|
507
496
|
});
|
|
508
497
|
const nodeIdEncoder = createRunLengthEncoder((buffer, value) => {
|
|
509
|
-
|
|
498
|
+
buffer.extend(nodeIdToNodeIdBytes(value));
|
|
510
499
|
});
|
|
511
500
|
return {
|
|
512
501
|
add: (timestamp) => {
|
|
513
502
|
const delta = timestamp.millis - previousMillis;
|
|
514
|
-
|
|
503
|
+
assertType(NonNegativeInt, delta);
|
|
515
504
|
count++;
|
|
516
505
|
syncCount();
|
|
517
506
|
previousMillis = timestamp.millis;
|
|
@@ -536,29 +525,6 @@ export const createTimestampsBuffer = () => {
|
|
|
536
525
|
},
|
|
537
526
|
};
|
|
538
527
|
};
|
|
539
|
-
const createRunLengthEncoder = (encodeValue) => {
|
|
540
|
-
const buffer = createBuffer();
|
|
541
|
-
let previousLength = zeroNonNegativeInt;
|
|
542
|
-
let previousValue = null;
|
|
543
|
-
let runLength = zeroNonNegativeInt;
|
|
544
|
-
return {
|
|
545
|
-
add: (value) => {
|
|
546
|
-
if (value === previousValue) {
|
|
547
|
-
runLength++;
|
|
548
|
-
buffer.truncate(previousLength);
|
|
549
|
-
}
|
|
550
|
-
else {
|
|
551
|
-
previousValue = value;
|
|
552
|
-
runLength = NonNegativeInt.orThrow(1);
|
|
553
|
-
}
|
|
554
|
-
previousLength = buffer.getLength();
|
|
555
|
-
encodeValue(buffer, value);
|
|
556
|
-
encodeNonNegativeInt(buffer, runLength);
|
|
557
|
-
},
|
|
558
|
-
getLength: () => buffer.getLength(),
|
|
559
|
-
unwrap: () => buffer.unwrap(),
|
|
560
|
-
};
|
|
561
|
-
};
|
|
562
528
|
export const applyProtocolMessageAsClient = (inputMessage, options = {}) => async (run) => {
|
|
563
529
|
const { storage } = run.deps;
|
|
564
530
|
try {
|
|
@@ -678,7 +644,7 @@ version = protocolVersion) => async (run) => {
|
|
|
678
644
|
});
|
|
679
645
|
}
|
|
680
646
|
const messageType = input.shift();
|
|
681
|
-
|
|
647
|
+
assertSame(messageType, MessageType.Request);
|
|
682
648
|
const hasWriteKey = input.shift();
|
|
683
649
|
let writeKey;
|
|
684
650
|
if (hasWriteKey === 1) {
|
|
@@ -1139,7 +1105,7 @@ const decodeTimestamps = (buffer, length) => {
|
|
|
1139
1105
|
throw new ProtocolDecodeError(counter.error.type);
|
|
1140
1106
|
return counter.value;
|
|
1141
1107
|
});
|
|
1142
|
-
const nodeIds = decodeRle(buffer, length, () =>
|
|
1108
|
+
const nodeIds = decodeRle(buffer, length, () => nodeIdBytesToNodeId(buffer.shiftN(nodeIdBytesLength)));
|
|
1143
1109
|
const timestamps = createMutableArray(length);
|
|
1144
1110
|
for (let i = 0; i < length; i++) {
|
|
1145
1111
|
timestamps[i] = {
|
|
@@ -1150,108 +1116,10 @@ const decodeTimestamps = (buffer, length) => {
|
|
|
1150
1116
|
}
|
|
1151
1117
|
return timestamps;
|
|
1152
1118
|
};
|
|
1153
|
-
export const decodeRle = (buffer, length, decodeValue) => {
|
|
1154
|
-
const values = createMutableArray(length);
|
|
1155
|
-
let index = 0;
|
|
1156
|
-
while (index < length) {
|
|
1157
|
-
const value = decodeValue();
|
|
1158
|
-
const runLength = decodeNonNegativeInt(buffer);
|
|
1159
|
-
// Prevent infinite loop on malformed input.
|
|
1160
|
-
if (runLength === 0) {
|
|
1161
|
-
throw new ProtocolDecodeError("Invalid RLE encoding: runLength must be positive");
|
|
1162
|
-
}
|
|
1163
|
-
const remaining = length - index;
|
|
1164
|
-
// Prevent CPU/memory amplification via oversized runLength.
|
|
1165
|
-
if (runLength > remaining) {
|
|
1166
|
-
throw new ProtocolDecodeError(`Invalid RLE encoding: runLength ${runLength} exceeds remaining ${remaining}`);
|
|
1167
|
-
}
|
|
1168
|
-
for (let i = 0; i < runLength; i++) {
|
|
1169
|
-
values[index] = value;
|
|
1170
|
-
index++;
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
return values;
|
|
1174
|
-
};
|
|
1175
1119
|
const decodeId = (buffer) => {
|
|
1176
1120
|
const bytes = buffer.shiftN(idBytesTypeValueLength);
|
|
1177
1121
|
return idBytesToId(bytes);
|
|
1178
1122
|
};
|
|
1179
|
-
/**
|
|
1180
|
-
* Evolu uses MessagePack to handle finite numbers except for NonNegativeInt.
|
|
1181
|
-
* For NonNegativeInt, Evolu provides more efficient encoding.
|
|
1182
|
-
*/
|
|
1183
|
-
export const encodeNumber = (buffer, number) => {
|
|
1184
|
-
buffer.extend(packr.pack(number));
|
|
1185
|
-
};
|
|
1186
|
-
export const decodeNumber = (buffer) => {
|
|
1187
|
-
let number;
|
|
1188
|
-
let end;
|
|
1189
|
-
packr.unpackMultiple(buffer.unwrap(), (n, _, e) => {
|
|
1190
|
-
number = n;
|
|
1191
|
-
end = e;
|
|
1192
|
-
return false;
|
|
1193
|
-
});
|
|
1194
|
-
const endResult = NonNegativeInt.fromUnknown(end);
|
|
1195
|
-
if (!endResult.ok)
|
|
1196
|
-
throw new ProtocolDecodeError(endResult.error.type);
|
|
1197
|
-
const numberResult = FiniteNumber.fromUnknown(number);
|
|
1198
|
-
if (!numberResult.ok)
|
|
1199
|
-
throw new ProtocolDecodeError(numberResult.error.type);
|
|
1200
|
-
buffer.shiftN(endResult.value);
|
|
1201
|
-
return numberResult.value;
|
|
1202
|
-
};
|
|
1203
|
-
/**
|
|
1204
|
-
* Encodes an array of boolean flags into a single byte.
|
|
1205
|
-
*
|
|
1206
|
-
* Each element in the array corresponds to a bit (0-7). Array can have 0-8
|
|
1207
|
-
* elements.
|
|
1208
|
-
*
|
|
1209
|
-
* ### Example
|
|
1210
|
-
*
|
|
1211
|
-
* ```ts
|
|
1212
|
-
* import { assertEqual, createBuffer } from "@evolu/common";
|
|
1213
|
-
* import { encodeFlags } from "@evolu/common/local-first";
|
|
1214
|
-
*
|
|
1215
|
-
* const buffer = createBuffer();
|
|
1216
|
-
* encodeFlags(buffer, [true, false, true]);
|
|
1217
|
-
*
|
|
1218
|
-
* assertEqual(buffer.unwrap(), new Uint8Array([0b101]));
|
|
1219
|
-
* ```
|
|
1220
|
-
*/
|
|
1221
|
-
export const encodeFlags = (buffer, flags) => {
|
|
1222
|
-
let byte = 0;
|
|
1223
|
-
for (let i = 0; i < flags.length && i < 8; i++) {
|
|
1224
|
-
if (flags[i]) {
|
|
1225
|
-
byte |= 1 << i;
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1228
|
-
buffer.extend([byte]);
|
|
1229
|
-
};
|
|
1230
|
-
/**
|
|
1231
|
-
* Decodes a byte into an array of boolean flags.
|
|
1232
|
-
*
|
|
1233
|
-
* ### Example
|
|
1234
|
-
*
|
|
1235
|
-
* ```ts
|
|
1236
|
-
* import { assertEqual, createBuffer, PositiveInt } from "@evolu/common";
|
|
1237
|
-
* import { decodeFlags } from "@evolu/common/local-first";
|
|
1238
|
-
*
|
|
1239
|
-
* const buffer = createBuffer([0b101]);
|
|
1240
|
-
* const flags = decodeFlags(buffer, PositiveInt.orThrow(3));
|
|
1241
|
-
*
|
|
1242
|
-
* assertEqual(flags, [true, false, true]);
|
|
1243
|
-
* assertEqual(buffer.getLength(), 0);
|
|
1244
|
-
* ```
|
|
1245
|
-
*/
|
|
1246
|
-
export const decodeFlags = (buffer, count) => {
|
|
1247
|
-
const byte = buffer.shift();
|
|
1248
|
-
const length = globalThis.Math.min(count, 8);
|
|
1249
|
-
const flags = createMutableArray(length);
|
|
1250
|
-
for (let i = 0; i < length; i++) {
|
|
1251
|
-
flags[i] = (byte & (1 << i)) !== 0;
|
|
1252
|
-
}
|
|
1253
|
-
return flags;
|
|
1254
|
-
};
|
|
1255
1123
|
/**
|
|
1256
1124
|
* Encodes and encrypts a {@link DbChange} using the provided owner's encryption
|
|
1257
1125
|
* key. Returns an encrypted binary representation as {@link EncryptedDbChange}.
|
|
@@ -1343,73 +1211,26 @@ export const decryptAndDecodeDbChange = (message, key) => {
|
|
|
1343
1211
|
}
|
|
1344
1212
|
};
|
|
1345
1213
|
/**
|
|
1346
|
-
*
|
|
1347
|
-
* more efficient than encoding via {@link encodeNumber}.
|
|
1214
|
+
* Decodes a ProtocolMessage into a readable JSON object for debugging.
|
|
1348
1215
|
*
|
|
1349
|
-
*
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
}
|
|
1356
|
-
let remaining = BigInt(int);
|
|
1357
|
-
const bytes = [];
|
|
1358
|
-
while (remaining !== 0n) {
|
|
1359
|
-
const byte = globalThis.Number(remaining & 127n);
|
|
1360
|
-
bytes.push(byte);
|
|
1361
|
-
remaining >>= 7n;
|
|
1362
|
-
}
|
|
1363
|
-
for (let i = 0; i < bytes.length - 1; i++) {
|
|
1364
|
-
bytes[i] |= 128;
|
|
1365
|
-
}
|
|
1366
|
-
buffer.extend(bytes);
|
|
1367
|
-
};
|
|
1368
|
-
/**
|
|
1369
|
-
* 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
|
|
1370
1222
|
*
|
|
1371
|
-
*
|
|
1223
|
+
* If you want to help, please contribute to this function.
|
|
1372
1224
|
*/
|
|
1373
|
-
export const
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
//
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
result |= BigInt(byte & 127) << shift;
|
|
1381
|
-
if ((byte & 128) === 0)
|
|
1382
|
-
break;
|
|
1383
|
-
shift += 7n;
|
|
1384
|
-
}
|
|
1385
|
-
const int = NonNegativeInt.fromUnknown(globalThis.Number(result));
|
|
1386
|
-
if (!int.ok)
|
|
1387
|
-
throw new ProtocolDecodeError(int.error.type);
|
|
1388
|
-
return int.value;
|
|
1389
|
-
};
|
|
1390
|
-
export const encodeLength = (buffer, value) => {
|
|
1391
|
-
encodeNonNegativeInt(buffer, NonNegativeInt.orThrow(value.length));
|
|
1392
|
-
};
|
|
1393
|
-
export const decodeLength = decodeNonNegativeInt;
|
|
1394
|
-
export const encodeString = (buffer, value) => {
|
|
1395
|
-
const bytes = utf8ToBytes(value);
|
|
1396
|
-
encodeLength(buffer, bytes);
|
|
1397
|
-
buffer.extend(bytes);
|
|
1398
|
-
};
|
|
1399
|
-
export const decodeString = (buffer) => {
|
|
1400
|
-
const length = decodeLength(buffer);
|
|
1401
|
-
const bytes = buffer.shiftN(length);
|
|
1402
|
-
return bytesToUtf8(bytes);
|
|
1403
|
-
};
|
|
1404
|
-
export const encodeNodeId = (buffer, nodeId) => {
|
|
1405
|
-
buffer.extend(hexToBytes(nodeId));
|
|
1406
|
-
};
|
|
1407
|
-
export const decodeNodeId = (buffer) => {
|
|
1408
|
-
const bytes = buffer.shiftN(NonNegativeInt.orThrow(8));
|
|
1409
|
-
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.");
|
|
1410
1232
|
};
|
|
1411
1233
|
// Small ints are encoded into ProtocolValueType, saving one byte per int.
|
|
1412
|
-
const isSmallInt = (value) => value >= 0 && value < 20;
|
|
1413
1234
|
export const ProtocolValueType = {
|
|
1414
1235
|
// 0-19 small ints
|
|
1415
1236
|
// SQLite types
|
|
@@ -1471,10 +1292,17 @@ export const encodeSqliteValue = (buffer, value) => {
|
|
|
1471
1292
|
// Some valid JSON strings like "-0E0" get normalized to "0" during parsing,
|
|
1472
1293
|
// which would cause data corruption if we don't verify round-trip safety.
|
|
1473
1294
|
if (json.ok && JSON.stringify(jsonToJsonValue(json.value)) === value) {
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1295
|
+
jsonBuffer.reset();
|
|
1296
|
+
try {
|
|
1297
|
+
encodeJsonValue(jsonBuffer, jsonToJsonValue(json.value));
|
|
1298
|
+
const jsonBytes = jsonBuffer.unwrap();
|
|
1299
|
+
encodeNonNegativeInt(buffer, ProtocolValueType.Json);
|
|
1300
|
+
encodeLength(buffer, jsonBytes);
|
|
1301
|
+
buffer.extend(jsonBytes);
|
|
1302
|
+
}
|
|
1303
|
+
finally {
|
|
1304
|
+
jsonBuffer.reset();
|
|
1305
|
+
}
|
|
1478
1306
|
return;
|
|
1479
1307
|
}
|
|
1480
1308
|
const base64Url = Base64Url.from.parent(value);
|
|
@@ -1490,7 +1318,9 @@ export const encodeSqliteValue = (buffer, value) => {
|
|
|
1490
1318
|
return;
|
|
1491
1319
|
}
|
|
1492
1320
|
case "number": {
|
|
1493
|
-
|
|
1321
|
+
// Negative zero is a non-negative integer in JavaScript, but integer
|
|
1322
|
+
// encoding would lose its observable sign.
|
|
1323
|
+
if (!Object.is(value, -0) && NonNegativeInt.is(value)) {
|
|
1494
1324
|
if (isSmallInt(value)) {
|
|
1495
1325
|
encodeNonNegativeInt(buffer, value);
|
|
1496
1326
|
return;
|
|
@@ -1530,8 +1360,12 @@ export const decodeSqliteValue = (buffer) => {
|
|
|
1530
1360
|
return decodeNonNegativeInt(buffer);
|
|
1531
1361
|
case ProtocolValueType.Json: {
|
|
1532
1362
|
const length = decodeLength(buffer);
|
|
1533
|
-
const
|
|
1534
|
-
|
|
1363
|
+
const jsonValueBuffer = createBuffer(buffer.shiftN(length));
|
|
1364
|
+
const value = decodeJsonValue(jsonValueBuffer);
|
|
1365
|
+
if (jsonValueBuffer.getLength() !== 0) {
|
|
1366
|
+
throw new ProtocolDecodeError("Invalid JSON MessagePack length");
|
|
1367
|
+
}
|
|
1368
|
+
return JSON.stringify(value);
|
|
1535
1369
|
}
|
|
1536
1370
|
case ProtocolValueType.DateIsoWithNonNegativeTime:
|
|
1537
1371
|
case ProtocolValueType.DateIsoWithNegativeTime: {
|
|
@@ -1554,23 +1388,5 @@ export const decodeSqliteValue = (buffer) => {
|
|
|
1554
1388
|
throw new ProtocolDecodeError("invalid ProtocolValueType");
|
|
1555
1389
|
}
|
|
1556
1390
|
};
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
*
|
|
1560
|
-
* Note: This is a stub for future implementation. It should use:
|
|
1561
|
-
*
|
|
1562
|
-
* - DecodeVersionAndOwner
|
|
1563
|
-
* - DecodeError or decodeWriteKeys (depending on context)
|
|
1564
|
-
* - DecodeMessages
|
|
1565
|
-
* - DecodeRanges
|
|
1566
|
-
*
|
|
1567
|
-
* If you want to help, please contribute to this function.
|
|
1568
|
-
*/
|
|
1569
|
-
export const decodeProtocolMessageToJson = (_protocolMessage, _isInitiator) => {
|
|
1570
|
-
// TODO: Implement using
|
|
1571
|
-
// - decodeVersionAndOwner
|
|
1572
|
-
// -- decodeError or decodeWriteKeys (should be refactored out),
|
|
1573
|
-
// -- decodeMessages, and decodeRanges.
|
|
1574
|
-
// This is a stub for PRs and community contributions.
|
|
1575
|
-
throw new Error("decodeProtocolMessageToJson is not implemented yet.");
|
|
1576
|
-
};
|
|
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
|
*/
|