Ultra-fast MessagePack implementation with extensions for records and structured cloning
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Configurable class instances providing advanced MessagePack features like record structures, structured cloning, custom extensions, and performance optimizations.
Main packing class that extends Unpackr to provide both packing and unpacking capabilities with shared configuration.
/**
* Advanced MessagePack encoder/decoder with configurable options
*/
class Packr extends Unpackr {
constructor(options?: Options);
// Buffer management properties
offset: number;
position: number;
// Packing methods
pack(value: any, encodeOptions?: number): Buffer;
encode(value: any, encodeOptions?: number): Buffer;
// Buffer management methods
useBuffer(buffer: Buffer | Uint8Array): void;
clearSharedData(): void;
}Usage Examples:
import { Packr } from "msgpackr";
// Basic usage with default options
const packr = new Packr();
const packed = packr.pack({ name: "Alice", age: 25 });
const unpacked = packr.unpack(packed);
// With record structures for optimal performance
const optimizedPackr = new Packr({
useRecords: true,
structures: []
});
// Reuse structures across multiple objects
const data = [
{ name: "Alice", age: 25, active: true },
{ name: "Bob", age: 30, active: false },
{ name: "Charlie", age: 35, active: true }
];
data.forEach(item => {
const packed = optimizedPackr.pack(item); // Reuses structure definitions
});
// Custom buffer management
const packr2 = new Packr();
const customBuffer = new Uint8Array(1024);
packr2.useBuffer(customBuffer);
const result = packr2.pack(someData);Main unpacking class providing configurable MessagePack decoding with advanced options.
/**
* Advanced MessagePack decoder with configurable options
*/
class Unpackr {
constructor(options?: Options);
// Unpacking methods
unpack(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any;
decode(messagePack: Buffer | Uint8Array, options?: UnpackOptions): any;
unpackMultiple(messagePack: Buffer | Uint8Array): any[];
unpackMultiple(
messagePack: Buffer | Uint8Array,
forEach: (value: any, start?: number, end?: number) => any
): void;
}Usage Examples:
import { Unpackr } from "msgpackr";
// With structured cloning support
const unpackr = new Unpackr({
structuredClone: true,
mapsAsObjects: true
});
// Handle cyclical references
const packedWithCycles = /* buffer with cyclical object */;
const restored = unpackr.unpack(packedWithCycles);
// Object identity and cycles are preserved
// With custom type handling
const typedUnpackr = new Unpackr({
moreTypes: true,
int64AsType: 'bigint',
useTimestamp32: true
});
const result = typedUnpackr.unpack(someTypedData);Alias classes for Packr and Unpackr respectively, providing alternative naming conventions.
/**
* Alias for Packr class
*/
class Encoder extends Packr {}
/**
* Alias for Unpackr class
*/
class Decoder extends Unpackr {}Comprehensive configuration interface for customizing MessagePack behavior.
interface Options {
// Float handling
useFloat32?: FLOAT32_OPTIONS;
// Record structure optimization
useRecords?: boolean | ((value: any) => boolean);
structures?: {}[];
maxSharedStructures?: number;
maxOwnStructures?: number;
shouldShareStructure?: (keys: string[]) => boolean;
getStructures?(): {}[];
saveStructures?(structures: {}[]): boolean | void;
// Type handling
moreTypes?: boolean;
int64AsNumber?: boolean; // deprecated
int64AsType?: 'bigint' | 'number' | 'string';
largeBigIntToFloat?: boolean;
largeBigIntToString?: boolean;
useBigIntExtension?: boolean;
useTimestamp32?: boolean;
// Object mapping
mapsAsObjects?: boolean;
mapAsEmptyObject?: boolean;
setAsEmptyObject?: boolean;
variableMapSize?: boolean;
allowArraysInMapKeys?: boolean;
coercibleKeyAsNumber?: boolean;
// Advanced features
structuredClone?: boolean;
sequential?: boolean;
copyBuffers?: boolean;
bundleStrings?: boolean;
encodeUndefinedAsNil?: boolean;
// Custom handlers
writeFunction?: () => any;
onInvalidDate?: () => any;
}Configuration Examples:
import { Packr, FLOAT32_OPTIONS } from "msgpackr";
// Performance-optimized configuration
const fastPackr = new Packr({
useRecords: true,
useFloat32: FLOAT32_OPTIONS.DECIMAL_FIT,
sequential: true,
bundleStrings: true
});
// Structured cloning configuration
const cloningPackr = new Packr({
structuredClone: true,
moreTypes: true,
copyBuffers: true
});
// Custom type handling
const customPackr = new Packr({
int64AsType: 'string',
largeBigIntToString: true,
useTimestamp32: true,
onInvalidDate: () => null
});
// Record structure with custom sharing logic
const smartPackr = new Packr({
useRecords: (value) => {
// Only use records for objects with 3+ properties
return typeof value === 'object' &&
value !== null &&
Object.keys(value).length >= 3;
},
shouldShareStructure: (keys) => {
// Share structures for common patterns
return keys.includes('id') && keys.includes('name');
}
});Advanced buffer management for high-performance scenarios.
/**
* Use a specific buffer for packing operations
* @param buffer - Buffer or Uint8Array to use for packing
*/
useBuffer(buffer: Buffer | Uint8Array): void;
/**
* Clear shared structure and buffer data
*/
clearSharedData(): void;Usage Examples:
import { Packr } from "msgpackr";
const packr = new Packr({ useRecords: true });
// Pre-allocate buffer for better performance
const buffer = new Uint8Array(4096);
packr.useBuffer(buffer);
// Pack multiple items using the same buffer
const results = [];
for (const item of largeDataset) {
results.push(packr.pack(item));
}
// Clear shared data when switching contexts
packr.clearSharedData();useRecords: true for repeated object structuresuseBuffer() for high-frequency packing operationsFLOAT32_OPTIONS for your datasequential: true for streaming scenariosbundleStrings: true for repeated string values// High-performance configuration example
const highPerformancePackr = new Packr({
useRecords: true,
useFloat32: FLOAT32_OPTIONS.DECIMAL_FIT,
sequential: true,
bundleStrings: true,
maxSharedStructures: 32
});