Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
-Encodes numbers in little-endian order (least to most significant byte order)
-and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
-which is also used in Protocol Buffers.
-
-new encoding.Encoder()
-A BinaryEncoder handles the encoding to an Uint8Array.
-encoding.Encoder#bufs: Array<Uint8Array>
-encoding.createEncoder(): module:encoding.Encoder
-encoding.length(encoder: module:encoding.Encoder): number
-The current length of the encoded data.
-encoding.toUint8Array(encoder: module:encoding.Encoder): Uint8Array
-Transform to Uint8Array.
-encoding.verifyLen(encoder: module:encoding.Encoder, len: number)
-Verify that it is possible to write len bytes wtihout checking. If
-necessary, a new Buffer with the required length is attached.
-encoding.write(encoder: module:encoding.Encoder, num: number)
-Write one byte to the encoder.
-encoding.set(encoder: module:encoding.Encoder, pos: number, num: number)
-Write one byte at a specific position.
-Position must already be written (i.e. encoder.length > pos)
-encoding.writeUint8(encoder: module:encoding.Encoder, num: number)
-Write one byte as an unsigned integer.
-encoding.setUint8(encoder: module:encoding.Encoder, pos: number, num: number)
-Write one byte as an unsigned Integer at a specific location.
-encoding.writeUint16(encoder: module:encoding.Encoder, num: number)
-Write two bytes as an unsigned integer.
-encoding.setUint16(encoder: module:encoding.Encoder, pos: number, num: number)
-Write two bytes as an unsigned integer at a specific location.
-encoding.writeUint32(encoder: module:encoding.Encoder, num: number)
-Write two bytes as an unsigned integer
-encoding.writeUint32BigEndian(encoder: module:encoding.Encoder, num: number)
-Write two bytes as an unsigned integer in big endian order.
-(most significant byte first)
-encoding.setUint32(encoder: module:encoding.Encoder, pos: number, num: number)
-Write two bytes as an unsigned integer at a specific location.
-encoding.writeVarUint(encoder: module:encoding.Encoder, num: number)
-Write a variable length unsigned integer. Max encodable integer is 2^53.
-encoding.writeVarInt(encoder: module:encoding.Encoder, num: number)
-Write a variable length integer.
-We use the 7th bit instead for signaling that this is a negative number.
-encoding.writeVarString
-encoding.writeBinaryEncoder(encoder: module:encoding.Encoder, append: module:encoding.Encoder)
-Write the content of another Encoder.
-encoding.writeUint8Array(encoder: module:encoding.Encoder, uint8Array: Uint8Array)
-Append fixed-length Uint8Array to the encoder.
-encoding.writeVarUint8Array(encoder: module:encoding.Encoder, uint8Array: Uint8Array)
-Append an Uint8Array to Encoder.
-encoding.writeOnDataView(encoder: module:encoding.Encoder, len: number): DataView
-Create an DataView of the next len bytes. Use it to write data after
-calling this function.
-// write float32 using DataView
-const dv = writeOnDataView(encoder, 4)
-dv.setFloat32(0, 1.1)
-// read float32 using DataView
-const dv = readFromDataView(encoder, 4)
-dv.getFloat32(0) // => 1.100000023841858 (leaving it to the reader to find out why this is the correct result)
-
-encoding.writeFloat32(encoder: module:encoding.Encoder, num: number)
-encoding.writeFloat64(encoder: module:encoding.Encoder, num: number)
-encoding.writeBigInt64(encoder: module:encoding.Encoder, num: bigint)
-encoding.writeBigUint64(encoder: module:encoding.Encoder, num: bigint)
-encoding.writeAny(encoder: module:encoding.Encoder, data: undefined|null|number|bigint|boolean|string|Object<string,any>|Array<any>|Uint8Array)
-Encode data with efficient binary format.
-Differences to JSON:
-• Transforms data to a binary format (not to a string)
-• Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON)
-• Numbers are efficiently encoded either as a variable length integer, as a
-32 bit float, as a 64 bit float, or as a 64 bit bigint.
-Encoding table:
-
-
-
-| Data Type |
-Prefix |
-Encoding Method |
-Comment |
-
-
-
-
-| undefined |
-127 |
- |
-Functions, symbol, and everything that cannot be identified is encoded as undefined |
-
-
-| null |
-126 |
- |
- |
-
-
-| integer |
-125 |
-writeVarInt |
-Only encodes 32 bit signed integers |
-
-
-| float32 |
-124 |
-writeFloat32 |
- |
-
-
-| float64 |
-123 |
-writeFloat64 |
- |
-
-
-| bigint |
-122 |
-writeBigInt64 |
- |
-
-
-| boolean (false) |
-121 |
- |
-True and false are different data types so we save the following byte |
-
-
-| boolean (true) |
-120 |
- |
-- 0b01111000 so the last bit determines whether true or false |
-
-
-| string |
-119 |
-writeVarString |
- |
-
-
-| object<string,any> |
-118 |
-custom |
-Writes {length} then {length} key-value pairs |
-
-
-| array |
-117 |
-custom |
-Writes {length} then {length} json values |
-
-
-| Uint8Array |
-116 |
-writeVarUint8Array |
-We use Uint8Array for any kind of binary data |
-
-
-
-Reasons for the decreasing prefix:
-We need the first bit for extendability (later we may want to encode the
-prefix with writeVarUint). The remaining 7 bits are divided as follows:
-[0-30] the beginning of the data range is used for custom purposes
-(defined by the function that uses this library)
-[31-127] the end of the data range is used for data encoding by
-lib0/encoding.js
-new encoding.RleEncoder(writer: function(module:encoding.Encoder, T):void)
-Now come a few stateful encoder that have their own classes.
-encoding.RleEncoder#s: T|null
-Current state
-encoding.RleEncoder#write(v: T)
-new encoding.IntDiffEncoder(start: number)
-Basic diff decoder using variable length encoding.
-Encodes the values [3, 1100, 1101, 1050, 0] to [3, 1097, 1, -51, -1050] using writeVarInt.
-encoding.IntDiffEncoder#s: number
-Current state
-encoding.IntDiffEncoder#write(v: number)
-new encoding.RleIntDiffEncoder(start: number)
-A combination of IntDiffEncoder and RleEncoder.
-Basically first writes the IntDiffEncoder and then counts duplicate diffs using RleEncoding.
-Encodes the values [1,1,1,2,3,4,5,6] as [1,1,0,2,1,5] (RLE([1,0,0,1,1,1,1,1]) ⇒ RleIntDiff[1,1,0,2,1,5])
-encoding.RleIntDiffEncoder#s: number
-Current state
-encoding.RleIntDiffEncoder#write(v: number)
-new encoding.UintOptRleEncoder()
-Optimized Rle encoder that does not suffer from the mentioned problem of the basic Rle encoder.
-Internally uses VarInt encoder to write unsigned integers. If the input occurs multiple times, we write
-write it as a negative number. The UintOptRleDecoder then understands that it needs to read a count.
-Encodes [1,2,3,3,3] as [1,2,-3,3] (once 1, once 2, three times 3)
-encoding.UintOptRleEncoder#s: number
-encoding.UintOptRleEncoder#write(v: number)
-encoding.UintOptRleEncoder#toUint8Array()
-new encoding.IncUintOptRleEncoder()
-Increasing Uint Optimized RLE Encoder
-The RLE encoder counts the number of same occurences of the same value.
-The IncUintOptRle encoder counts if the value increases.
-I.e. 7, 8, 9, 10 will be encoded as [-7, 4]. 1, 3, 5 will be encoded
-as [1, 3, 5].
-encoding.IncUintOptRleEncoder#s: number
-encoding.IncUintOptRleEncoder#write(v: number)
-encoding.IncUintOptRleEncoder#toUint8Array()
-new encoding.IntDiffOptRleEncoder()
-A combination of the IntDiffEncoder and the UintOptRleEncoder.
-The count approach is similar to the UintDiffOptRleEncoder, but instead of using the negative bitflag, it encodes
-in the LSB whether a count is to be read. Therefore this Encoder only supports 31 bit integers!
-Encodes [1, 2, 3, 2] as [3, 1, 6, -1] (more specifically [(1 << 1) | 1, (3 << 0) | 0, -1])
-Internally uses variable length encoding. Contrary to normal UintVar encoding, the first byte contains:
-
-- 1 bit that denotes whether the next value is a count (LSB)
-- 1 bit that denotes whether this value is negative (MSB - 1)
-- 1 bit that denotes whether to continue reading the variable length integer (MSB)
-
-Therefore, only five bits remain to encode diff ranges.
-Use this Encoder only when appropriate. In most cases, this is probably a bad idea.
-encoding.IntDiffOptRleEncoder#s: number
-encoding.IntDiffOptRleEncoder#write(v: number)
-encoding.IntDiffOptRleEncoder#toUint8Array()
-new encoding.StringEncoder()
-Optimized String Encoder.
-Encoding many small strings in a simple Encoder is not very efficient. The function call to decode a string takes some time and creates references that must be eventually deleted.
-In practice, when decoding several million small strings, the GC will kick in more and more often to collect orphaned string objects (or maybe there is another reason?).
-This string encoder solves the above problem. All strings are concatenated and written as a single string using a single encoding call.
-The lengths are encoded using a UintOptRleEncoder.
-encoding.StringEncoder#sarr: Array<string>
-encoding.StringEncoder#write(string: string)
-encoding.StringEncoder#toUint8Array()
-encoding.RleEncoder#bufs: Array<Uint8Array>
-encoding.IntDiffEncoder#bufs: Array<Uint8Array>
-encoding.RleIntDiffEncoder#bufs: Array<Uint8Array>
-
-