@mcintyre94/codecs-core
Advanced tools
Comparing version 2.0.0-experimental.993878f to 2.0.0-experimental.f9b30bb
@@ -1,8 +0,148 @@ | ||
export * from './assertions.js'; | ||
export * from './bytes.js'; | ||
export * from './codec.js'; | ||
export * from './combine-codec.js'; | ||
export * from './fix-codec.js'; | ||
export * from './map-codec.js'; | ||
export * from './reverse-codec.js'; | ||
//# sourceMappingURL=index.d.ts.map | ||
/** | ||
* Defines an offset in bytes. | ||
*/ | ||
type Offset = number; | ||
/** | ||
* The shared attributes between codecs, encoders and decoders. | ||
*/ | ||
type CodecData = { | ||
/** A description for the codec. */ | ||
description: string; | ||
/** The fixed size of the encoded value in bytes, or `null` if it is variable. */ | ||
fixedSize: number | null; | ||
/** The maximum size an encoded value can be in bytes, or `null` if it is variable. */ | ||
maxSize: number | null; | ||
}; | ||
/** | ||
* An object that can encode a value to a `Uint8Array`. | ||
*/ | ||
type Encoder<T> = CodecData & { | ||
/** The function that encodes a value into bytes. */ | ||
encode: (value: T) => Uint8Array; | ||
}; | ||
/** | ||
* An object that can decode a value from a `Uint8Array`. | ||
*/ | ||
type Decoder<T> = CodecData & { | ||
/** | ||
* The function that decodes a value from bytes. | ||
* It returns the decoded value and the number of bytes read. | ||
*/ | ||
decode: (bytes: Uint8Array, offset?: Offset) => [T, Offset]; | ||
}; | ||
/** | ||
* An object that can encode and decode a value to and from a `Uint8Array`. | ||
* It supports encoding looser types than it decodes for convenience. | ||
* For example, a `bigint` encoder will always decode to a `bigint` | ||
* but can be used to encode a `number`. | ||
* | ||
* @typeParam From - The type of the value to encode. | ||
* @typeParam To - The type of the decoded value. Defaults to `From`. | ||
*/ | ||
type Codec<From, To extends From = From> = Encoder<From> & Decoder<To>; | ||
/** | ||
* Defines common configurations for codec factories. | ||
*/ | ||
type BaseCodecConfig = { | ||
/** A custom description for the Codec. */ | ||
description?: string; | ||
}; | ||
/** | ||
* Wraps all the attributes of an object in Codecs. | ||
*/ | ||
type WrapInCodec<T, U extends T = T> = { | ||
[P in keyof T]: Codec<T[P], U[P]>; | ||
}; | ||
/** | ||
* Asserts that a given byte array is not empty. | ||
*/ | ||
declare function assertByteArrayIsNotEmptyForCodec(codecDescription: string, bytes: Uint8Array, offset?: number): void; | ||
/** | ||
* Asserts that a given byte array has enough bytes to decode. | ||
*/ | ||
declare function assertByteArrayHasEnoughBytesForCodec(codecDescription: string, expected: number, bytes: Uint8Array, offset?: number): void; | ||
/** | ||
* Asserts that a given codec is fixed-size codec. | ||
*/ | ||
declare function assertFixedSizeCodec(data: Pick<CodecData, 'fixedSize'>, message?: string): asserts data is { | ||
fixedSize: number; | ||
}; | ||
/** | ||
* Concatenates an array of `Uint8Array`s into a single `Uint8Array`. | ||
* Reuses the original byte array when applicable. | ||
*/ | ||
declare const mergeBytes: (byteArrays: Uint8Array[]) => Uint8Array; | ||
/** | ||
* Pads a `Uint8Array` with zeroes to the specified length. | ||
* If the array is longer than the specified length, it is returned as-is. | ||
*/ | ||
declare const padBytes: (bytes: Uint8Array, length: number) => Uint8Array; | ||
/** | ||
* Fixes a `Uint8Array` to the specified length. | ||
* If the array is longer than the specified length, it is truncated. | ||
* If the array is shorter than the specified length, it is padded with zeroes. | ||
*/ | ||
declare const fixBytes: (bytes: Uint8Array, length: number) => Uint8Array; | ||
/** | ||
* Combines an encoder and a decoder into a codec. | ||
* The encoder and decoder must have the same fixed size, max size and description. | ||
* If a description is provided, it will override the encoder and decoder descriptions. | ||
*/ | ||
declare function combineCodec<From, To extends From = From>(encoder: Encoder<From>, decoder: Decoder<To>, description?: string): Codec<From, To>; | ||
/** | ||
* Creates a fixed-size encoder from a given encoder. | ||
* | ||
* @param encoder - The encoder to wrap into a fixed-size encoder. | ||
* @param fixedBytes - The fixed number of bytes to write. | ||
* @param description - A custom description for the encoder. | ||
*/ | ||
declare function fixEncoder<T>(encoder: Encoder<T>, fixedBytes: number, description?: string): Encoder<T>; | ||
/** | ||
* Creates a fixed-size decoder from a given decoder. | ||
* | ||
* @param decoder - The decoder to wrap into a fixed-size decoder. | ||
* @param fixedBytes - The fixed number of bytes to read. | ||
* @param description - A custom description for the decoder. | ||
*/ | ||
declare function fixDecoder<T>(decoder: Decoder<T>, fixedBytes: number, description?: string): Decoder<T>; | ||
/** | ||
* Creates a fixed-size codec from a given codec. | ||
* | ||
* @param codec - The codec to wrap into a fixed-size codec. | ||
* @param fixedBytes - The fixed number of bytes to read/write. | ||
* @param description - A custom description for the codec. | ||
*/ | ||
declare function fixCodec<T, U extends T = T>(codec: Codec<T, U>, fixedBytes: number, description?: string): Codec<T, U>; | ||
/** | ||
* Converts an encoder A to a encoder B by mapping their values. | ||
*/ | ||
declare function mapEncoder<T, U>(encoder: Encoder<T>, unmap: (value: U) => T): Encoder<U>; | ||
/** | ||
* Converts an decoder A to a decoder B by mapping their values. | ||
*/ | ||
declare function mapDecoder<T, U>(decoder: Decoder<T>, map: (value: T, bytes: Uint8Array, offset: number) => U): Decoder<U>; | ||
/** | ||
* Converts a codec A to a codec B by mapping their values. | ||
*/ | ||
declare function mapCodec<NewFrom, OldFrom, To extends NewFrom & OldFrom>(codec: Codec<OldFrom, To>, unmap: (value: NewFrom) => OldFrom): Codec<NewFrom, To>; | ||
declare function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(codec: Codec<OldFrom, OldTo>, unmap: (value: NewFrom) => OldFrom, map: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo): Codec<NewFrom, NewTo>; | ||
/** | ||
* Reverses the bytes of a fixed-size encoder. | ||
*/ | ||
declare function reverseEncoder<T>(encoder: Encoder<T>): Encoder<T>; | ||
/** | ||
* Reverses the bytes of a fixed-size decoder. | ||
*/ | ||
declare function reverseDecoder<T>(decoder: Decoder<T>): Decoder<T>; | ||
/** | ||
* Reverses the bytes of a fixed-size codec. | ||
*/ | ||
declare function reverseCodec<T, U extends T = T>(codec: Codec<T, U>): Codec<T, U>; | ||
export { type BaseCodecConfig, type Codec, type CodecData, type Decoder, type Encoder, type Offset, type WrapInCodec, assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertFixedSizeCodec, combineCodec, fixBytes, fixCodec, fixDecoder, fixEncoder, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; |
{ | ||
"name": "@mcintyre94/codecs-core", | ||
"version": "2.0.0-experimental.993878f", | ||
"version": "2.0.0-experimental.f9b30bb", | ||
"description": "Core types and helpers for encoding and decoding byte arrays on Solana", | ||
@@ -60,3 +60,2 @@ "exports": { | ||
"eslint-plugin-jest": "^27.4.2", | ||
"eslint-plugin-require-extensions": "^0.1.3", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
@@ -87,2 +86,3 @@ "jest": "^29.7.0", | ||
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch", | ||
"publish:typedefs": "rm -r dist/types && pnpm tsup --config build-scripts/tsup.config.library.ts --dts-only --out-dir dist/types", | ||
"publish-packages": "pnpm publish --tag experimental --access public --no-git-checks", | ||
@@ -89,0 +89,0 @@ "style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/* package.json", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
151946
20
19
1368