@@ -325,7 +325,7 @@ export const defaultProtocolMessageRangesMaxSize =
325325export type ProtocolMessage = Uint8Array & Brand < "ProtocolMessage" > ;
326326
327327/** Evolu Protocol version. */
328- export const protocolVersion = 0 as NonNegativeInt ;
328+ export const protocolVersion = NonNegativeInt . orThrow ( 0 ) ;
329329
330330export const MessageType = {
331331 /** Request message from initiator (client) to non-initiator (relay). */
@@ -525,7 +525,7 @@ export const createProtocolMessageForSync =
525525
526526 splitRange ( deps ) (
527527 ownerIdBytes ,
528- 0 as NonNegativeInt ,
528+ NonNegativeInt . orThrow ( 0 ) ,
529529 size ,
530530 InfiniteUpperBound ,
531531 buffer ,
@@ -628,7 +628,7 @@ export const createProtocolMessageBuffer = (
628628 const isWithinSizeLimits = ( ) => getSize ( ) <= totalMaxSize ;
629629
630630 const getSize = ( ) =>
631- ( getHeaderAndMessagesSize ( ) + getRangesSize ( ) ) as PositiveInt ;
631+ PositiveInt . orThrow ( getHeaderAndMessagesSize ( ) + getRangesSize ( ) ) ;
632632
633633 const getHeaderAndMessagesSize = ( ) =>
634634 buffers . header . getLength ( ) +
@@ -726,7 +726,10 @@ export const createProtocolMessageBuffer = (
726726 buffers . ranges . timestamps . addInfinite ( ) ;
727727 }
728728
729- encodeNonNegativeInt ( buffers . ranges . types , range . type as NonNegativeInt ) ;
729+ encodeNonNegativeInt (
730+ buffers . ranges . types ,
731+ NonNegativeInt . orThrow ( range . type ) ,
732+ ) ;
730733
731734 switch ( range . type ) {
732735 case RangeType . Skip :
@@ -781,7 +784,7 @@ export interface TimestampsBuffer {
781784}
782785
783786export const createTimestampsBuffer = ( ) : TimestampsBuffer => {
784- let count = 0 as NonNegativeInt ;
787+ let count = NonNegativeInt . orThrow ( 0 ) ;
785788 const countBuffer = createBuffer ( ) ;
786789
787790 const syncCount = ( ) => {
@@ -848,9 +851,9 @@ const createRunLengthEncoder = <T>(
848851 encodeValue : ( buffer : Buffer , value : T ) => void ,
849852) : RunLengthEncoder < T > => {
850853 const buffer = createBuffer ( ) ;
851- let previousLength = 0 as NonNegativeInt ;
854+ let previousLength = NonNegativeInt . orThrow ( 0 ) ;
852855 let previousValue = null as T | null ;
853- let runLength = 0 as NonNegativeInt ;
856+ let runLength = NonNegativeInt . orThrow ( 0 ) ;
854857
855858 return {
856859 add : ( value ) => {
@@ -859,7 +862,7 @@ const createRunLengthEncoder = <T>(
859862 buffer . truncate ( previousLength ) ;
860863 } else {
861864 previousValue = value ;
862- runLength = 1 as NonNegativeInt ;
865+ runLength = NonNegativeInt . orThrow ( 1 ) ;
863866 }
864867 previousLength = buffer . getLength ( ) ;
865868 encodeValue ( buffer , value ) ;
@@ -1255,7 +1258,7 @@ const sync =
12551258 if ( storageSize == null ) return err ( ProtocolErrorCode . SyncError ) ;
12561259
12571260 let prevUpperBound : RangeUpperBound | null = null ;
1258- let prevIndex = 0 as NonNegativeInt ;
1261+ let prevIndex = NonNegativeInt . orThrow ( 0 ) ;
12591262
12601263 let skip = false ;
12611264 let nonSkipRangeAdded = false ;
@@ -1458,7 +1461,7 @@ const splitRange =
14581461 upperBound : RangeUpperBound ,
14591462 buffer : ProtocolMessageBuffer ,
14601463 ) : void => {
1461- const itemCount = ( upper - lower ) as NonNegativeInt ;
1464+ const itemCount = NonNegativeInt . orThrow ( upper - lower ) ;
14621465 const buckets = computeBalancedBuckets ( itemCount ) ;
14631466
14641467 if ( ! buckets . ok ) {
@@ -1470,7 +1473,7 @@ const splitRange =
14701473
14711474 deps . storage . iterate (
14721475 ownerId ,
1473- 0 as NonNegativeInt ,
1476+ NonNegativeInt . orThrow ( 0 ) ,
14741477 itemCount ,
14751478 ( timestamp ) => {
14761479 range . timestamps . add ( timestampBytesToTimestamp ( timestamp ) ) ;
@@ -1486,7 +1489,10 @@ const splitRange =
14861489 const fingerprintRangesBuckets =
14871490 lower === 0
14881491 ? buckets . value
1489- : [ lower , ...buckets . value . map ( ( b ) => ( b + lower ) as NonNegativeInt ) ] ;
1492+ : [
1493+ lower ,
1494+ ...buckets . value . map ( ( b ) => NonNegativeInt . orThrow ( b + lower ) ) ,
1495+ ] ;
14901496
14911497 const fingerprintRanges = deps . storage . fingerprintRanges (
14921498 ownerId ,
@@ -1510,7 +1516,7 @@ const decodeRanges = (buffer: Buffer): ReadonlyArray<Range> => {
15101516 const rangesCount = decodeNonNegativeInt ( buffer ) ;
15111517 if ( rangesCount === 0 ) return [ ] ;
15121518
1513- const timestampsCount = ( rangesCount - 1 ) as NonNegativeInt ;
1519+ const timestampsCount = NonNegativeInt . orThrow ( rangesCount - 1 ) ;
15141520 const timestamps = decodeTimestamps ( buffer , timestampsCount ) ;
15151521 const rangeTypes : Array < RangeType > = [ ] ;
15161522
@@ -1845,7 +1851,7 @@ export const decodeNonNegativeInt = (buffer: Buffer): NonNegativeInt => {
18451851} ;
18461852
18471853export const encodeLength = ( buffer : Buffer , value : ArrayLike < any > ) : void => {
1848- encodeNonNegativeInt ( buffer , value . length as NonNegativeInt ) ;
1854+ encodeNonNegativeInt ( buffer , NonNegativeInt . orThrow ( value . length ) ) ;
18491855} ;
18501856
18511857export const decodeLength = decodeNonNegativeInt ;
@@ -1867,7 +1873,7 @@ export const encodeNodeId = (buffer: Buffer, nodeId: NodeId): void => {
18671873} ;
18681874
18691875export const decodeNodeId = ( buffer : Buffer ) : NodeId => {
1870- const bytes = buffer . shiftN ( 8 as NonNegativeInt ) ;
1876+ const bytes = buffer . shiftN ( NonNegativeInt . orThrow ( 8 ) ) ;
18711877 return bytesToHex ( bytes ) as NodeId ;
18721878} ;
18731879
@@ -1879,26 +1885,26 @@ export const ProtocolValueType = {
18791885 // 0-19 small ints
18801886
18811887 // SQLite types
1882- String : 20 as NonNegativeInt ,
1883- Number : 21 as NonNegativeInt ,
1884- Null : 22 as NonNegativeInt ,
1885- Bytes : 23 as NonNegativeInt ,
1888+ String : NonNegativeInt . orThrow ( 20 ) ,
1889+ Number : NonNegativeInt . orThrow ( 21 ) ,
1890+ Null : NonNegativeInt . orThrow ( 22 ) ,
1891+ Bytes : NonNegativeInt . orThrow ( 23 ) ,
18861892 // We can add more types for other DBs or anything else later.
18871893
18881894 // Optimized types
1889- NonNegativeInt : 30 as NonNegativeInt ,
1895+ NonNegativeInt : NonNegativeInt . orThrow ( 30 ) ,
18901896
18911897 // String optimizations
1892- EmptyString : 31 as NonNegativeInt , // 1 byte vs 2 bytes (50% reduction)
1893- Base64Url : 32 as NonNegativeInt ,
1894- Id : 33 as NonNegativeInt ,
1895- Json : 34 as NonNegativeInt ,
1898+ EmptyString : NonNegativeInt . orThrow ( 31 ) , // 1 byte vs 2 bytes (50% reduction)
1899+ Base64Url : NonNegativeInt . orThrow ( 32 ) ,
1900+ Id : NonNegativeInt . orThrow ( 33 ) ,
1901+ Json : NonNegativeInt . orThrow ( 34 ) ,
18961902
18971903 // new Date().toISOString() - 24 bytes
18981904 // encoded with fixed length - 8 bytes
18991905 // encode as NonNegativeInt - 6 bytes (additional 25% reduction)
1900- DateIsoWithNonNegativeTime : 35 as NonNegativeInt ,
1901- DateIsoWithNegativeTime : 36 as NonNegativeInt , // 9 bytes
1906+ DateIsoWithNonNegativeTime : NonNegativeInt . orThrow ( 35 ) ,
1907+ DateIsoWithNegativeTime : NonNegativeInt . orThrow ( 36 ) , // 9 bytes
19021908
19031909 // TODO: Operations (from 40)
19041910 // Increment, Decrement, Patch, whatever.
0 commit comments