UNPKG

@metaplex-foundation/umi-serializers

Version:

A comprehensive set of serializers for the Umi framework

1 lines 4.77 kB
{"version":3,"file":"string.cjs","sources":["../../src/string.ts"],"sourcesContent":["import {\n BaseSerializerOptions,\n DeserializingEmptyBufferError,\n NotEnoughBytesError,\n Serializer,\n fixSerializer,\n mergeBytes,\n} from '@metaplex-foundation/umi-serializers-core';\nimport { utf8 } from '@metaplex-foundation/umi-serializers-encodings';\nimport {\n NumberSerializer,\n u32,\n} from '@metaplex-foundation/umi-serializers-numbers';\nimport { getSizeDescription } from './utils';\n\n/**\n * Defines the options for string serializers.\n * @category Serializers\n */\nexport type StringSerializerOptions = BaseSerializerOptions & {\n /**\n * The size of the string. It can be one of the following:\n * - a {@link NumberSerializer} that prefixes the string with its size.\n * - a fixed number of bytes.\n * - or `'variable'` to use the rest of the buffer.\n * @defaultValue `u32()`\n */\n size?: NumberSerializer | number | 'variable';\n /**\n * The string serializer to use for encoding and decoding the content.\n * @defaultValue `utf8`\n */\n encoding?: Serializer<string>;\n};\n\n/**\n * Creates a string serializer.\n *\n * @param options - A set of options for the serializer.\n * @category Serializers\n */\nexport function string(\n options: StringSerializerOptions = {}\n): Serializer<string> {\n const size = options.size ?? u32();\n const encoding = options.encoding ?? utf8;\n const description =\n options.description ??\n `string(${encoding.description}; ${getSizeDescription(size)})`;\n\n if (size === 'variable') {\n return { ...encoding, description };\n }\n\n if (typeof size === 'number') {\n return fixSerializer(encoding, size, description);\n }\n\n return {\n description,\n fixedSize: null,\n maxSize: null,\n serialize: (value: string) => {\n const contentBytes = encoding.serialize(value);\n const lengthBytes = size.serialize(contentBytes.length);\n return mergeBytes([lengthBytes, contentBytes]);\n },\n deserialize: (buffer: Uint8Array, offset = 0) => {\n if (buffer.slice(offset).length === 0) {\n throw new DeserializingEmptyBufferError('string');\n }\n const [lengthBigInt, lengthOffset] = size.deserialize(buffer, offset);\n const length = Number(lengthBigInt);\n offset = lengthOffset;\n const contentBuffer = buffer.slice(offset, offset + length);\n if (contentBuffer.length < length) {\n throw new NotEnoughBytesError('string', length, contentBuffer.length);\n }\n const [value, contentOffset] = encoding.deserialize(contentBuffer);\n offset += contentOffset;\n return [value, offset];\n },\n };\n}\n"],"names":["string","options","size","u32","encoding","utf8","description","getSizeDescription","fixSerializer","fixedSize","maxSize","serialize","value","contentBytes","lengthBytes","length","mergeBytes","deserialize","buffer","offset","slice","DeserializingEmptyBufferError","lengthBigInt","lengthOffset","Number","contentBuffer","NotEnoughBytesError","contentOffset"],"mappings":";;;;;;;;;AAeA;AACA;AACA;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,MAAM,CACpBC,OAAgC,GAAG,EAAE,EACjB;AACpB,EAAA,MAAMC,IAAI,GAAGD,OAAO,CAACC,IAAI,IAAIC,yBAAG,EAAE,CAAA;AAClC,EAAA,MAAMC,QAAQ,GAAGH,OAAO,CAACG,QAAQ,IAAIC,4BAAI,CAAA;AACzC,EAAA,MAAMC,WAAW,GACfL,OAAO,CAACK,WAAW,IAClB,CAAA,OAAA,EAASF,QAAQ,CAACE,WAAY,CAAIC,EAAAA,EAAAA,wBAAkB,CAACL,IAAI,CAAE,CAAE,CAAA,CAAA,CAAA;EAEhE,IAAIA,IAAI,KAAK,UAAU,EAAE;IACvB,OAAO;AAAE,MAAA,GAAGE,QAAQ;AAAEE,MAAAA,WAAAA;KAAa,CAAA;AACrC,GAAA;AAEA,EAAA,IAAI,OAAOJ,IAAI,KAAK,QAAQ,EAAE;AAC5B,IAAA,OAAOM,gCAAa,CAACJ,QAAQ,EAAEF,IAAI,EAAEI,WAAW,CAAC,CAAA;AACnD,GAAA;EAEA,OAAO;IACLA,WAAW;AACXG,IAAAA,SAAS,EAAE,IAAI;AACfC,IAAAA,OAAO,EAAE,IAAI;IACbC,SAAS,EAAGC,KAAa,IAAK;AAC5B,MAAA,MAAMC,YAAY,GAAGT,QAAQ,CAACO,SAAS,CAACC,KAAK,CAAC,CAAA;MAC9C,MAAME,WAAW,GAAGZ,I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