micro-packed
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -1,2 +0,2 @@ | ||
import * as base from '@scure/base'; | ||
import type { Coder as BaseCoder } from '@scure/base'; | ||
export declare const EMPTY: Uint8Array; | ||
@@ -7,4 +7,4 @@ export declare const NULL: Uint8Array; | ||
export declare const isBytes: (b: unknown) => b is Uint8Array; | ||
export declare type Bytes = Uint8Array; | ||
export declare type Option<T> = T | undefined; | ||
export type Bytes = Uint8Array; | ||
export type Option<T> = T | undefined; | ||
export interface Coder<F, T> { | ||
@@ -24,25 +24,25 @@ encode(from: F): T; | ||
} | ||
export declare type CoderType<T> = BytesCoderStream<T> & BytesCoder<T>; | ||
export declare type Sized<T> = CoderType<T> & { | ||
export type CoderType<T> = BytesCoderStream<T> & BytesCoder<T>; | ||
export type Sized<T> = CoderType<T> & { | ||
size: number; | ||
}; | ||
export declare type UnwrapCoder<T> = T extends CoderType<infer U> ? U : T; | ||
export declare type Length = CoderType<number> | CoderType<bigint> | number | Bytes | string | null; | ||
declare type ArrLike<T> = Array<T> | ReadonlyArray<T>; | ||
export declare type TypedArray = Uint8Array | Int8Array | Uint8ClampedArray | Uint16Array | Int16Array | Uint32Array | Int32Array; | ||
export declare type Writable<T> = T extends {} ? T extends TypedArray ? T : { | ||
export type UnwrapCoder<T> = T extends CoderType<infer U> ? U : T; | ||
export type Length = CoderType<number> | CoderType<bigint> | number | Bytes | string | null; | ||
type ArrLike<T> = Array<T> | ReadonlyArray<T>; | ||
export type TypedArray = Uint8Array | Int8Array | Uint8ClampedArray | Uint16Array | Int16Array | Uint32Array | Int32Array; | ||
export type Writable<T> = T extends {} ? T extends TypedArray ? T : { | ||
-readonly [P in keyof T]: Writable<T[P]>; | ||
} : T; | ||
declare type Values<T> = T[keyof T]; | ||
declare type NonUndefinedKey<T, K extends keyof T> = T[K] extends undefined ? never : K; | ||
declare type NullableKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? never : K; | ||
declare type OptKey<T, K extends keyof T> = NullableKey<T, K> & NonUndefinedKey<T, K>; | ||
declare type ReqKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? K : never; | ||
declare type OptKeys<T> = Pick<T, { | ||
export type Values<T> = T[keyof T]; | ||
export type NonUndefinedKey<T, K extends keyof T> = T[K] extends undefined ? never : K; | ||
export type NullableKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? never : K; | ||
export type OptKey<T, K extends keyof T> = NullableKey<T, K> & NonUndefinedKey<T, K>; | ||
export type ReqKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? K : never; | ||
export type OptKeys<T> = Pick<T, { | ||
[K in keyof T]: OptKey<T, K>; | ||
}[keyof T]>; | ||
declare type ReqKeys<T> = Pick<T, { | ||
export type ReqKeys<T> = Pick<T, { | ||
[K in keyof T]: ReqKey<T, K>; | ||
}[keyof T]>; | ||
declare type StructInput<T extends Record<string, any>> = { | ||
export type StructInput<T extends Record<string, any>> = { | ||
[P in keyof ReqKeys<T>]: T[P]; | ||
@@ -52,7 +52,7 @@ } & { | ||
}; | ||
declare type StructRecord<T extends Record<string, any>> = { | ||
export type StructRecord<T extends Record<string, any>> = { | ||
[P in keyof T]: CoderType<T[P]>; | ||
}; | ||
declare type StructOut = Record<string, any>; | ||
declare type PadFn = (i: number) => number; | ||
export type StructOut = Record<string, any>; | ||
export type PadFn = (i: number) => number; | ||
export declare class Reader { | ||
@@ -105,4 +105,4 @@ readonly data: Bytes; | ||
export declare function isCoder<T>(elm: any): elm is CoderType<T>; | ||
declare function dict<T>(): base.Coder<[string, T][], Record<string, T>>; | ||
declare type Enum = { | ||
declare function dict<T>(): BaseCoder<[string, T][], Record<string, T>>; | ||
type Enum = { | ||
[k: string]: number | string; | ||
@@ -112,4 +112,4 @@ } & { | ||
}; | ||
declare type EnumKeys<T extends Enum> = keyof T; | ||
declare function tsEnum<T extends Enum>(e: T): base.Coder<number, EnumKeys<T>>; | ||
type EnumKeys<T extends Enum> = keyof T; | ||
declare function tsEnum<T extends Enum>(e: T): BaseCoder<number, EnumKeys<T>>; | ||
declare function decimal(precision: number): { | ||
@@ -119,12 +119,12 @@ encode: (from: bigint) => string; | ||
}; | ||
declare type BaseInput<F> = F extends base.Coder<infer T, any> ? T : never; | ||
declare type BaseOutput<F> = F extends base.Coder<any, infer T> ? T : never; | ||
declare function match<L extends base.Coder<unknown | undefined, unknown | undefined>[], I = { | ||
type BaseInput<F> = F extends BaseCoder<infer T, any> ? T : never; | ||
type BaseOutput<F> = F extends BaseCoder<any, infer T> ? T : never; | ||
declare function match<L extends BaseCoder<unknown | undefined, unknown | undefined>[], I = { | ||
[K in keyof L]: NonNullable<BaseInput<L[K]>>; | ||
}[number], O = { | ||
[K in keyof L]: NonNullable<BaseOutput<L[K]>>; | ||
}[number]>(lst: L): base.Coder<I, O>; | ||
}[number]>(lst: L): BaseCoder<I, O>; | ||
export declare const coders: { | ||
dict: typeof dict; | ||
number: base.Coder<bigint, number>; | ||
number: BaseCoder<bigint, number>; | ||
tsEnum: typeof tsEnum; | ||
@@ -164,6 +164,6 @@ decimal: typeof decimal; | ||
export declare const hex: (len: Length, le?: boolean, withZero?: boolean) => CoderType<string>; | ||
export declare function apply<T, F>(inner: CoderType<T>, b: base.Coder<T, F>): CoderType<F>; | ||
export declare function apply<T, F>(inner: CoderType<T>, b: BaseCoder<T, F>): CoderType<F>; | ||
export declare function validate<T>(inner: CoderType<T>, fn: (elm: T) => T): CoderType<T>; | ||
export declare function lazy<T>(fn: () => CoderType<T>): CoderType<T>; | ||
declare type baseFmt = 'utf8' | 'hex' | 'base16' | 'base32' | 'base64' | 'base64url' | 'base58' | 'base58xmr'; | ||
type baseFmt = 'utf8' | 'hex' | 'base16' | 'base32' | 'base64' | 'base64url' | 'base58' | 'base58xmr'; | ||
export declare const bytesFormatted: (len: Length, fmt: baseFmt, le?: boolean) => BytesCoderStream<string> & BytesCoder<string>; | ||
@@ -180,3 +180,3 @@ export declare const flag: (flagValue: Bytes, xor?: boolean) => CoderType<boolean>; | ||
}>>(fields: T): CoderType<O>; | ||
declare type PrefixLength = string | number | CoderType<number> | CoderType<bigint>; | ||
type PrefixLength = string | number | CoderType<number> | CoderType<bigint>; | ||
export declare function prefix<T>(len: PrefixLength, inner: CoderType<T>): CoderType<T>; | ||
@@ -183,0 +183,0 @@ export declare function array<T>(len: Length, inner: CoderType<T>): CoderType<T[]>; |
28
index.js
@@ -1,2 +0,2 @@ | ||
import * as base from '@scure/base'; | ||
import { base64, bytes as baseBytes, hex as baseHex, str as baseStr, utf8 } from '@scure/base'; | ||
export const EMPTY = new Uint8Array(); | ||
@@ -117,3 +117,3 @@ export const NULL = new Uint8Array([0]); | ||
return; | ||
throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${base.hex.encode(this.data.slice(this.pos))}`); | ||
throw this.err(`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${baseHex.encode(this.data.slice(this.pos))}`); | ||
} | ||
@@ -449,4 +449,4 @@ fieldPathPush(s) { | ||
size: inner.size, | ||
encodeStream: (w, value) => inner.encodeStream(w, base.utf8.decode(value)), | ||
decodeStream: (r) => base.utf8.encode(inner.decodeStream(r)), | ||
encodeStream: (w, value) => inner.encodeStream(w, utf8.decode(value)), | ||
decodeStream: (r) => utf8.encode(inner.decodeStream(r)), | ||
}); | ||
@@ -462,6 +462,6 @@ }; | ||
throw new Error('hex(withZero=true).encode input should start with 0x'); | ||
const bytes = base.hex.decode(withZero ? value.slice(2) : value); | ||
const bytes = baseHex.decode(withZero ? value.slice(2) : value); | ||
return inner.encodeStream(w, bytes); | ||
}, | ||
decodeStream: (r) => (withZero ? '0x' : '') + base.hex.encode(inner.decodeStream(r)), | ||
decodeStream: (r) => (withZero ? '0x' : '') + baseHex.encode(inner.decodeStream(r)), | ||
}); | ||
@@ -514,4 +514,4 @@ }; | ||
size: inner.size, | ||
encodeStream: (w, value) => inner.encodeStream(w, base.bytes(fmt, value)), | ||
decodeStream: (r) => base.str(fmt, inner.decodeStream(r)), | ||
encodeStream: (w, value) => inner.encodeStream(w, baseBytes(fmt, value)), | ||
decodeStream: (r) => baseStr(fmt, inner.decodeStream(r)), | ||
}); | ||
@@ -604,3 +604,3 @@ }; | ||
export const magicBytes = (constant) => { | ||
const c = typeof constant === 'string' ? base.utf8.decode(constant) : constant; | ||
const c = typeof constant === 'string' ? utf8.decode(constant) : constant; | ||
return magic(bytes(c.length), c); | ||
@@ -931,3 +931,3 @@ }; | ||
const data = inner.encode(value); | ||
const encoded = base.base64.encode(data); | ||
const encoded = base64.encode(data); | ||
let lines = []; | ||
@@ -941,3 +941,3 @@ for (let i = 0; i < encoded.length; i += lineLen) { | ||
if (checksum) | ||
body += `=${base.base64.encode(checksum(data))}\n`; | ||
body += `=${base64.encode(checksum(data))}\n`; | ||
return `${markBegin}\n\n${body}${markEnd}\n`; | ||
@@ -949,5 +949,5 @@ }, | ||
if (checksum && lines[lines.length - 1].startsWith('=')) { | ||
const body = base.base64.decode(lines.slice(0, -1).join('')); | ||
const body = base64.decode(lines.slice(0, -1).join('')); | ||
const cs = lines[lines.length - 1].slice(1); | ||
const realCS = base.base64.encode(checksum(body)); | ||
const realCS = base64.encode(checksum(body)); | ||
if (realCS !== cs) | ||
@@ -957,3 +957,3 @@ throw new Error(`Base64Armor: invalid checksum ${cs} instead of ${realCS}`); | ||
} | ||
return inner.decode(base.base64.decode(lines.join(''))); | ||
return inner.decode(base64.decode(lines.join(''))); | ||
}, | ||
@@ -960,0 +960,0 @@ }; |
84
index.ts
@@ -1,2 +0,3 @@ | ||
import * as base from '@scure/base'; | ||
import { base64, bytes as baseBytes, hex as baseHex, str as baseStr, utf8 } from '@scure/base'; | ||
import type { Coder as BaseCoder } from '@scure/base'; | ||
@@ -13,4 +14,4 @@ /** | ||
// Useful default values | ||
export const EMPTY = new Uint8Array(); // Empty bytes array | ||
export const NULL = new Uint8Array([0]); // NULL | ||
export const EMPTY = /* @__PURE__ */ new Uint8Array(); // Empty bytes array | ||
export const NULL = /* @__PURE__ */ new Uint8Array([0]); // NULL | ||
@@ -83,22 +84,22 @@ export function equalBytes(a: Uint8Array, b: Uint8Array): boolean { | ||
type Values<T> = T[keyof T]; | ||
type NonUndefinedKey<T, K extends keyof T> = T[K] extends undefined ? never : K; | ||
type NullableKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? never : K; | ||
export type Values<T> = T[keyof T]; | ||
export type NonUndefinedKey<T, K extends keyof T> = T[K] extends undefined ? never : K; | ||
export type NullableKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? never : K; | ||
// Opt: value !== undefined, but value === T|undefined | ||
type OptKey<T, K extends keyof T> = NullableKey<T, K> & NonUndefinedKey<T, K>; | ||
type ReqKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? K : never; | ||
export type OptKey<T, K extends keyof T> = NullableKey<T, K> & NonUndefinedKey<T, K>; | ||
export type ReqKey<T, K extends keyof T> = T[K] extends NonNullable<T[K]> ? K : never; | ||
type OptKeys<T> = Pick<T, { [K in keyof T]: OptKey<T, K> }[keyof T]>; | ||
type ReqKeys<T> = Pick<T, { [K in keyof T]: ReqKey<T, K> }[keyof T]>; | ||
export type OptKeys<T> = Pick<T, { [K in keyof T]: OptKey<T, K> }[keyof T]>; | ||
export type ReqKeys<T> = Pick<T, { [K in keyof T]: ReqKey<T, K> }[keyof T]>; | ||
type StructInput<T extends Record<string, any>> = { [P in keyof ReqKeys<T>]: T[P] } & { | ||
export type StructInput<T extends Record<string, any>> = { [P in keyof ReqKeys<T>]: T[P] } & { | ||
[P in keyof OptKeys<T>]?: T[P]; | ||
}; | ||
type StructRecord<T extends Record<string, any>> = { | ||
export type StructRecord<T extends Record<string, any>> = { | ||
[P in keyof T]: CoderType<T[P]>; | ||
}; | ||
type StructOut = Record<string, any>; | ||
type PadFn = (i: number) => number; | ||
export type StructOut = Record<string, any>; | ||
export type PadFn = (i: number) => number; | ||
@@ -184,3 +185,3 @@ // Utils | ||
throw this.err( | ||
`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${base.hex.encode( | ||
`${this.leftBytes} bytes ${this.bitPos} bits left after unpack: ${baseHex.encode( | ||
this.data.slice(this.pos) | ||
@@ -204,3 +205,6 @@ )}` | ||
bitPos = 0; | ||
constructor(public path: StructOut[] = [], public fieldPath: string[] = []) {} | ||
constructor( | ||
public path: StructOut[] = [], | ||
public fieldPath: string[] = [] | ||
) {} | ||
err(msg: string) { | ||
@@ -323,3 +327,3 @@ return new Error(`Writer(${this.fieldPath.join('/')}): ${msg}`); | ||
// - encode/decode -> from/to? coder->convert? | ||
function dict<T>(): base.Coder<[string, T][], Record<string, T>> { | ||
function dict<T>(): BaseCoder<[string, T][], Record<string, T>> { | ||
return { | ||
@@ -341,3 +345,3 @@ encode: (from: [string, T][]): Record<string, T> => { | ||
// but we still can use them since real value will be smaller than u32 | ||
const number: base.Coder<bigint, number> = { | ||
const number: BaseCoder<bigint, number> = { | ||
encode: (from: bigint): number => { | ||
@@ -354,3 +358,3 @@ if (from > BigInt(Number.MAX_SAFE_INTEGER)) | ||
type EnumKeys<T extends Enum> = keyof T; | ||
function tsEnum<T extends Enum>(e: T): base.Coder<number, EnumKeys<T>> { | ||
function tsEnum<T extends Enum>(e: T): BaseCoder<number, EnumKeys<T>> { | ||
return { | ||
@@ -399,4 +403,4 @@ encode: (from: number): string => e[from], | ||
// TODO: export from @scure/base? | ||
type BaseInput<F> = F extends base.Coder<infer T, any> ? T : never; | ||
type BaseOutput<F> = F extends base.Coder<any, infer T> ? T : never; | ||
type BaseInput<F> = F extends BaseCoder<infer T, any> ? T : never; | ||
type BaseOutput<F> = F extends BaseCoder<any, infer T> ? T : never; | ||
@@ -416,6 +420,6 @@ /** | ||
function match< | ||
L extends base.Coder<unknown | undefined, unknown | undefined>[], | ||
L extends BaseCoder<unknown | undefined, unknown | undefined>[], | ||
I = { [K in keyof L]: NonNullable<BaseInput<L[K]>> }[number], | ||
O = { [K in keyof L]: NonNullable<BaseOutput<L[K]>> }[number] | ||
>(lst: L): base.Coder<I, O> { | ||
O = { [K in keyof L]: NonNullable<BaseOutput<L[K]>> }[number], | ||
>(lst: L): BaseCoder<I, O> { | ||
return { | ||
@@ -551,4 +555,4 @@ encode: (from: I): O => { | ||
size: inner.size, | ||
encodeStream: (w: Writer, value: string) => inner.encodeStream(w, base.utf8.decode(value)), | ||
decodeStream: (r: Reader): string => base.utf8.encode(inner.decodeStream(r)), | ||
encodeStream: (w: Writer, value: string) => inner.encodeStream(w, utf8.decode(value)), | ||
decodeStream: (r: Reader): string => utf8.encode(inner.decodeStream(r)), | ||
}); | ||
@@ -566,7 +570,7 @@ }; | ||
throw new Error('hex(withZero=true).encode input should start with 0x'); | ||
const bytes = base.hex.decode(withZero ? value.slice(2) : value); | ||
const bytes = baseHex.decode(withZero ? value.slice(2) : value); | ||
return inner.encodeStream(w, bytes); | ||
}, | ||
decodeStream: (r: Reader): string => | ||
(withZero ? '0x' : '') + base.hex.encode(inner.decodeStream(r)), | ||
(withZero ? '0x' : '') + baseHex.encode(inner.decodeStream(r)), | ||
}); | ||
@@ -576,3 +580,3 @@ }; | ||
// Interoperability with base | ||
export function apply<T, F>(inner: CoderType<T>, b: base.Coder<T, F>): CoderType<F> { | ||
export function apply<T, F>(inner: CoderType<T>, b: BaseCoder<T, F>): CoderType<F> { | ||
if (!isCoder(inner)) throw new Error(`apply: invalid inner value ${inner}`); | ||
@@ -632,4 +636,4 @@ return wrap({ | ||
size: inner.size, | ||
encodeStream: (w: Writer, value: string) => inner.encodeStream(w, base.bytes(fmt, value)), | ||
decodeStream: (r: Reader): string => base.str(fmt, inner.decodeStream(r)), | ||
encodeStream: (w: Writer, value: string) => inner.encodeStream(w, baseBytes(fmt, value)), | ||
decodeStream: (r: Reader): string => baseStr(fmt, inner.decodeStream(r)), | ||
}); | ||
@@ -726,3 +730,3 @@ }; | ||
export const magicBytes = (constant: Bytes | string): CoderType<undefined> => { | ||
const c = typeof constant === 'string' ? base.utf8.decode(constant) : constant; | ||
const c = typeof constant === 'string' ? utf8.decode(constant) : constant; | ||
return magic(bytes(c.length), c); | ||
@@ -783,3 +787,3 @@ }; | ||
T extends ArrLike<CoderType<any>>, | ||
O = Writable<{ [K in keyof T]: UnwrapCoder<T[K]> }> | ||
O = Writable<{ [K in keyof T]: UnwrapCoder<T[K]> }>, | ||
>(fields: T): CoderType<O> { | ||
@@ -930,3 +934,3 @@ if (!Array.isArray(fields)) | ||
TagValue extends string | number, | ||
Variants extends Record<TagValue, CoderType<any>> | ||
Variants extends Record<TagValue, CoderType<any>>, | ||
>(tag: CoderType<TagValue>, variants: Variants): CoderType<T> { | ||
@@ -957,3 +961,3 @@ if (!isCoder(tag)) throw new Error(`tag: invalid tag value ${tag}`); | ||
TagValue extends string | number, | ||
Variants extends Record<string, [TagValue, CoderType<any>]> | ||
Variants extends Record<string, [TagValue, CoderType<any>]>, | ||
>(tagCoder: CoderType<TagValue>, variants: Variants): CoderType<T> { | ||
@@ -1082,3 +1086,3 @@ if (!isCoder(tagCoder)) throw new Error(`mappedTag: invalid tag value ${tag}`); | ||
const data = inner.encode(value); | ||
const encoded = base.base64.encode(data); | ||
const encoded = base64.encode(data); | ||
let lines = []; | ||
@@ -1090,3 +1094,3 @@ for (let i = 0; i < encoded.length; i += lineLen) { | ||
let body = lines.join(''); | ||
if (checksum) body += `=${base.base64.encode(checksum(data))}\n`; | ||
if (checksum) body += `=${base64.encode(checksum(data))}\n`; | ||
return `${markBegin}\n\n${body}${markEnd}\n`; | ||
@@ -1098,5 +1102,5 @@ }, | ||
if (checksum && lines[lines.length - 1].startsWith('=')) { | ||
const body = base.base64.decode(lines.slice(0, -1).join('')); | ||
const body = base64.decode(lines.slice(0, -1).join('')); | ||
const cs = lines[lines.length - 1].slice(1); | ||
const realCS = base.base64.encode(checksum(body)); | ||
const realCS = base64.encode(checksum(body)); | ||
if (realCS !== cs) | ||
@@ -1106,3 +1110,3 @@ throw new Error(`Base64Armor: invalid checksum ${cs} instead of ${realCS}`); | ||
} | ||
return inner.decode(base.base64.decode(lines.join(''))); | ||
return inner.decode(base64.decode(lines.join(''))); | ||
}, | ||
@@ -1109,0 +1113,0 @@ }; |
{ | ||
"name": "micro-packed", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Less painful binary encoding / decoding", | ||
@@ -18,2 +18,3 @@ "files": [ | ||
"lint": "prettier --check index.ts", | ||
"format": "prettier --write index.ts", | ||
"test": "node test/index.js" | ||
@@ -29,8 +30,8 @@ }, | ||
"dependencies": { | ||
"@scure/base": "~1.1.1" | ||
"@scure/base": "~1.1.3" | ||
}, | ||
"devDependencies": { | ||
"micro-should": "0.4.0", | ||
"prettier": "2.6.2", | ||
"typescript": "4.7.3" | ||
"prettier": "3.1.1", | ||
"typescript": "5.3.2" | ||
}, | ||
@@ -37,0 +38,0 @@ "keywords": [ |
@@ -38,2 +38,21 @@ # micro-packed | ||
- [Array](#array) | ||
- [Bytes](#bytes) | ||
- [String](#string) | ||
- [Tuple](#tuple) | ||
- [Map](#map) | ||
- [Tag](#tag) | ||
- [Magic](#magic) | ||
- [Bits](#bits) | ||
- [Pointer](#pointer) | ||
- [Padding](#padding) | ||
- [Flag](#flag) | ||
- [Flagged](#flagged) | ||
- [Optional](#optional) | ||
- [Lazy](#lazy) | ||
- [Dict](#dict) | ||
- [Validate](#validate) | ||
- [Debug](#debug) | ||
- [Primitive types](#primitive-types) | ||
### Array | ||
@@ -40,0 +59,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99966
2203
278
Updated@scure/base@~1.1.3