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I,CAAC,CAAC,CAAG,EAAA,CAAC,MAAM,CAAC,CAAA,EAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA,CAAA;AAAA,GACjF;AACA,EAAO,OAAA,KAAA,CAAA;AACX","file":"index.node.mjs","sourcesContent":["/**\n * An implementation of the Rust Option type in JavaScript.\n * It can be one of the following:\n * - {@link Some}: Meaning there is a value of type T.\n * - {@link None}: Meaning there is no value.\n */\nexport type Option = None | Some;\n\n/**\n * Defines a looser type that can be used when serializing an {@link Option}.\n * This allows us to pass null or the Option value directly whilst still\n * supporting the Option type for use-cases that need more type safety.\n */\nexport type OptionOrNullable = Option | T | null;\n\n/**\n * Represents an option of type `T` that has a value.\n *\n * @see {@link Option}\n */\nexport type Some = Readonly<{ __option: 'Some'; value: T }>;\n\n/**\n * Represents an option of type `T` that has no value.\n *\n * @see {@link Option}\n */\nexport type None = Readonly<{ __option: 'None' }>;\n\n/**\n * Creates a new {@link Option} of type `T` that has a value.\n *\n * @see {@link Option}\n */\nexport const some = (value: T): Option => ({ __option: 'Some', value });\n\n/**\n * Creates a new {@link Option} of type `T` that has no value.\n *\n * @see {@link Option}\n */\nexport const none = (): Option => ({ __option: 'None' });\n\n/**\n * Whether the given data is an {@link Option}.\n */\nexport const isOption = (input: unknown): input is Option =>\n !!(\n input &&\n typeof input === 'object' &&\n '__option' in input &&\n ((input.__option === 'Some' && 'value' in input) || input.__option === 'None')\n );\n\n/**\n * Whether the given {@link Option} is a {@link Some}.\n */\nexport const isSome = (option: Option): option is Some => option.__option === 'Some';\n\n/**\n * Whether the given {@link Option} is a {@link None}.\n */\nexport const isNone = (option: Option): option is None => option.__option === 'None';\n","import { isSome, none, Option, some } from './option';\n\n/**\n * Unwraps the value of an {@link Option} of type `T`\n * or returns a fallback value that defaults to `null`.\n */\nexport function unwrapOption(option: Option): T | null;\nexport function unwrapOption(option: Option, fallback: () => U): T | U;\nexport function unwrapOption(option: Option, fallback?: () => U): T | U {\n if (isSome(option)) return option.value;\n return fallback ? fallback() : (null as U);\n}\n\n/**\n * Wraps a nullable value into an {@link Option}.\n */\nexport const wrapNullable = (nullable: T | null): Option => (nullable !== null ? some(nullable) : none());\n","import {\n assertIsFixedSize,\n Codec,\n combineCodec,\n containsBytes,\n Decoder,\n Encoder,\n fixDecoderSize,\n FixedSizeCodec,\n FixedSizeDecoder,\n FixedSizeEncoder,\n fixEncoderSize,\n ReadonlyUint8Array,\n transformDecoder,\n transformEncoder,\n VariableSizeCodec,\n VariableSizeDecoder,\n VariableSizeEncoder,\n} from '@solana/codecs-core';\nimport {\n getBooleanDecoder,\n getBooleanEncoder,\n getConstantDecoder,\n getConstantEncoder,\n getTupleDecoder,\n getTupleEncoder,\n getUnionDecoder,\n getUnionEncoder,\n getUnitDecoder,\n getUnitEncoder,\n} from '@solana/codecs-data-structures';\nimport {\n FixedSizeNumberCodec,\n FixedSizeNumberDecoder,\n FixedSizeNumberEncoder,\n getU8Decoder,\n getU8Encoder,\n NumberCodec,\n NumberDecoder,\n NumberEncoder,\n} from '@solana/codecs-numbers';\n\nimport { isOption, isSome, None, none, Option, OptionOrNullable, Some, some } from './option';\nimport { wrapNullable } from './unwrap-option';\n\n/** Defines the config for Option codecs. */\nexport type OptionCodecConfig = {\n /**\n * Defines how the `None` value should be represented.\n *\n * By default, no none value is used. This means a `None` value will be\n * represented by the absence of the item.\n *\n * When `'zeroes'` is provided, a `None` value will skip the bytes that would\n * have been used for the item. Note that this returns a fixed-size codec\n * and thus will only work if the item codec is of fixed size.\n *\n * When a custom byte array is provided, a `None` value will be represented\n * by the provided byte array. Note that this returns a variable-size codec\n * since the byte array representing `None` does not need to match the size\n * of the item codec.\n *\n * @defaultValue No none value is used.\n */\n noneValue?: ReadonlyUint8Array | 'zeroes';\n\n /**\n * The codec to use for the boolean prefix, if any.\n *\n * By default a `u8` number is used as a prefix to determine if the value is `None`.\n * The value `0` is encoded for `None` and `1` if the value is present.\n * This can be set to any number codec to customize the prefix.\n *\n * When `null` is provided, no prefix is used and the `noneValue` is used to\n * determine if the value is `None`. If no `noneValue` is provided, then the\n * absence of any bytes is used to determine if the value is `None`.\n *\n * @defaultValue `u8` prefix.\n */\n prefix?: TPrefix | null;\n};\n\n/**\n * Creates a encoder for an optional value using the `Option` type.\n *\n * @param item - The encoder to use for the value that may be present.\n * @param config - A set of config for the encoder.\n */\nexport function getOptionEncoder(\n item: FixedSizeEncoder,\n config: OptionCodecConfig & { noneValue: 'zeroes'; prefix: null },\n): FixedSizeEncoder, TSize>;\nexport function getOptionEncoder(\n item: FixedSizeEncoder,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): FixedSizeEncoder>;\nexport function getOptionEncoder(\n item: FixedSizeEncoder,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): VariableSizeEncoder>;\nexport function getOptionEncoder(\n item: Encoder,\n config?: OptionCodecConfig & { noneValue?: ReadonlyUint8Array },\n): VariableSizeEncoder>;\nexport function getOptionEncoder(\n item: Encoder,\n config: OptionCodecConfig = {},\n): Encoder> {\n const prefix = (() => {\n if (config.prefix === null) {\n return transformEncoder(getUnitEncoder(), (_boolean: boolean) => undefined);\n }\n return getBooleanEncoder({ size: config.prefix ?? getU8Encoder() });\n })();\n const noneValue = (() => {\n if (config.noneValue === 'zeroes') {\n assertIsFixedSize(item);\n return fixEncoderSize(getUnitEncoder(), item.fixedSize);\n }\n if (!config.noneValue) {\n return getUnitEncoder();\n }\n return getConstantEncoder(config.noneValue);\n })();\n\n return getUnionEncoder(\n [\n transformEncoder(getTupleEncoder([prefix, noneValue]), (_value: None | null): [boolean, void] => [\n false,\n undefined,\n ]),\n transformEncoder(getTupleEncoder([prefix, item]), (value: Some | TFrom): [boolean, TFrom] => [\n true,\n isOption(value) && isSome(value) ? value.value : value,\n ]),\n ],\n variant => {\n const option = isOption(variant) ? variant : wrapNullable(variant);\n return Number(isSome(option));\n },\n );\n}\n\n/**\n * Creates a decoder for an optional value using the `Option` type.\n *\n * @param item - The decoder to use for the value that may be present.\n * @param config - A set of config for the decoder.\n */\nexport function getOptionDecoder(\n item: FixedSizeDecoder,\n config: OptionCodecConfig & { noneValue: 'zeroes'; prefix: null },\n): FixedSizeDecoder, TSize>;\nexport function getOptionDecoder(\n item: FixedSizeDecoder,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): FixedSizeDecoder>;\nexport function getOptionDecoder(\n item: FixedSizeDecoder,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): VariableSizeDecoder>;\nexport function getOptionDecoder(\n item: Decoder,\n config?: OptionCodecConfig & { noneValue?: ReadonlyUint8Array },\n): VariableSizeDecoder>;\nexport function getOptionDecoder(\n item: Decoder,\n config: OptionCodecConfig = {},\n): Decoder> {\n const prefix = (() => {\n if (config.prefix === null) {\n return transformDecoder(getUnitDecoder(), () => false);\n }\n return getBooleanDecoder({ size: config.prefix ?? getU8Decoder() });\n })();\n const noneValue = (() => {\n if (config.noneValue === 'zeroes') {\n assertIsFixedSize(item);\n return fixDecoderSize(getUnitDecoder(), item.fixedSize);\n }\n if (!config.noneValue) {\n return getUnitDecoder();\n }\n return getConstantDecoder(config.noneValue);\n })();\n\n return getUnionDecoder(\n [\n transformDecoder(getTupleDecoder([prefix, noneValue]), () => none()),\n transformDecoder(getTupleDecoder([prefix, item]), ([, value]) => some(value)),\n ],\n (bytes, offset) => {\n if (config.prefix === null && !config.noneValue) {\n return Number(offset < bytes.length);\n }\n if (config.prefix === null && config.noneValue != null) {\n const zeroValue =\n config.noneValue === 'zeroes' ? new Uint8Array(noneValue.fixedSize).fill(0) : config.noneValue;\n return containsBytes(bytes, zeroValue, offset) ? 0 : 1;\n }\n return Number(prefix.read(bytes, offset)[0]);\n },\n );\n}\n\n/**\n * Creates a codec for an optional value using the `Option` type.\n *\n * @param item - The codec to use for the value that may be present.\n * @param config - A set of config for the codec.\n */\nexport function getOptionCodec(\n item: FixedSizeCodec,\n config: OptionCodecConfig & { noneValue: 'zeroes'; prefix: null },\n): FixedSizeCodec, Option, TSize>;\nexport function getOptionCodec(\n item: FixedSizeCodec,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): FixedSizeCodec, Option>;\nexport function getOptionCodec(\n item: FixedSizeCodec,\n config: OptionCodecConfig & { noneValue: 'zeroes' },\n): VariableSizeCodec, Option>;\nexport function getOptionCodec(\n item: Codec,\n config?: OptionCodecConfig & { noneValue?: ReadonlyUint8Array },\n): VariableSizeCodec, Option>;\nexport function getOptionCodec(\n item: Codec,\n config: OptionCodecConfig = {},\n): Codec, Option> {\n type ConfigCast = OptionCodecConfig & { noneValue?: ReadonlyUint8Array };\n return combineCodec(\n getOptionEncoder(item, config as ConfigCast),\n getOptionDecoder(item, config as ConfigCast),\n );\n}\n","import { isOption, isSome, None, Some } from './option';\n\n/**\n * Lists all types that should not be recursively unwrapped.\n *\n * @see {@link UnwrappedOption}\n */\ntype UnUnwrappables =\n | Date\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array\n | bigint\n | boolean\n | number\n | string\n | symbol\n | null\n | undefined;\n\n/**\n * A type that defines the recursive unwrapping of a type `T`\n * such that all nested {@link Option} types are unwrapped.\n *\n * For each nested {@link Option} type, if the option is a {@link Some},\n * it returns the type of its value, otherwise, it returns the provided\n * fallback type `U` which defaults to `null`.\n */\nexport type UnwrappedOption =\n T extends Some\n ? UnwrappedOption\n : T extends None\n ? U\n : T extends UnUnwrappables\n ? T\n : T extends object\n ? { [key in keyof T]: UnwrappedOption }\n : T extends Array\n ? Array>\n : T;\n\n/**\n * Recursively go through a type `T` such that all\n * nested {@link Option} types are unwrapped.\n *\n * For each nested {@link Option} type, if the option is a {@link Some},\n * it returns its value, otherwise, it returns the provided fallback value\n * which defaults to `null`.\n */\nexport function unwrapOptionRecursively(input: T): UnwrappedOption;\nexport function unwrapOptionRecursively(input: T, fallback: () => U): UnwrappedOption;\nexport function unwrapOptionRecursively(input: T, fallback?: () => U): UnwrappedOption {\n // Types to bypass.\n if (!input || ArrayBuffer.isView(input)) {\n return input as UnwrappedOption;\n }\n\n const next = (x: X) =>\n (fallback ? unwrapOptionRecursively(x, fallback) : unwrapOptionRecursively(x)) as UnwrappedOption;\n\n // Handle Option.\n if (isOption(input)) {\n if (isSome(input)) return next(input.value) as UnwrappedOption;\n return (fallback ? fallback() : null) as UnwrappedOption;\n }\n\n // Walk.\n if (Array.isArray(input)) {\n return input.map(next) as UnwrappedOption;\n }\n if (typeof input === 'object') {\n return Object.fromEntries(Object.entries(input).map(([k, v]) => [k, next(v)])) as UnwrappedOption;\n }\n return input as UnwrappedOption;\n}\n"]}