Skip to content

Commit

Permalink
Merge branch 'release/v0.22.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Jul 15, 2024
2 parents 09f023f + 7f71308 commit 98492a8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.22.9",
"version": "0.22.10",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
8 changes: 4 additions & 4 deletions src/common/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { Encoder } from '../msg/encoder'
import { assertCondition } from '../assert'
import type { BinInput } from '../data/bin'
import { toUint8Array } from '../data/bin'
import { createEncoder, encodeToUint8Array, length, writeAny, writeFloat32, writeUint16, writeUint32, writeUint8, writeUint8Array, writeVarInt, writeVarString, writeVarUint, writeVarUint8Array } from './lib0/encoding'
import { createBinEncoder, encodeToUint8Array, length, writeAny, writeFloat32, writeUint16, writeUint32, writeUint8, writeUint8Array, writeVarInt, writeVarString, writeVarUint, writeVarUint8Array } from './lib0/encoding'
import { createDecoder, readAny, readFloat32, readUint16, readUint32, readUint8, readUint8Array, readVarInt, readVarString, readVarUint, readVarUint8Array } from './lib0/decoding'

// Use in Zeed channels

export class BinaryEncoder implements Encoder {
async encode(data: any): Promise<Uint8Array> {
const dataEncoder = createEncoder()
const dataEncoder = createBinEncoder()
writeAny(dataEncoder, data)
return encodeToUint8Array(dataEncoder)
}
Expand All @@ -23,7 +23,7 @@ export class BinaryEncoder implements Encoder {

/** Encode any object, including Uint8Array data */
export function encodeJson(data: any): Uint8Array {
const dataEncoder = createEncoder()
const dataEncoder = createBinEncoder()
writeAny(dataEncoder, data)
return encodeToUint8Array(dataEncoder)
}
Expand All @@ -37,7 +37,7 @@ export function decodeJson(data: Uint8Array | ArrayBuffer): any {

/** Incrementally encode binary data */
export function createBinaryStreamEncoder(initialData?: BinInput) {
const dataEncoder = createEncoder()
const dataEncoder = createBinEncoder()

if (initialData != null)
writeUint8Array(dataEncoder, toUint8Array(initialData))
Expand Down
4 changes: 2 additions & 2 deletions src/common/bin/lib0/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { createDecoder, readAny } from './decoding'
import { createEncoder, encodeToUint8Array, writeAny } from './encoding'
import { createBinEncoder, encodeToUint8Array, writeAny } from './encoding'

export function createUint8ArrayFromLen(len: number) {
return new Uint8Array(len)
Expand All @@ -25,7 +25,7 @@ export function copyUint8Array(uint8Array: Uint8Array): Uint8Array {
* See encoding.writeAny for more information.
*/
export function encodeAny(data: any): Uint8Array {
const encoder = createEncoder()
const encoder = createBinEncoder()
writeAny(encoder, data)
return encodeToUint8Array(encoder)
}
Expand Down
62 changes: 31 additions & 31 deletions src/common/bin/lib0/decoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const errorUnexpectedEndOfArray = 'Unexpected end of array'
const errorIntegerOutOfRange = 'Integer out of Range'

/**
* A Decoder handles the decoding of an Uint8Array.
* A BinDecoder handles the decoding of an Uint8Array.
*/
export class Decoder {
export class BinDecoder {
/** Decoding target. */
arr: Uint8Array
/** Current decoding position. */
Expand All @@ -48,19 +48,19 @@ export class Decoder {
}
}

export function createDecoder(uint8Array: Uint8Array): Decoder {
return new Decoder(uint8Array)
export function createDecoder(uint8Array: Uint8Array): BinDecoder {
return new BinDecoder(uint8Array)
}

export function hasContent(decoder: Decoder): boolean {
export function hasContent(decoder: BinDecoder): boolean {
return decoder.pos !== decoder.arr.length
}

/**
* Clone a decoder instance.
* Optionally set a new position parameter.
*/
export function clone(decoder: Decoder, newPos: number = decoder.pos): Decoder {
export function clone(decoder: BinDecoder, newPos: number = decoder.pos): BinDecoder {
const _decoder = createDecoder(decoder.arr)
_decoder.pos = newPos
return _decoder
Expand All @@ -72,7 +72,7 @@ export function clone(decoder: Decoder, newPos: number = decoder.pos): Decoder {
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
*/
export function readUint8Array(decoder: Decoder, len: number): Uint8Array {
export function readUint8Array(decoder: BinDecoder, len: number): Uint8Array {
const view = createUint8ArrayViewFromArrayBuffer(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len)
decoder.pos += len
return view
Expand All @@ -84,7 +84,7 @@ export function readUint8Array(decoder: Decoder, len: number): Uint8Array {
* numbers < 2^7 is stored in one bytlength
* numbers < 2^14 is stored in two bylength
*/
export function readVarUint(decoder: Decoder): number {
export function readVarUint(decoder: BinDecoder): number {
let num = 0
let mult = 1
const len = decoder.arr.length
Expand All @@ -107,35 +107,35 @@ export function readVarUint(decoder: Decoder): number {
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
*/
export function readVarUint8Array(decoder: Decoder): Uint8Array {
export function readVarUint8Array(decoder: BinDecoder): Uint8Array {
return readUint8Array(decoder, readVarUint(decoder))
}

/**
* Read the rest of the content as an ArrayBuffer
*/
export function readTailAsUint8Array(decoder: Decoder): Uint8Array {
export function readTailAsUint8Array(decoder: BinDecoder): Uint8Array {
return readUint8Array(decoder, decoder.arr.length - decoder.pos)
}

/**
* Skip one byte, jump to the next position.
*/
export function skip8(decoder: Decoder): number {
export function skip8(decoder: BinDecoder): number {
return decoder.pos++
}

/**
* Read one byte as unsigned integer.
*/
export function readUint8(decoder: Decoder): number {
export function readUint8(decoder: BinDecoder): number {
return decoder.arr[decoder.pos++]
}

/**
* Read 2 bytes as unsigned integer.
*/
export function readUint16(decoder: Decoder): number {
export function readUint16(decoder: BinDecoder): number {
const uint = decoder.arr[decoder.pos]
+ (decoder.arr[decoder.pos + 1] << 8)
decoder.pos += 2
Expand All @@ -145,7 +145,7 @@ export function readUint16(decoder: Decoder): number {
/**
* Read 4 bytes as unsigned integer.
*/
export function readUint32(decoder: Decoder): number {
export function readUint32(decoder: BinDecoder): number {
const uint = (decoder.arr[decoder.pos]
+ (decoder.arr[decoder.pos + 1] << 8)
+ (decoder.arr[decoder.pos + 2] << 16)
Expand All @@ -158,7 +158,7 @@ export function readUint32(decoder: Decoder): number {
* Read 4 bytes as unsigned integer in big endian order.
* (most significant byte first)
*/
export function readUint32BigEndian(decoder: Decoder): number {
export function readUint32BigEndian(decoder: BinDecoder): number {
const uint = (decoder.arr[decoder.pos + 3]
+ (decoder.arr[decoder.pos + 2] << 8)
+ (decoder.arr[decoder.pos + 1] << 16)
Expand All @@ -171,15 +171,15 @@ export function readUint32BigEndian(decoder: Decoder): number {
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
*/
export function peekUint8(decoder: Decoder): number {
export function peekUint8(decoder: BinDecoder): number {
return decoder.arr[decoder.pos]
}

/**
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
*/
export function peekUint16(decoder: Decoder): number {
export function peekUint16(decoder: BinDecoder): number {
return decoder.arr[decoder.pos]
+ (decoder.arr[decoder.pos + 1] << 8)
}
Expand All @@ -188,7 +188,7 @@ export function peekUint16(decoder: Decoder): number {
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
*/
export function peekUint32(decoder: Decoder): number {
export function peekUint32(decoder: BinDecoder): number {
return (
decoder.arr[decoder.pos]
+ (decoder.arr[decoder.pos + 1] << 8)
Expand All @@ -204,7 +204,7 @@ export function peekUint32(decoder: Decoder): number {
* numbers < 2^14 is stored in two bylength
* @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
*/
export function readVarInt(decoder: Decoder): number {
export function readVarInt(decoder: BinDecoder): number {
let r = decoder.arr[decoder.pos++]
let num = r & BITS6
let mult = 64
Expand All @@ -231,7 +231,7 @@ export function readVarInt(decoder: Decoder): number {
/**
* Look ahead and read varUint without incrementing position
*/
export function peekVarUint(decoder: Decoder): number {
export function peekVarUint(decoder: BinDecoder): number {
const pos = decoder.pos
const s = readVarUint(decoder)
decoder.pos = pos
Expand All @@ -241,7 +241,7 @@ export function peekVarUint(decoder: Decoder): number {
/**
* Look ahead and read varUint without incrementing position
*/
export function peekVarInt(decoder: Decoder): number {
export function peekVarInt(decoder: BinDecoder): number {
const pos = decoder.pos
const s = readVarInt(decoder)
decoder.pos = pos
Expand All @@ -257,7 +257,7 @@ export function peekVarInt(decoder: Decoder): number {
* But most environments have a maximum number of arguments per functions.
* For effiency reasons we apply a maximum of 10000 characters at once.
*/
function _readVarStringPolyfill(decoder: Decoder): string {
function _readVarStringPolyfill(decoder: BinDecoder): string {
let remainingLen = readVarUint(decoder)
if (remainingLen === 0) {
return ''
Expand Down Expand Up @@ -287,7 +287,7 @@ function _readVarStringPolyfill(decoder: Decoder): string {
* Read string of variable length
* varUint is used to store the length of the string
*/
export function readVarString(decoder: Decoder): string {
export function readVarString(decoder: BinDecoder): string {
const utf8TextDecoder = getUtf8TextDecoder()
return utf8TextDecoder
? utf8TextDecoder.decode(readVarUint8Array(decoder))
Expand All @@ -297,36 +297,36 @@ export function readVarString(decoder: Decoder): string {
/**
* Look ahead and read varString without incrementing position
*/
export function peekVarString(decoder: Decoder): string {
export function peekVarString(decoder: BinDecoder): string {
const pos = decoder.pos
const s = readVarString(decoder)
decoder.pos = pos
return s
}

export function readFromDataView(decoder: Decoder, len: number): DataView {
export function readFromDataView(decoder: BinDecoder, len: number): DataView {
const dv = new DataView(decoder.arr.buffer, decoder.arr.byteOffset + decoder.pos, len)
decoder.pos += len
return dv
}

export function readFloat32(decoder: Decoder) {
export function readFloat32(decoder: BinDecoder) {
return readFromDataView(decoder, 4).getFloat32(0, false)
}

export function readFloat64(decoder: Decoder) {
export function readFloat64(decoder: BinDecoder) {
return readFromDataView(decoder, 8).getFloat64(0, false)
}

export function readBigInt64(decoder: Decoder) {
export function readBigInt64(decoder: BinDecoder) {
return (readFromDataView(decoder, 8)).getBigInt64(0, false)
}

export function readBigUint64(decoder: Decoder) {
export function readBigUint64(decoder: BinDecoder) {
return (readFromDataView(decoder, 8)).getBigUint64(0, false)
}

const readAnyLookupTable: Array<((arg0: Decoder) => any)> = [
const readAnyLookupTable: Array<((arg0: BinDecoder) => any)> = [
_ => undefined, // CASE 127: undefined
_ => null, // CASE 126: null
readVarInt, // CASE 125: integer
Expand Down Expand Up @@ -357,6 +357,6 @@ const readAnyLookupTable: Array<((arg0: Decoder) => any)> = [
readVarUint8Array, // CASE 116: Uint8Array
]

export function readAny(decoder: Decoder) {
export function readAny(decoder: BinDecoder) {
return readAnyLookupTable[127 - readUint8(decoder)](decoder)
}
Loading

0 comments on commit 98492a8

Please sign in to comment.