@metaplex-foundation/umi-serializers
Version:
A comprehensive set of serializers for the Umi framework
1 lines • 6.15 kB
Source Map (JSON)
{"version":3,"file":"nullable.cjs","sources":["../../src/nullable.ts"],"sourcesContent":["import { Nullable } from '@metaplex-foundation/umi-options';\nimport {\n BaseSerializerOptions,\n ExpectedFixedSizeSerializerError,\n Serializer,\n mergeBytes,\n} from '@metaplex-foundation/umi-serializers-core';\nimport {\n NumberSerializer,\n u8,\n} from '@metaplex-foundation/umi-serializers-numbers';\nimport { sumSerializerSizes } from './sumSerializerSizes';\nimport { getSizeDescription } from './utils';\n\n/**\n * Defines the options for `Nullable` serializers.\n * @category Serializers\n */\nexport type NullableSerializerOptions = BaseSerializerOptions & {\n /**\n * The serializer to use for the boolean prefix.\n * @defaultValue `u8()`\n */\n prefix?: NumberSerializer;\n /**\n * Whether the item serializer should be of fixed size.\n *\n * When this is true, a `null` value will skip the bytes that would\n * have been used for the item. Note that this will only work if the\n * item serializer is of fixed size.\n * @defaultValue `false`\n */\n fixed?: boolean;\n};\n\n/**\n * Creates a serializer for an optional value using `null` as the `None` value.\n *\n * @param item - The serializer to use for the value that may be present.\n * @param options - A set of options for the serializer.\n * @category Serializers\n */\nexport function nullable<T, U extends T = T>(\n item: Serializer<T, U>,\n options: NullableSerializerOptions = {}\n): Serializer<Nullable<T>, Nullable<U>> {\n const prefix = options.prefix ?? u8();\n const fixed = options.fixed ?? false;\n let descriptionSuffix = `; ${getSizeDescription(prefix)}`;\n let fixedSize = item.fixedSize === 0 ? prefix.fixedSize : null;\n if (fixed) {\n if (item.fixedSize === null || prefix.fixedSize === null) {\n throw new ExpectedFixedSizeSerializerError(\n 'Fixed nullables can only be used with fixed-size serializers'\n );\n }\n descriptionSuffix += '; fixed';\n fixedSize = prefix.fixedSize + item.fixedSize;\n }\n return {\n description:\n options.description ??\n `nullable(${item.description + descriptionSuffix})`,\n fixedSize,\n maxSize: sumSerializerSizes([prefix.maxSize, item.maxSize]),\n serialize: (option: Nullable<T>) => {\n const prefixByte = prefix.serialize(Number(option !== null));\n if (fixed) {\n const itemFixedSize = item.fixedSize as number;\n const itemBytes =\n option !== null\n ? item.serialize(option).slice(0, itemFixedSize)\n : new Uint8Array(itemFixedSize).fill(0);\n return mergeBytes([prefixByte, itemBytes]);\n }\n const itemBytes =\n option !== null ? item.serialize(option) : new Uint8Array();\n return mergeBytes([prefixByte, itemBytes]);\n },\n deserialize: (bytes: Uint8Array, offset = 0) => {\n if (bytes.slice(offset).length === 0) {\n return [null, offset];\n }\n const fixedOffset =\n offset + (prefix.fixedSize ?? 0) + (item.fixedSize ?? 0);\n const [isSome, prefixOffset] = prefix.deserialize(bytes, offset);\n offset = prefixOffset;\n if (isSome === 0) {\n return [null, fixed ? fixedOffset : offset];\n }\n const [value, newOffset] = item.deserialize(bytes, offset);\n offset = newOffset;\n return [value, fixed ? fixedOffset : offset];\n },\n };\n}\n"],"names":["nullable","item","options","prefix","u8","fixed","descriptionSuffix","getSizeDescription","fixedSize","ExpectedFixedSizeSerializerError","description","maxSize","sumSerializerSizes","serialize","option","prefixByte","Number","itemFixedSize","itemBytes","slice","Uint8Array","fill","mergeBytes","deserialize","bytes","offset","length","fixedOffset","isSome","prefixOffset","value","newOffset"],"mappings":";;;;;;;;;AAcA;AACA;AACA;AACA;;AAkBA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,QAAQ,CACtBC,IAAsB,EACtBC,OAAkC,GAAG,EAAE,EACD;AACtC,EAAA,MAAMC,MAAM,GAAGD,OAAO,CAACC,MAAM,IAAIC,wBAAE,EAAE,CAAA;AACrC,EAAA,MAAMC,KAAK,GAAGH,OAAO,CAACG,KAAK,IAAI,KAAK,CAAA;AACpC,EAAA,IAAIC,iBAAiB,GAAI,CAAA,EAAA,EAAIC,wBAAkB,CAACJ,MAAM,CAAE,CAAC,CAAA,CAAA;AACzD,EAAA,IAAIK,SAAS,GAAGP,IAAI,CAACO,SAAS,KAAK,CAAC,GAAGL,MAAM,CAACK,SAAS,GAAG,IA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