{"_id":"@araviel/mtf-beet","name":"@araviel/mtf-beet","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@araviel/mtf-beet","version":"0.1.0","description":"Strict borsh compatible de/serializer","sideEffects":false,"main":"dist/cjs/src/beet.js","module":"dist/esm/src/beet.js","types":"dist/types/src/beet.d.ts","exports":{"import":"./dist/esm/src/beet.js","require":"./dist/cjs/src/beet.js"},"scripts":{"check:publish-ready":"yarn build && yarn test","preversion":"yarn check:publish-ready","postversion":"git add package.json && git commit -m \"chore: update $npm_package_name to v$npm_package_version\" && git tag $npm_package_name@$npm_package_version","prepublishOnly":"yarn check:publish-ready","postpublish":"git push origin && git push origin --tags","build":"rimraf dist && tsc -p tsconfig.json && tsc -p tsconfig.esm.json","pretest":"yarn build","test":"tape dist/cjs/test/{unit,compat}/*.js","test:browser":"airtap ./test/{unit,compat}/*.ts","bsamples":"cd ../tools/bsamples && cargo run","lint":"prettier -c ./src/","lint:fix":"prettier --write ./src","doc":"rimraf ../docs/beet && typedoc","doctoc":"doctoc README.md"},"repository":{"type":"git","url":"git+ssh://git@github.com/metaplex-foundation/beet.git"},"author":{"name":"Thorsten Lorenz","email":"thlorenz@gmx.de"},"license":"Apache-2.0","directories":{"dist":"./dist/src"},"dependencies":{"ansicolors":"^0.3.2","bn.js":"^5.2.0","debug":"^4.3.3"},"devDependencies":{"@types/ansicolors":"^0.0.33","@types/bn.js":"^5.1.0","@types/debug":"^4.1.7","@types/node":"^14.18.0","@types/tape":"^4.13.2","airtap":"^4.0.4","airtap-system":"^0.1.0","browserify":"^17.0.0","doctoc":"^2.1.0","prettier":"^2.5.1","rimraf":"^3.0.2","spok":"^1.4.3","supports-color":"^8.1.1","tape":"^5.3.2","tsify":"^5.0.4","typedoc":"^0.22.10","typescript":"^4.5.2"},"gitHead":"65f885188dbbca7473065be39bdcad64787b0ad2","bugs":{"url":"https://github.com/metaplex-foundation/beet/issues"},"homepage":"https://github.com/metaplex-foundation/beet#readme","_id":"@araviel/mtf-beet@0.1.0","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-f31Kd+V6shs1Rohe9ALn1ru8Y8mbbMIq9O/WcAoDIxa3Q9PmL5t1lVb7auS3IEuCOWSBEN/DLuCce7V7prPmHw==","shasum":"4ded19c28b056d5038ebc6cf7ee3771319dc4b17","tarball":"https://registry.npmjs.org/@araviel/mtf-beet/-/mtf-beet-0.1.0.tgz","fileCount":63,"unpackedSize":195820,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDYY9VZOQD7hlfXIjV8Zono2qpLEZxHD3CfV7mLVX/nHAiEAhH8K3gXOehZ/+sICG2YPAKYXt6vvlMl79S5vzA3CEY0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJicYhIACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqFJxAAg283mN7+YXKpUDnXxhpG4l9KQWYwNqFs6noQrM5h+fh7nFpB\r\n0JagBOE2ZzvugBPbmRkg4o6hYtjVp/TdxoFCj+JHhjAyT7R0FKXFGhpFAvii\r\nTzu9l2W3vwpaA4UeLjZ4lrx0OK6MfUat+OdIyhiong0D8YP2oVjhydOJbXhJ\r\nn6b52cW0mlQc8caKbO2vRYiN+htMSEH3TWV5Kc6TDPS0moRhECQF/VMjMRH0\r\nMyNNZF4pGFeJak2R4NvxUUC1clOwC+e+pjj+HPtA2aBXUrhpDFWctbLTYjjR\r\nfQokdBZj0EKn4y5StojFH8NG8OawLrszN6QGP1DbqoEqX92qPy4LmaXrQqpU\r\nPDm16PlKq+APzV93l1Dg85m7CDDMK2hExBwrfh+p6qReTR5MzXkHIfga8go8\r\nfy1RHTlEO/TrcnkuLkfUPzYOtIq/Mt16Kpq42ycxyL4L6yWm6x6CeW5iSv5/\r\n9oNrrVo/d8N7fy1QxDL07Iyw+sTfriTpzd7COPevfA78w58uEO8H3JK+Oepv\r\n/c6v8fGumQltRg3v46pe76OksmZJH4qW+Uo0HpWZBfr6DyxslYK4Ny2VXyOE\r\n8TOA+xX9onhJ8K0hP8xgoerXiA8hm+fDT8gX/vM8mEQpACUs3J2hZAPih9HC\r\nrL03ND6TqRfkxQQS5qC7KFPIrhgiaiJFqPE=\r\n=dmKd\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"araviel","email":"contact@araviel.io"},"maintainers":[{"name":"araviel","email":"contact@araviel.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mtf-beet_0.1.0_1651607624478_0.7363222363234021"},"_hasShrinkwrap":false}},"time":{"created":"2022-05-03T19:53:44.412Z","0.1.0":"2022-05-03T19:53:44.624Z","modified":"2022-05-03T19:53:44.783Z"},"maintainers":[{"name":"araviel","email":"contact@araviel.io"}],"description":"Strict borsh compatible de/serializer","homepage":"https://github.com/metaplex-foundation/beet#readme","repository":{"type":"git","url":"git+ssh://git@github.com/metaplex-foundation/beet.git"},"author":{"name":"Thorsten Lorenz","email":"thlorenz@gmx.de"},"bugs":{"url":"https://github.com/metaplex-foundation/beet/issues"},"license":"Apache-2.0","readme":"# @metaplex-foundation/beet\n\nStrict borsh compatible de/serializer.\n\n<!-- START doctoc generated TOC please keep comment here to allow auto update -->\n<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n- [Features](#features)\n- [Fixed Size vs. Dynamic Types](#fixed-size-vs-dynamic-types)\n- [API](#api)\n- [Examples](#examples)\n  - [Single Fixed Struct Configuration](#single-fixed-struct-configuration)\n  - [Single Fixable Struct Configuration](#single-fixable-struct-configuration)\n  - [Nested Struct Configuration](#nested-struct-configuration)\n  - [Struct with non-primitive fields](#struct-with-non-primitive-fields)\n  - [Using Beet Primitives Directly](#using-beet-primitives-directly)\n    - [Fixed Size](#fixed-size)\n    - [Dynamic Size](#dynamic-size)\n  - [Using Beet Composites Directly](#using-beet-composites-directly)\n    - [Option](#option)\n    - [Array](#array)\n      - [Uniform Fixed Size](#uniform-fixed-size)\n      - [Dynamic Size](#dynamic-size-1)\n    - [Enum with Data Variants](#enum-with-data-variants)\n- [LICENSE](#license)\n\n<!-- END doctoc generated TOC please keep comment here to allow auto update -->\n\n\n## Features\n\n- fully composable, i.e. `coption(array(utf8String))` is handled\ncorrectly\n- structs can be nested and composed\n- pre-computes `byteSize` of any fixed size type, no matter how deeply nested or composed it is\n- converts non-fixed types to their fixed versions simply by providing a value or serialized\n  data\n- fixed size and _fixable_ structs expose identical serialize/deserialize API and perform\n  conversions under the hood when needed\n- logs struct configs including byte sizes as well as de/serialization tasks for easy\ndiagnostics\n\n```\nbeet:debug struct GameStruct {\nbeet:debug   win: u8 1 B\nbeet:debug   totalWin: u16 2 B\nbeet:debug   whaleAccount: u128 16 B\nbeet:debug   losses: i32 4 B\nbeet:debug } 23 B +0ms\nbeet:trace serializing [GameStruct] _GameScore { win: 1, totalWin: 100, whaleAccount: <BN: fffffffffffffffffffffffffffffffb>, losses: -234 } to 23 bytes buffer +0ms\nbeet:trace deserializing [GameStruct] from 23 bytes buffer +2ms\nbeet:trace <Buffer 01 64 00 fb ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 16 ff ff ff> +0ms\nbeet:trace [\nbeet:trace     1, 100,   0, 251, 255, 255,\nbeet:trace   255, 255, 255, 255, 255, 255,\nbeet:trace   255, 255, 255, 255, 255, 255,\nbeet:trace   255,  22, 255, 255, 255\nbeet:trace ] +0ms\n```\n\n## Fixed Size vs. Dynamic Types \n\nBeet is optimized for _fixed_ types as this allows logging detailed diagnostics about the\nstructure of data that it is processing as well as avoiding Buffer resizes.\n\nOnly _beets_ that have _fixed_ in the name are of fixed size, all others are _fixable_ types\nwhich expose `toFixedFromData` and `toFixedFromValue` methods to convert to a _fixed beet_ from\nserialized data or a value respectively.\n\nBeet provides the `FixableBeetStruct` to de/serialize args that have non-fixed size fields. \n\nThus beet implements the entire [borsh spec](https://borsh.io/), however if you want a library\nthat processes dynamic types directly use one of the alternatives, i.e. [borsh-js](https://github.com/near/borsh-js).\n\n## API\n\nPlease find the [API docs here](https://metaplex-foundation.github.io/beet/docs/beet).\n\n## Examples\n\n### Single Fixed Struct Configuration\n\n```ts\nimport { BeetStruct, i32, u16, u8 } from '@metaplex-foundation/beet'\n\nclass Result {\n  constructor(\n    readonly win: number,\n    readonly totalWin: number,\n    readonly losses: number\n  ) {}\n\n  static readonly struct = new BeetStruct<Result>(\n    [\n      ['win', u8],\n      ['totalWin', u16],\n      ['losses', i32],\n    ],\n    (args) => new Result(args.win!, args.totalWin!, args.losses!),\n    'Results'\n  )\n}\n```\n\n### Single Fixable Struct Configuration\n\n```ts\nimport { FixableBeetStruct, i32, u16, u8, array } from '@metaplex-foundation/beet'\n\nclass Result {\n  constructor(\n    readonly win: number,\n    readonly totalWin: number,\n    readonly losses: number[]\n  ) {}\n\n  static readonly struct = new FixableBeetStruct<Result>(\n    [\n      ['win', u8],\n      ['totalWin', u16],\n      ['losses', array(i32)],\n    ],\n    (args) => new Result(args.win!, args.totalWin!, args.losses!),\n    'Results'\n  )\n}\n```\n\n### Nested Struct Configuration\n\n**NOTE:** uses `Result` struct from the above example for the `results` field of `Trader`\n\n```ts\nimport { BeetStruct, fixedSizeUtf8String } from '@metaplex-foundation/beet'\nclass Trader {\n  constructor(\n    readonly name: string,\n    readonly results: Results,\n    readonly age: number\n  ) {}\n\n  static readonly struct = new BeetStruct<Trader>(\n    [\n      ['name', fixedSizeUtf8String(4)],\n      ['results', Results.struct],\n      ['age', u8],\n    ],\n    (args) => new Trader(args.name!, args.results!, args.age!),\n    'Trader'\n  )\n}\n  \nconst trader = new Trader('bob ', new Results(20, 1200, -455), 22)\nconst [buf] = Trader.struct.serialize(trader)\nconst [deserializedTrader] = Trader.struct.deserialize(buf)\n```\n\n### Struct with non-primitive fields\n\n**NOTE:** depends on `beet-solana` extension package for the `PublicKey` implementation\n\n```ts\nimport * as web3 from '@solana/web3.js'\nimport * as beet from '@metaplex-foundation/beet'\nimport * as beetSolana from '@metaplex-foundation/beet-solana'\n\ntype InstructionArgs = {\n  instructionDiscriminator: number[]\n  authority: web3.PublicKey\n  maybePublickKey: beet.COption<web3.PublicKey>\n}\n\n// Uses the BeetArgsStruct wrapper around BeetStruct\nconst createStruct = new beet.BeetArgsStruct<InstructionArgs>(\n  [\n    ['instructionDiscriminator', beet.fixedSizeArray(beet.u8, 8)],\n    ['authority', beetSolana.publicKey],\n    ['maybePublickKey', beet.coption(beetSolana.publicKey)],\n  ],\n  'InstructionArgs'\n)\n```\n\n### Using Beet Primitives Directly\n\n#### Fixed Size\n\n```ts\nimport { u8 } from '@metaplex-foundation/beet'\nconst n = 1\nconst buf = Buffer.alloc(u8.byteSize)\nu8.write(buf, 0, n)\nu8.read(buf, 0) // === 1\n```\n\n#### Dynamic Size\n\n```ts\nimport { u8, array } from '@metaplex-foundation/beet'\nconst xs = [ 1, 2 ]\nconst beet = array(u8)\nconst fixedBeet = beet.toFixedFromValue(xs)\nconst buf = Buffer.alloc(fixedBeet.byteSize)\nfixedBeet.write(buf, 0, xs)\nfixedBeet.read(buf, 0) // === [ 1, 2 ]\n```\n\n### Using Beet Composites Directly\n\n**NOTE:** use `Result` struct from the above example to wrap in a _Composite_ type \n\n#### Option\n\n**NOTE:** that the `coption` is a _fixable_ beet since it has a different size for the _Some_ vs.\nthe _None_ case.\n\n```ts\nconst resultOption: Beet<COption<Result>> = coption(Result.struct)\n\nconst result = new Result(20, 1200, -455)\nconst fixedBeet = resultOption.toFixedFromValue(result)\n\nconst buf = Buffer.alloc(fixedBeet.byteSize)\nfixedBeet.write(buf, 0, result)\nbeet.read(buf, 0) // same  as result\n```\n\n#### Array\n\n##### Uniform Fixed Size\n\n```ts\nconst resultArray: Beet<Array<Result>> = uniformFixedSizeArray(Result.struct, 3)\nconst results =[ new Result(20, 1200, -455), new Result(3, 999, 0), new Result(30, 100, -3) ]\nconst buf = Buffer.alloc(resultArray.byteSize)\nbeet.write(buf, 0, results)\nbeet.read(buf, 0) // same  as results\n```\n\n##### Dynamic Size\n\n```ts\nconst resultArray: Beet<Array<Result>> = array(Result.struct)\nconst results =[ new Result(20, 1200, -455), new Result(3, 999, 0), new Result(30, 100, -3) ]\nconst fixedBeet = resultsArray.toFixedFromValue(results)\n\nconst buf = Buffer.alloc(fixedBeet.byteSize)\nfixedBeet.write(buf, 0, results)\nfixedBeet.read(buf, 0) // same  as results\n```\n\n#### Enum with Data Variants\n\n```ts\nenum ResultKind {\n  Good,\n  Bad,\n}\n\nconst goodResult: DataEnum<ResultKind, Result> = {\n  kind: ResultKind.Good,\n  data: new Result(20, 1200, -455),\n}\nconst resultEnum: Beet<DataEnum<ResultKind, Result>> = dataEnum(Result.struct)\n\nconst buf = Buffer.alloc(resultEnum.byteSize)\nbeet.write(buf, 0, goodResult)\nbeet.read(buf, 0) // same as goodResult\n```\n\n## LICENSE\n\nApache-2.0\n","readmeFilename":"README.md"}