@evolu/common 8.9.0 → 8.11.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/Bytes.d.ts +647 -0
- package/dist/src/Bytes.d.ts.map +1 -0
- package/dist/src/{Binary.js → Bytes.js} +266 -16
- package/dist/src/Config.d.ts +142 -0
- package/dist/src/Config.d.ts.map +1 -0
- package/dist/src/Config.js +181 -0
- package/dist/src/Console.d.ts +62 -7
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +20 -4
- package/dist/src/Crypto.d.ts +76 -4
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +55 -4
- package/dist/src/Error.d.ts +45 -0
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +69 -0
- package/dist/src/Fs.d.ts +376 -0
- package/dist/src/Fs.d.ts.map +1 -0
- package/dist/src/Fs.js +113 -0
- package/dist/src/Identicon.d.ts +2 -2
- package/dist/src/Identicon.js +2 -2
- package/dist/src/LeakDetector.d.ts +22 -3
- package/dist/src/LeakDetector.d.ts.map +1 -1
- package/dist/src/LeakDetector.js +12 -2
- package/dist/src/LockManager.d.ts +8 -0
- package/dist/src/LockManager.d.ts.map +1 -1
- package/dist/src/LockManager.js +6 -0
- package/dist/src/Number.d.ts +50 -7
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +47 -8
- package/dist/src/Object.d.ts +32 -0
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +46 -0
- package/dist/src/Platform.d.ts +47 -7
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +24 -5
- package/dist/src/Random.d.ts +25 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +14 -2
- package/dist/src/Resource.d.ts +156 -1
- package/dist/src/Resource.d.ts.map +1 -1
- package/dist/src/Resource.js +201 -72
- package/dist/src/Schedule.d.ts +11 -10
- package/dist/src/Schedule.d.ts.map +1 -1
- package/dist/src/Schedule.js +1 -1
- package/dist/src/Sqlite.d.ts +132 -16
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +64 -10
- package/dist/src/Task.d.ts +15 -4
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +41 -15
- package/dist/src/Test.d.ts +9 -0
- package/dist/src/Test.d.ts.map +1 -1
- package/dist/src/Test.js +4 -0
- package/dist/src/Time.d.ts +179 -20
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +95 -6
- package/dist/src/Type.d.ts +3056 -1539
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +2548 -584
- package/dist/src/WebSocket.d.ts +164 -13
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +133 -24
- package/dist/src/Worker.d.ts +90 -8
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +28 -2
- package/dist/src/index.d.ts +9 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -4
- package/dist/src/intl/_en.d.ts +24 -1
- package/dist/src/intl/_en.d.ts.map +1 -1
- package/dist/src/intl/_en.js +20 -0
- package/dist/src/intl/ar.d.ts +24 -1
- package/dist/src/intl/ar.d.ts.map +1 -1
- package/dist/src/intl/ar.js +20 -0
- package/dist/src/intl/bn.d.ts +24 -1
- package/dist/src/intl/bn.d.ts.map +1 -1
- package/dist/src/intl/bn.js +20 -0
- package/dist/src/intl/ca.d.ts +24 -1
- package/dist/src/intl/ca.d.ts.map +1 -1
- package/dist/src/intl/ca.js +20 -0
- package/dist/src/intl/cs.d.ts +24 -1
- package/dist/src/intl/cs.d.ts.map +1 -1
- package/dist/src/intl/cs.js +20 -0
- package/dist/src/intl/da.d.ts +24 -1
- package/dist/src/intl/da.d.ts.map +1 -1
- package/dist/src/intl/da.js +20 -0
- package/dist/src/intl/de.d.ts +24 -1
- package/dist/src/intl/de.d.ts.map +1 -1
- package/dist/src/intl/de.js +20 -0
- package/dist/src/intl/el.d.ts +24 -1
- package/dist/src/intl/el.d.ts.map +1 -1
- package/dist/src/intl/el.js +20 -0
- package/dist/src/intl/es.d.ts +24 -1
- package/dist/src/intl/es.d.ts.map +1 -1
- package/dist/src/intl/es.js +20 -0
- package/dist/src/intl/fa.d.ts +24 -1
- package/dist/src/intl/fa.d.ts.map +1 -1
- package/dist/src/intl/fa.js +20 -0
- package/dist/src/intl/fi.d.ts +24 -1
- package/dist/src/intl/fi.d.ts.map +1 -1
- package/dist/src/intl/fi.js +20 -0
- package/dist/src/intl/fil.d.ts +24 -1
- package/dist/src/intl/fil.d.ts.map +1 -1
- package/dist/src/intl/fil.js +20 -0
- package/dist/src/intl/fr.d.ts +24 -1
- package/dist/src/intl/fr.d.ts.map +1 -1
- package/dist/src/intl/fr.js +20 -0
- package/dist/src/intl/he.d.ts +24 -1
- package/dist/src/intl/he.d.ts.map +1 -1
- package/dist/src/intl/he.js +20 -0
- package/dist/src/intl/hi.d.ts +24 -1
- package/dist/src/intl/hi.d.ts.map +1 -1
- package/dist/src/intl/hi.js +20 -0
- package/dist/src/intl/hr.d.ts +24 -1
- package/dist/src/intl/hr.d.ts.map +1 -1
- package/dist/src/intl/hr.js +20 -0
- package/dist/src/intl/hu.d.ts +22 -1
- package/dist/src/intl/hu.d.ts.map +1 -1
- package/dist/src/intl/hu.js +18 -0
- package/dist/src/intl/id.d.ts +24 -1
- package/dist/src/intl/id.d.ts.map +1 -1
- package/dist/src/intl/id.js +20 -0
- package/dist/src/intl/it.d.ts +24 -1
- package/dist/src/intl/it.d.ts.map +1 -1
- package/dist/src/intl/it.js +20 -0
- package/dist/src/intl/ja.d.ts +24 -1
- package/dist/src/intl/ja.d.ts.map +1 -1
- package/dist/src/intl/ja.js +20 -0
- package/dist/src/intl/ko.d.ts +24 -1
- package/dist/src/intl/ko.d.ts.map +1 -1
- package/dist/src/intl/ko.js +20 -0
- package/dist/src/intl/ml.d.ts +24 -1
- package/dist/src/intl/ml.d.ts.map +1 -1
- package/dist/src/intl/ml.js +20 -0
- package/dist/src/intl/mr.d.ts +24 -1
- package/dist/src/intl/mr.d.ts.map +1 -1
- package/dist/src/intl/mr.js +20 -0
- package/dist/src/intl/ms.d.ts +24 -1
- package/dist/src/intl/ms.d.ts.map +1 -1
- package/dist/src/intl/ms.js +20 -0
- package/dist/src/intl/nb.d.ts +22 -1
- package/dist/src/intl/nb.d.ts.map +1 -1
- package/dist/src/intl/nb.js +18 -0
- package/dist/src/intl/nl.d.ts +24 -1
- package/dist/src/intl/nl.d.ts.map +1 -1
- package/dist/src/intl/nl.js +20 -0
- package/dist/src/intl/pa.d.ts +24 -1
- package/dist/src/intl/pa.d.ts.map +1 -1
- package/dist/src/intl/pa.js +20 -0
- package/dist/src/intl/pl.d.ts +23 -0
- package/dist/src/intl/pl.d.ts.map +1 -1
- package/dist/src/intl/pl.js +20 -0
- package/dist/src/intl/pt-BR.d.ts +24 -1
- package/dist/src/intl/pt-BR.d.ts.map +1 -1
- package/dist/src/intl/pt-BR.js +20 -0
- package/dist/src/intl/pt.d.ts +24 -1
- package/dist/src/intl/pt.d.ts.map +1 -1
- package/dist/src/intl/pt.js +20 -0
- package/dist/src/intl/ro.d.ts +24 -1
- package/dist/src/intl/ro.d.ts.map +1 -1
- package/dist/src/intl/ro.js +20 -0
- package/dist/src/intl/sk.d.ts +24 -1
- package/dist/src/intl/sk.d.ts.map +1 -1
- package/dist/src/intl/sk.js +20 -0
- package/dist/src/intl/sl.d.ts +24 -1
- package/dist/src/intl/sl.d.ts.map +1 -1
- package/dist/src/intl/sl.js +20 -0
- package/dist/src/intl/sv.d.ts +24 -1
- package/dist/src/intl/sv.d.ts.map +1 -1
- package/dist/src/intl/sv.js +20 -0
- package/dist/src/intl/sw.d.ts +21 -0
- package/dist/src/intl/sw.d.ts.map +1 -1
- package/dist/src/intl/sw.js +18 -0
- package/dist/src/intl/ta.d.ts +24 -1
- package/dist/src/intl/ta.d.ts.map +1 -1
- package/dist/src/intl/ta.js +20 -0
- package/dist/src/intl/te.d.ts +24 -1
- package/dist/src/intl/te.d.ts.map +1 -1
- package/dist/src/intl/te.js +20 -0
- package/dist/src/intl/th.d.ts +24 -1
- package/dist/src/intl/th.d.ts.map +1 -1
- package/dist/src/intl/th.js +20 -0
- package/dist/src/intl/tr.d.ts +24 -1
- package/dist/src/intl/tr.d.ts.map +1 -1
- package/dist/src/intl/tr.js +20 -0
- package/dist/src/intl/uk.d.ts +80 -57
- package/dist/src/intl/uk.d.ts.map +1 -1
- package/dist/src/intl/uk.js +174 -149
- package/dist/src/intl/ur.d.ts +24 -1
- package/dist/src/intl/ur.d.ts.map +1 -1
- package/dist/src/intl/ur.js +20 -0
- package/dist/src/intl/vi.d.ts +24 -1
- package/dist/src/intl/vi.d.ts.map +1 -1
- package/dist/src/intl/vi.js +20 -0
- package/dist/src/intl/zh-CN.d.ts +24 -1
- package/dist/src/intl/zh-CN.d.ts.map +1 -1
- package/dist/src/intl/zh-CN.js +20 -0
- package/dist/src/intl/zh-TW.d.ts +24 -1
- package/dist/src/intl/zh-TW.d.ts.map +1 -1
- package/dist/src/intl/zh-TW.js +20 -0
- package/dist/src/local-first/Db.d.ts +52 -3
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +412 -137
- package/dist/src/local-first/Evolu.d.ts +336 -211
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +102 -15
- package/dist/src/local-first/Owner.d.ts +13 -30
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +13 -30
- package/dist/src/local-first/Protocol.d.ts +95 -17
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +119 -39
- package/dist/src/local-first/Query.d.ts +8 -15
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Schema.d.ts +345 -21
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +214 -17
- package/dist/src/local-first/Shared.d.ts +537 -22
- package/dist/src/local-first/Shared.d.ts.map +1 -1
- package/dist/src/local-first/Shared.js +1437 -234
- package/dist/src/local-first/Storage.d.ts +192 -14
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +82 -21
- package/dist/src/local-first/Timestamp.d.ts +392 -41
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +404 -82
- package/dist/src/local-first/index.d.ts +0 -1
- package/dist/src/local-first/index.d.ts.map +1 -1
- package/dist/src/local-first/index.js +0 -1
- package/package.json +1 -1
- package/src/Assert.test.ts +2 -5
- package/src/{Binary.test.ts → Bytes.test.ts} +286 -1
- package/src/{Binary.ts → Bytes.ts} +652 -21
- package/src/Config.test.ts +668 -0
- package/src/Config.ts +410 -0
- package/src/Console.ts +62 -7
- package/src/Crypto.ts +76 -4
- package/src/Eq.test.ts +2 -3
- package/src/Error.test.ts +76 -3
- package/src/Error.ts +71 -0
- package/src/Fs.test.ts +105 -0
- package/src/Fs.ts +488 -0
- package/src/Identicon.ts +2 -2
- package/src/LeakDetector.ts +22 -3
- package/src/LockManager.ts +8 -0
- package/src/Number.test.ts +82 -18
- package/src/Number.ts +76 -8
- package/src/Object.test.ts +139 -10
- package/src/Object.ts +49 -0
- package/src/Platform.ts +50 -8
- package/src/Random.ts +25 -2
- package/src/Resource.test.ts +837 -0
- package/src/Resource.ts +235 -15
- package/src/Schedule.test.ts +50 -12
- package/src/Schedule.ts +24 -14
- package/src/Sqlite.ts +138 -18
- package/src/Task.test.ts +189 -8
- package/src/Task.ts +56 -17
- package/src/Test.ts +9 -0
- package/src/Time.test.ts +82 -11
- package/src/Time.ts +246 -24
- package/src/Type.test.ts +3994 -1119
- package/src/Type.ts +7258 -3842
- package/src/Types.test.ts +4 -14
- package/src/WebSocket.ts +313 -40
- package/src/Worker.ts +90 -8
- package/src/index.ts +18 -7
- package/src/intl/_en.ts +70 -0
- package/src/intl/ar.ts +71 -0
- package/src/intl/bn.ts +70 -0
- package/src/intl/ca.ts +70 -0
- package/src/intl/cs.ts +70 -0
- package/src/intl/da.ts +70 -0
- package/src/intl/de.ts +70 -0
- package/src/intl/el.ts +70 -0
- package/src/intl/es.ts +70 -0
- package/src/intl/fa.ts +70 -0
- package/src/intl/fi.ts +70 -0
- package/src/intl/fil.ts +70 -0
- package/src/intl/fr.ts +70 -0
- package/src/intl/he.ts +70 -0
- package/src/intl/hi.ts +70 -0
- package/src/intl/hr.ts +70 -0
- package/src/intl/hu.ts +69 -0
- package/src/intl/id.ts +70 -0
- package/src/intl/intl.test.ts +819 -1
- package/src/intl/it.ts +70 -0
- package/src/intl/ja.ts +70 -0
- package/src/intl/ko.ts +68 -0
- package/src/intl/ml.ts +70 -0
- package/src/intl/mr.ts +70 -0
- package/src/intl/ms.ts +71 -0
- package/src/intl/nb.ts +69 -0
- package/src/intl/nl.ts +70 -0
- package/src/intl/pa.ts +70 -0
- package/src/intl/pl.ts +63 -0
- package/src/intl/pt-BR.ts +70 -0
- package/src/intl/pt.ts +71 -0
- package/src/intl/ro.ts +70 -0
- package/src/intl/sk.ts +71 -0
- package/src/intl/sl.ts +70 -0
- package/src/intl/sv.ts +70 -0
- package/src/intl/sw.ts +62 -0
- package/src/intl/ta.ts +70 -0
- package/src/intl/te.ts +70 -0
- package/src/intl/th.ts +68 -0
- package/src/intl/tr.ts +70 -0
- package/src/intl/uk.ts +228 -155
- package/src/intl/ur.ts +70 -0
- package/src/intl/vi.ts +70 -0
- package/src/intl/zh-CN.ts +68 -0
- package/src/intl/zh-TW.ts +68 -0
- package/src/local-first/Db.ts +644 -339
- package/src/local-first/Evolu.test.ts +686 -21
- package/src/local-first/Evolu.ts +450 -228
- package/src/local-first/Owner.ts +13 -30
- package/src/local-first/Protocol.test.ts +618 -11
- package/src/local-first/Protocol.ts +197 -73
- package/src/local-first/Query.ts +8 -15
- package/src/local-first/Schema.test.ts +143 -0
- package/src/local-first/Schema.ts +374 -24
- package/src/local-first/Shared.test.ts +7731 -559
- package/src/local-first/Shared.ts +2036 -267
- package/src/local-first/Storage.ts +219 -33
- package/src/local-first/Timestamp.test.ts +344 -70
- package/src/local-first/Timestamp.ts +435 -119
- package/src/local-first/index.ts +0 -1
- package/dist/src/Binary.d.ts +0 -254
- package/dist/src/Binary.d.ts.map +0 -1
- package/dist/src/local-first/Error.d.ts +0 -12
- package/dist/src/local-first/Error.d.ts.map +0 -1
- package/dist/src/local-first/Error.js +0 -6
- package/dist/src/local-first/LocalAuth.d.ts +0 -150
- package/dist/src/local-first/LocalAuth.d.ts.map +0 -1
- package/dist/src/local-first/LocalAuth.js +0 -179
- package/src/local-first/Error.ts +0 -17
- package/src/local-first/LocalAuth.ts +0 -457
|
@@ -174,8 +174,8 @@
|
|
|
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, assertSame, } from "../Assert.js";
|
|
178
|
-
import { createBuffer, createRunLengthEncoder, decodeFlags, decodeJsonValue, decodeLength, decodeNonNegativeInt, decodeNumber, decodeRle, decodeString, encodeFlags, encodeJsonValue, encodeLength, encodeNonNegativeInt, encodeNumber, encodeString, } from "../
|
|
177
|
+
import { assert, assertNonEmptyArray, assertNonNullable, assertNotUndefined, assertSame, } from "../Assert.js";
|
|
178
|
+
import { createBuffer, createRunLengthEncoder, decodeFlags, decodeJsonValue, decodeLength, decodeNonNegativeInt, decodeNumber, decodeRle, decodeString, encodeFlags, encodeJsonValue, encodeLength, encodeNonNegativeInt, encodeNumber, encodeString, } from "../Bytes.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";
|
|
@@ -243,10 +243,12 @@ export const MessageType = {
|
|
|
243
243
|
Broadcast: 2,
|
|
244
244
|
};
|
|
245
245
|
/**
|
|
246
|
-
* Parses the
|
|
246
|
+
* Parses the {@link ProtocolHeader} a transport needs to route a message.
|
|
247
247
|
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
248
|
+
* Parsing succeeds for versions this peer cannot process, because a
|
|
249
|
+
* non-initiator answers a version mismatch with only its version and the owner
|
|
250
|
+
* ID. Rejecting that reply here would drop it before
|
|
251
|
+
* {@link applyProtocolMessageAsClient} can report {@link ProtocolVersionError}.
|
|
250
252
|
*/
|
|
251
253
|
export const parseProtocolHeader = (inputMessage) => {
|
|
252
254
|
try {
|
|
@@ -330,6 +332,71 @@ export const createProtocolMessageFromCrdtMessages = (deps) => (owner, messages,
|
|
|
330
332
|
}
|
|
331
333
|
return buffer.unwrap();
|
|
332
334
|
};
|
|
335
|
+
/**
|
|
336
|
+
* Creates size-limited broadcast {@link ProtocolMessage}s containing every
|
|
337
|
+
* supplied {@link CrdtMessage}.
|
|
338
|
+
*
|
|
339
|
+
* Broadcasts contain no synchronization ranges or write key. Unlike
|
|
340
|
+
* {@link createProtocolMessageFromCrdtMessages}, this function splits all
|
|
341
|
+
* messages into complete frames rather than relying on later synchronization
|
|
342
|
+
* rounds to deliver messages that do not fit. Each individual message must fit
|
|
343
|
+
* the configured size limit.
|
|
344
|
+
*
|
|
345
|
+
* ### Example
|
|
346
|
+
*
|
|
347
|
+
* ```ts
|
|
348
|
+
* import {
|
|
349
|
+
* assertSame,
|
|
350
|
+
* createId,
|
|
351
|
+
* getOrThrow,
|
|
352
|
+
* testCreateDeps,
|
|
353
|
+
* } from "@evolu/common";
|
|
354
|
+
* import {
|
|
355
|
+
* createProtocolBroadcastMessagesFromCrdtMessages,
|
|
356
|
+
* MessageType,
|
|
357
|
+
* parseProtocolHeader,
|
|
358
|
+
* testAppOwner,
|
|
359
|
+
* testCreateCrdtMessage,
|
|
360
|
+
* } from "@evolu/common/local-first";
|
|
361
|
+
*
|
|
362
|
+
* const deps = testCreateDeps();
|
|
363
|
+
* const broadcasts = createProtocolBroadcastMessagesFromCrdtMessages(deps)(
|
|
364
|
+
* testAppOwner,
|
|
365
|
+
* [testCreateCrdtMessage(createId(deps), 1, "Ada")],
|
|
366
|
+
* );
|
|
367
|
+
* assertSame(broadcasts.length, 1);
|
|
368
|
+
* assertSame(
|
|
369
|
+
* getOrThrow(parseProtocolHeader(broadcasts[0])).messageType,
|
|
370
|
+
* MessageType.Broadcast,
|
|
371
|
+
* );
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
export const createProtocolBroadcastMessagesFromCrdtMessages = (deps) => (owner, messages, maxSize = defaultProtocolMessageMaxSize) => {
|
|
375
|
+
const broadcasts = [];
|
|
376
|
+
let buffer = createProtocolMessageBuffer(owner.id, {
|
|
377
|
+
messageType: MessageType.Broadcast,
|
|
378
|
+
totalMaxSize: maxSize,
|
|
379
|
+
});
|
|
380
|
+
for (const message of messages) {
|
|
381
|
+
const encryptedMessage = {
|
|
382
|
+
timestamp: message.timestamp,
|
|
383
|
+
change: encodeAndEncryptDbChange(deps)(message, owner.encryptionKey),
|
|
384
|
+
};
|
|
385
|
+
if (!buffer.canAddMessage(encryptedMessage)) {
|
|
386
|
+
const nextBuffer = createProtocolMessageBuffer(owner.id, {
|
|
387
|
+
messageType: MessageType.Broadcast,
|
|
388
|
+
totalMaxSize: maxSize,
|
|
389
|
+
});
|
|
390
|
+
assert(nextBuffer.canAddMessage(encryptedMessage), "the message is too big");
|
|
391
|
+
broadcasts.push(buffer.unwrap());
|
|
392
|
+
buffer = nextBuffer;
|
|
393
|
+
}
|
|
394
|
+
buffer.addMessage(encryptedMessage);
|
|
395
|
+
}
|
|
396
|
+
broadcasts.push(buffer.unwrap());
|
|
397
|
+
assertNonEmptyArray(broadcasts);
|
|
398
|
+
return broadcasts;
|
|
399
|
+
};
|
|
333
400
|
/** Creates a {@link ProtocolMessage} for sync. */
|
|
334
401
|
export const createProtocolMessageForSync = (deps) => (ownerId, subscriptionFlag) => {
|
|
335
402
|
const buffer = createProtocolMessageBuffer(ownerId, {
|
|
@@ -576,17 +643,19 @@ export const applyProtocolMessageAsClient = (inputMessage, options = {}) => asyn
|
|
|
576
643
|
if (isNonEmptyArray(messages)) {
|
|
577
644
|
try {
|
|
578
645
|
const result = await run(storage.writeMessages(ownerIdBytes, messages));
|
|
579
|
-
// Quota errors are handled by Storage; protocol just stops syncing.
|
|
580
646
|
if (!result.ok)
|
|
581
|
-
return
|
|
647
|
+
return result;
|
|
582
648
|
}
|
|
583
649
|
catch (error) {
|
|
584
650
|
if (AbortError.is(error))
|
|
585
651
|
throw error;
|
|
586
652
|
run.deps.console.error(error);
|
|
587
|
-
return ok({ type: "
|
|
653
|
+
return ok({ type: "Failed", cause: "Write" });
|
|
588
654
|
}
|
|
589
655
|
}
|
|
656
|
+
if (messageType === MessageType.Broadcast) {
|
|
657
|
+
return ok({ type: "Broadcast" });
|
|
658
|
+
}
|
|
590
659
|
// Now: No writeKey, no sync.
|
|
591
660
|
// TODO: Allow to sync SharedReadonlyOwner
|
|
592
661
|
// Without local changes, writeKey will not be required.
|
|
@@ -594,26 +663,34 @@ export const applyProtocolMessageAsClient = (inputMessage, options = {}) => asyn
|
|
|
594
663
|
// the sync will stop.
|
|
595
664
|
const writeKey = options.writeKey;
|
|
596
665
|
if (writeKey == null) {
|
|
597
|
-
return ok({ type: "
|
|
598
|
-
}
|
|
599
|
-
if (messageType === MessageType.Broadcast) {
|
|
600
|
-
return ok({ type: "Broadcast" });
|
|
666
|
+
return ok({ type: "Readonly" });
|
|
601
667
|
}
|
|
602
668
|
const ranges = decodeRanges(input);
|
|
603
669
|
if (!isNonEmptyArray(ranges)) {
|
|
604
|
-
return ok({ type: "
|
|
670
|
+
return ok({ type: "Converged" });
|
|
605
671
|
}
|
|
606
672
|
const output = createProtocolMessageBuffer(ownerId, {
|
|
607
673
|
messageType: MessageType.Request,
|
|
608
674
|
writeKey,
|
|
609
675
|
rangesMaxSize: options.rangesMaxSize,
|
|
610
676
|
});
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
677
|
+
let broadcast;
|
|
678
|
+
const result = sync(run.deps)(ranges, output, ownerIdBytes, (message) => {
|
|
679
|
+
broadcast ??= createProtocolMessageBuffer(ownerId, {
|
|
680
|
+
messageType: MessageType.Broadcast,
|
|
681
|
+
});
|
|
682
|
+
broadcast.addMessage(message);
|
|
683
|
+
});
|
|
684
|
+
// A failure was logged by sync.
|
|
685
|
+
if (!result.ok)
|
|
686
|
+
return ok({ type: "Failed", cause: "Sync" });
|
|
687
|
+
if (!result.value)
|
|
688
|
+
return ok({ type: "Converged" });
|
|
689
|
+
return ok({
|
|
690
|
+
type: "Response",
|
|
691
|
+
message: output.unwrap(),
|
|
692
|
+
...(broadcast && { broadcast: broadcast.unwrap() }),
|
|
693
|
+
});
|
|
617
694
|
}
|
|
618
695
|
catch (error) {
|
|
619
696
|
if (AbortError.is(error))
|
|
@@ -687,9 +764,14 @@ version = protocolVersion) => async (run) => {
|
|
|
687
764
|
try {
|
|
688
765
|
const result = await run(storage.writeMessages(ownerIdBytes, messages));
|
|
689
766
|
if (!result.ok) {
|
|
767
|
+
const isQuotaError = result.error.type === "StorageQuotaError";
|
|
768
|
+
if (!isQuotaError)
|
|
769
|
+
run.deps.console.error(result.error);
|
|
690
770
|
const message = createProtocolMessageBuffer(ownerId, {
|
|
691
771
|
messageType: MessageType.Response,
|
|
692
|
-
errorCode:
|
|
772
|
+
errorCode: isQuotaError
|
|
773
|
+
? ProtocolErrorCode.QuotaError
|
|
774
|
+
: ProtocolErrorCode.WriteError,
|
|
693
775
|
}).unwrap();
|
|
694
776
|
return ok({ type: "Response", message });
|
|
695
777
|
}
|
|
@@ -778,7 +860,7 @@ const decodeVersionAndOwner = (input) => {
|
|
|
778
860
|
const parseProtocolHeaderFromBuffer = (input) => {
|
|
779
861
|
const [version, ownerId] = decodeVersionAndOwner(input);
|
|
780
862
|
if (version !== protocolVersion) {
|
|
781
|
-
|
|
863
|
+
return { type: "ProtocolHeader", version, ownerId };
|
|
782
864
|
}
|
|
783
865
|
const messageTypeValue = input.shift();
|
|
784
866
|
let messageType;
|
|
@@ -795,12 +877,7 @@ const parseProtocolHeaderFromBuffer = (input) => {
|
|
|
795
877
|
default:
|
|
796
878
|
throw new ProtocolDecodeError("Invalid MessageType");
|
|
797
879
|
}
|
|
798
|
-
return {
|
|
799
|
-
type: "ProtocolHeader",
|
|
800
|
-
version: 1,
|
|
801
|
-
ownerId,
|
|
802
|
-
messageType,
|
|
803
|
-
};
|
|
880
|
+
return { type: "ProtocolHeader", version, ownerId, messageType };
|
|
804
881
|
};
|
|
805
882
|
/**
|
|
806
883
|
* Error thrown for internal protocol validation failures, such as invalid data
|
|
@@ -824,7 +901,7 @@ const decodeMessages = (buffer) => {
|
|
|
824
901
|
}
|
|
825
902
|
return messages;
|
|
826
903
|
};
|
|
827
|
-
const sync = (deps) => (ranges, output, ownerIdBytes) => {
|
|
904
|
+
const sync = (deps) => (ranges, output, ownerIdBytes, onMessage) => {
|
|
828
905
|
const outputInitialSize = output.getSize();
|
|
829
906
|
let storageSize;
|
|
830
907
|
try {
|
|
@@ -911,7 +988,15 @@ const sync = (deps) => (ranges, output, ownerIdBytes) => {
|
|
|
911
988
|
}
|
|
912
989
|
else if (output.canSplitRange()) {
|
|
913
990
|
coalesceSkipsBeforeAdd();
|
|
914
|
-
|
|
991
|
+
try {
|
|
992
|
+
splitRange(deps)(ownerIdBytes, lower, upper, currentUpperBound, output);
|
|
993
|
+
}
|
|
994
|
+
catch (error) {
|
|
995
|
+
if (AbortError.is(error))
|
|
996
|
+
throw error;
|
|
997
|
+
deps.console.error(error);
|
|
998
|
+
return err(ProtocolErrorCode.SyncError);
|
|
999
|
+
}
|
|
915
1000
|
}
|
|
916
1001
|
else {
|
|
917
1002
|
return addFingerprintForRemainingRange(upper)
|
|
@@ -954,8 +1039,10 @@ const sync = (deps) => (ranges, output, ownerIdBytes) => {
|
|
|
954
1039
|
return false;
|
|
955
1040
|
}
|
|
956
1041
|
ourTimestamps.add(timestampBinary);
|
|
957
|
-
if (message)
|
|
1042
|
+
if (message) {
|
|
958
1043
|
output.addMessage(message);
|
|
1044
|
+
onMessage?.(message);
|
|
1045
|
+
}
|
|
959
1046
|
return true;
|
|
960
1047
|
});
|
|
961
1048
|
}
|
|
@@ -1007,7 +1094,7 @@ const splitRange = (deps) => (ownerId, lower, upper, upperBound, buffer) => {
|
|
|
1007
1094
|
upperBound,
|
|
1008
1095
|
timestamps: createTimestampsBuffer(),
|
|
1009
1096
|
};
|
|
1010
|
-
deps.storage.iterate(ownerId,
|
|
1097
|
+
deps.storage.iterate(ownerId, lower, upper, (timestamp) => {
|
|
1011
1098
|
range.timestamps.add(timestampBytesToTimestamp(timestamp));
|
|
1012
1099
|
return true;
|
|
1013
1100
|
});
|
|
@@ -1021,14 +1108,7 @@ const splitRange = (deps) => (ownerId, lower, upper, upperBound, buffer) => {
|
|
|
1021
1108
|
lower,
|
|
1022
1109
|
...buckets.value.map((b) => NonNegativeInt.orThrow(b + lower)),
|
|
1023
1110
|
];
|
|
1024
|
-
|
|
1025
|
-
try {
|
|
1026
|
-
fingerprintRanges = deps.storage.fingerprintRanges(ownerId, fingerprintRangesBuckets, upperBound);
|
|
1027
|
-
}
|
|
1028
|
-
catch (error) {
|
|
1029
|
-
deps.console.error(error);
|
|
1030
|
-
return;
|
|
1031
|
-
}
|
|
1111
|
+
const fingerprintRanges = deps.storage.fingerprintRanges(ownerId, fingerprintRangesBuckets, upperBound);
|
|
1032
1112
|
const rangesToUse = lower > 0 ? fingerprintRanges.slice(1) : fingerprintRanges;
|
|
1033
1113
|
for (const range of rangesToUse) {
|
|
1034
1114
|
buffer.addRange(range);
|
|
@@ -18,25 +18,22 @@ export type { NotNull as KyselyNotNull } from "kysely";
|
|
|
18
18
|
* import {
|
|
19
19
|
* assertType,
|
|
20
20
|
* createQueryBuilder,
|
|
21
|
-
*
|
|
21
|
+
* testEvoluSchema,
|
|
22
|
+
* type TestEvoluSchema,
|
|
22
23
|
* NonEmptyTrimmedString100,
|
|
23
24
|
* type Query,
|
|
25
|
+
* type TestTodoId,
|
|
24
26
|
* } from "@evolu/common";
|
|
25
27
|
*
|
|
26
|
-
* const
|
|
27
|
-
* type TodoId = typeof TodoId.Output;
|
|
28
|
-
* const Schema = {
|
|
29
|
-
* todo: { id: TodoId, title: NonEmptyTrimmedString100 },
|
|
30
|
-
* };
|
|
31
|
-
* const createQuery = createQueryBuilder(Schema);
|
|
28
|
+
* const createQuery = createQueryBuilder(testEvoluSchema);
|
|
32
29
|
* const allTodos = createQuery((db) => db.selectFrom("todo").selectAll());
|
|
33
30
|
*
|
|
34
31
|
* type AllTodosRow = typeof allTodos.Row;
|
|
35
32
|
* assertType<
|
|
36
|
-
* typeof allTodos extends Query<
|
|
33
|
+
* typeof allTodos extends Query<TestEvoluSchema> ? true : false,
|
|
37
34
|
* true
|
|
38
35
|
* >();
|
|
39
|
-
* assertType<AllTodosRow["id"],
|
|
36
|
+
* assertType<AllTodosRow["id"], TestTodoId>();
|
|
40
37
|
* assertType<AllTodosRow["title"], NonEmptyTrimmedString100 | null>();
|
|
41
38
|
* ```
|
|
42
39
|
*/
|
|
@@ -52,15 +49,11 @@ export type Query<S extends EvoluSchema = EvoluSchema, R extends Row = Row> = Sq
|
|
|
52
49
|
* import {
|
|
53
50
|
* assertType,
|
|
54
51
|
* createQueryBuilder,
|
|
55
|
-
*
|
|
52
|
+
* testEvoluSchema,
|
|
56
53
|
* type InferRow,
|
|
57
|
-
* NonEmptyTrimmedString100,
|
|
58
54
|
* } from "@evolu/common";
|
|
59
55
|
*
|
|
60
|
-
* const
|
|
61
|
-
* todo: { id: id("Todo"), title: NonEmptyTrimmedString100 },
|
|
62
|
-
* };
|
|
63
|
-
* const createQuery = createQueryBuilder(Schema);
|
|
56
|
+
* const createQuery = createQueryBuilder(testEvoluSchema);
|
|
64
57
|
* const allTodos = createQuery((db) =>
|
|
65
58
|
* db.selectFrom("todo").selectAll(),
|
|
66
59
|
* );
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../../src/local-first/Query.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,UAAU,EACV,QAAQ,IAAI,cAAc,EAC1B,UAAU,EACV,eAAe,EAChB,MAAM,QAAQ,CAAC;AAYhB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAIzC,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,QAAQ,CAAC;AAC1C,YAAY,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvD
|
|
1
|
+
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../../src/local-first/Query.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,UAAU,EACV,QAAQ,IAAI,cAAc,EAC1B,UAAU,EACV,eAAe,EAChB,MAAM,QAAQ,CAAC;AAYhB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAIzC,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,QAAQ,CAAC;AAC1C,YAAY,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvD;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