Skip to content

Commit

Permalink
Merge branch 'release/release/0.13.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 13, 2023
2 parents 3af3599 + 7c9565c commit 5272084
Show file tree
Hide file tree
Showing 48 changed files with 773 additions and 111 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.13.7",
"version": "0.13.8",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -67,17 +67,17 @@
"watch": "nr build:tsup -- --watch"
},
"devDependencies": {
"@antfu/eslint-config": "^0.41.0",
"@antfu/eslint-config": "^0.41.3",
"@antfu/ni": "^0.21.8",
"@types/node": "<20",
"@types/node": "^20",
"c8": "^8.0.1",
"cross-fetch": "^4.0.0",
"esbuild": "^0.19.2",
"eslint": "^8.48.0",
"eslint": "^8.49.0",
"madge": "^6.1.0",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vitest": "^0.34.3"
"vitest": "^0.34.4"
}
}
5 changes: 4 additions & 1 deletion src/browser/base64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// (C)opyright 2021-07-15 Dirk Holtwick, holtwick.it. All rights reserved.

/** @deprecated */
/**
* @param base64String
* @deprecated
*/
export function urlBase64ToUint8Array(
base64String: string,
): Uint8Array | undefined {
Expand Down
10 changes: 9 additions & 1 deletion src/browser/gravatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
// https://github.com/mazondo/gravatarjs/blob/master/gravatar.js
// https://en.gravatar.com/site/implement/images/

/** @deprecated Due to privacy concerns. Prefer local or custom solutions. */
/**
* @param email
* @param options
* @param options.size
* @param options.backup
* @param options.secure
* @param options.rating
* @deprecated Due to privacy concerns. Prefer local or custom solutions.
*/
function gravatar(
email: string,
options: { size?: any; backup?: any; secure?: any; rating?: any },
Expand Down
6 changes: 5 additions & 1 deletion src/browser/log/log-browser-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { browserSupportsColors } from './log-colors'
* Directly use console calls, this has the advantage to show the original source of the call
* i.e. it is possible to jump right into the code from the browser console logs. But other
* loggers will not work any more.
* @param opt
*/
export function LoggerBrowserSetupDebugFactory(opt: LogHandlerOptions = {}) {
const filter = opt.filter ?? localStorage.zeed ?? localStorage.debug
Expand Down Expand Up @@ -87,7 +88,10 @@ export function LoggerBrowserSetupDebugFactory(opt: LogHandlerOptions = {}) {
}
}

/** @deprecated This output is default for initial use of Logger in browser environments. */
/**
* @param _opt
* @deprecated This output is default for initial use of Logger in browser environments.
*/
export function activateConsoleDebug(_opt: LogHandlerOptions = {}) {
console.info('activateConsoleDebug is activated by default in browsers')
// Logger.setHandlers([LoggerBrowserHandler(opt)]) // Fallback for previously registered Loggers
Expand Down
17 changes: 14 additions & 3 deletions src/common/assert.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import { getGlobalLoggerIfExists } from './log'

/** Always throws. */
/**
* Always throws.
* @param {...any} messages
*/
export function fatal(...messages: any[]): never {
getGlobalLoggerIfExists()?.('assert')?.fatal(...messages)
throw new Error(`${messages.map(String).join(' ')}`)
}

/** Throws if condition is not truthy. */
/**
* Throws if condition is not truthy.
* @param condition
* @param {...any} messages
*/
export function assert(condition: unknown, ...messages: any[]): asserts condition {
if (condition == null || (typeof condition === 'number' && Number.isNaN(condition)) || !condition)
fatal(...messages)
}

/** Alias for `assert` for better differentiation to unit test's assert function. */
/**
* Alias for `assert` for better differentiation to unit test's assert function.
* @param condition
* @param {...any} messages
*/
export function assertCondition(condition: unknown, ...messages: any[]): asserts condition {
assert(condition, ...messages)
}
26 changes: 21 additions & 5 deletions src/common/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,42 @@ export class BinaryEncoder implements Encoder {

// Json like

/** Encode any object, including Uint8Array data */
/**
* Encode any object, including Uint8Array data
* @param data
*/
export function encodeJson(data: any): Uint8Array {
const dataEncoder = createEncoder()
writeAny(dataEncoder, data)
return encodeToUint8Array(dataEncoder)
}

/** Decode any object, including Uint8Array data */
/**
* Decode any object, including Uint8Array data
* @param data
*/
export function decodeJson(data: Uint8Array | ArrayBuffer): any {
return readAny(createDecoder(toUint8Array(data)))
}

// Stream like

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

if (initialData != null)
writeUint8Array(dataEncoder, toUint8Array(initialData))

return {
/** Just writes the bytes. Length is not stored! */
/**
* Just writes the bytes. Length is not stored!
* @param data
* @param expectedLength
*/
writeBytes: (data: BinInput, expectedLength: number) => {
const bin = toUint8Array(data)
assertCondition(!(expectedLength != null && bin.length !== expectedLength), `Expected ${expectedLength} bytes, got ${bin.length}`)
Expand All @@ -63,7 +76,10 @@ export function createBinaryStreamEncoder(initialData?: BinInput) {
}
}

/** Incrementally decode binary data */
/**
* Incrementally decode binary data
* @param data
*/
export function createBinaryStreamDecoder(data: BinInput) {
const dataDecoder = createDecoder(toUint8Array(data))
return {
Expand Down
3 changes: 3 additions & 0 deletions src/common/bin/lib0/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function createUint8ArrayFromLen(len: number) {

/**
* Copy the content of an Uint8Array view to a new ArrayBuffer.
* @param uint8Array
*/
export function copyUint8Array(uint8Array: Uint8Array): Uint8Array {
const newBuf = createUint8ArrayFromLen(uint8Array.byteLength)
Expand All @@ -23,6 +24,7 @@ export function copyUint8Array(uint8Array: Uint8Array): Uint8Array {
/**
* Encode anything as a UInt8Array. It's a pun on typescripts's `any` type.
* See encoding.writeAny for more information.
* @param data
*/
export function encodeAny(data: any): Uint8Array {
const encoder = createEncoder()
Expand All @@ -32,6 +34,7 @@ export function encodeAny(data: any): Uint8Array {

/**
* Decode an any-encoded value.
* @param buf
*/
export function decodeAny(buf: Uint8Array): any {
return readAny(createDecoder(buf))
Expand Down
4 changes: 4 additions & 0 deletions src/common/bin/lib0/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

/**
* Create Uint8Array with initial content from buffer
* @param buffer
* @param byteOffset
* @param length
*/
export function createUint8ArrayViewFromArrayBuffer(buffer: ArrayBuffer, byteOffset: number, length: number) {
return new Uint8Array(buffer, byteOffset, length)
}

/**
* Create Uint8Array with initial content from buffer
* @param buffer
*/
export function createUint8ArrayFromArrayBuffer(buffer: ArrayBuffer) {
return new Uint8Array(buffer)
Expand Down
35 changes: 26 additions & 9 deletions src/common/bin/lib0/decoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export function hasContent(decoder: Decoder): boolean {
/**
* Clone a decoder instance.
* Optionally set a new position parameter.
* @param decoder
* @param newPos
*/
export function clone(decoder: Decoder, newPos: number = decoder.pos): Decoder {
const _decoder = createDecoder(decoder.arr)
Expand All @@ -71,6 +73,8 @@ 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.
* @param decoder
* @param len
*/
export function readUint8Array(decoder: Decoder, len: number): Uint8Array {
const view = createUint8ArrayViewFromArrayBuffer(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len)
Expand All @@ -81,8 +85,9 @@ export function readUint8Array(decoder: Decoder, len: number): Uint8Array {
/**
* Read unsigned integer (32bit) with variable length.
* 1/8th of the storage is used as encoding overhead.
* * numbers < 2^7 is stored in one bytlength
* * numbers < 2^14 is stored in two bylength
* numbers < 2^7 is stored in one bytlength
* numbers < 2^14 is stored in two bylength
* @param decoder
*/
export function readVarUint(decoder: Decoder): number {
let num = 0
Expand All @@ -106,34 +111,39 @@ 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.
* @param decoder
*/
export function readVarUint8Array(decoder: Decoder): Uint8Array {
return readUint8Array(decoder, readVarUint(decoder))
}

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

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

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

/**
* Read 2 bytes as unsigned integer.
* @param decoder
*/
export function readUint16(decoder: Decoder): number {
const uint = decoder.arr[decoder.pos]
Expand All @@ -144,6 +154,7 @@ export function readUint16(decoder: Decoder): number {

/**
* Read 4 bytes as unsigned integer.
* @param decoder
*/
export function readUint32(decoder: Decoder): number {
const uint = (decoder.arr[decoder.pos]
Expand All @@ -157,6 +168,7 @@ export function readUint32(decoder: Decoder): number {
/**
* Read 4 bytes as unsigned integer in big endian order.
* (most significant byte first)
* @param decoder
*/
export function readUint32BigEndian(decoder: Decoder): number {
const uint = (decoder.arr[decoder.pos + 3]
Expand All @@ -170,6 +182,7 @@ export function readUint32BigEndian(decoder: Decoder): number {
/**
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
* @param decoder
*/
export function peekUint8(decoder: Decoder): number {
return decoder.arr[decoder.pos]
Expand All @@ -178,6 +191,7 @@ export function peekUint8(decoder: Decoder): number {
/**
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
* @param decoder
*/
export function peekUint16(decoder: Decoder): number {
return decoder.arr[decoder.pos]
Expand All @@ -187,6 +201,7 @@ export function peekUint16(decoder: Decoder): number {
/**
* Look ahead without incrementing the position
* to the next byte and read it as unsigned integer.
* @param decoder
*/
export function peekUint32(decoder: Decoder): number {
return (
Expand All @@ -200,8 +215,9 @@ export function peekUint32(decoder: Decoder): number {
/**
* Read signed integer (32bit) with variable length.
* 1/8th of the storage is used as encoding overhead.
* * numbers < 2^7 is stored in one bytlength
* * numbers < 2^14 is stored in two bylength
* numbers < 2^7 is stored in one bytlength
* numbers < 2^14 is stored in two bylength
* @param decoder
* @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 {
Expand Down Expand Up @@ -230,6 +246,7 @@ export function readVarInt(decoder: Decoder): number {

/**
* Look ahead and read varUint without incrementing position
* @param decoder
*/
export function peekVarUint(decoder: Decoder): number {
const pos = decoder.pos
Expand All @@ -240,6 +257,7 @@ export function peekVarUint(decoder: Decoder): number {

/**
* Look ahead and read varUint without incrementing position
* @param decoder
*/
export function peekVarInt(decoder: Decoder): number {
const pos = decoder.pos
Expand All @@ -256,6 +274,7 @@ export function peekVarInt(decoder: Decoder): number {
* when String.fromCodePoint is fed with all characters as arguments.
* But most environments have a maximum number of arguments per functions.
* For effiency reasons we apply a maximum of 10000 characters at once.
* @param decoder
*/
function _readVarStringPolyfill(decoder: Decoder): string {
let remainingLen = readVarUint(decoder)
Expand Down Expand Up @@ -285,7 +304,8 @@ function _readVarStringPolyfill(decoder: Decoder): string {

/**
* Read string of variable length
* * varUint is used to store the length of the string
* varUint is used to store the length of the string
* @param decoder
*/
export function readVarString(decoder: Decoder): string {
const utf8TextDecoder = getUtf8TextDecoder()
Expand All @@ -296,6 +316,7 @@ export function readVarString(decoder: Decoder): string {

/**
* Look ahead and read varString without incrementing position
* @param decoder
*/
export function peekVarString(decoder: Decoder): string {
const pos = decoder.pos
Expand Down Expand Up @@ -338,13 +359,9 @@ const readAnyLookupTable: Array<((arg0: Decoder) => any)> = [
readVarString, // CASE 119: string
(decoder) => { // CASE 118: object<string,any>
const len = readVarUint(decoder)
/**
* @type {Object<string,any>}
*/
const obj: { [s: string]: any } = {}
for (let i = 0; i < len; i++) {
const key = readVarString(decoder)

obj[key] = readAny(decoder)
}
return obj
Expand Down
Loading

0 comments on commit 5272084

Please sign in to comment.