Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ export type DecodeOptions<ContextType = undefined> = Readonly<
> &
ContextOf<ContextType>;

const getDecoder = (options: any) => new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
options.maxMapLength,
options.maxExtLength,
);

export const defaultDecodeOptions: DecodeOptions = {};
const defaultDecoder = getDecoder(defaultDecodeOptions);

/**
* It decodes a single MessagePack object in a buffer.
Expand All @@ -52,15 +63,7 @@ export function decode<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): unknown {
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
options.maxMapLength,
options.maxExtLength,
);
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
return decoder.decode(buffer);
}

Expand All @@ -72,14 +75,6 @@ export function decodeMulti<ContextType = undefined>(
buffer: ArrayLike<number> | BufferSource,
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
): Generator<unknown, void, unknown> {
const decoder = new Decoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxStrLength,
options.maxBinLength,
options.maxArrayLength,
options.maxMapLength,
options.maxExtLength,
);
const decoder = options === defaultDecodeOptions ? defaultDecoder : getDecoder(options);
return decoder.decodeMulti(buffer);
}
23 changes: 13 additions & 10 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ export type EncodeOptions<ContextType = undefined> = Partial<
> &
ContextOf<ContextType>;

const getEncoder = (options: any) => new Encoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxDepth,
options.initialBufferSize,
options.sortKeys,
options.forceFloat32,
options.ignoreUndefined,
options.forceIntegerToFloat,
);

const defaultEncodeOptions: EncodeOptions = {};
const defaultEncoder = getEncoder(defaultEncodeOptions);

/**
* It encodes `value` in the MessagePack format and
Expand All @@ -67,15 +79,6 @@ export function encode<ContextType = undefined>(
value: unknown,
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
): Uint8Array {
const encoder = new Encoder(
options.extensionCodec,
(options as typeof options & { context: any }).context,
options.maxDepth,
options.initialBufferSize,
options.sortKeys,
options.forceFloat32,
options.ignoreUndefined,
options.forceIntegerToFloat,
);
const encoder = options === defaultEncodeOptions ? defaultEncoder : getEncoder(options);
return encoder.encode(value);
}