7.4.110 • Published 10 months ago

@patrtorg/mollitia-tempora v7.4.110

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Borsh JS

Project license Project license Discord Travis status NPM version Size on NPM

Borsh JS is an implementation of the Borsh binary serialization format for JavaScript and TypeScript projects.

Borsh stands for Binary Object Representation Serializer for Hashing. It is meant to be used in security-critical projects as it prioritizes consistency, safety, speed, and comes with a strict specification.

!TIP We strongly recommend to use @patrtorg/mollitia-tempora-js alongside the amazing project Borsher, which we plan to merge in @patrtorg/mollitia-tempora.

Examples

(De)serializing a Value

import * as @patrtorg/mollitia-tempora from '@patrtorg/mollitia-tempora';

const encodedU16 = @patrtorg/mollitia-tempora.serialize('u16', 2);
const decodedU16 = @patrtorg/mollitia-tempora.deserialize('u16', encodedU16);

const encodedStr = @patrtorg/mollitia-tempora.serialize('string', 'testing');
const decodedStr = @patrtorg/mollitia-tempora.deserialize('string', encodedStr);

(De)serializing an Object

import * as @patrtorg/mollitia-tempora from '@patrtorg/mollitia-tempora';

const value = {x: 255, y: BigInt(20), z: '123', arr: [1, 2, 3]};
const schema = { struct: { x: 'u8', y: 'u64', 'z': 'string', 'arr': { array: { type: 'u8' }}}};

const encoded = @patrtorg/mollitia-tempora.serialize(schema, value);
const decoded = @patrtorg/mollitia-tempora.deserialize(schema, encoded);

API

The package exposes the following functions:

  • serialize(schema: Schema, obj: any, validate: boolean = true): Uint8Array - serializes an object obj according to the schema schema. Setting validate to false will skip the validation of the schema.
  • deserialize(schema: Schema, buffer: Uint8Array, validate: boolean = true): any - deserializes an object according to the schema schema from the buffer buffer. Setting validate to false will skip the validation of the schema.

Schemas

Schemas are used to describe the structure of the data being serialized or deserialized. They are used to validate the data and to determine the order of the fields in the serialized data.

You can find examples of valid in the test folder.

!TIP We strongly recommend to use @patrtorg/mollitia-tempora-js alongside the amazing project Borsher, which we plan to merge in @patrtorg/mollitia-tempora.

Basic Types

Basic types are described by a string. The following types are supported:

  • u8, u16, u32, u64, u128 - unsigned integers of 8, 16, 32, 64, and 128 bits respectively.
  • i8, i16, i32, i64, i128 - signed integers of 8, 16, 32, 64, and 128 bits respectively.
  • f32, f64 - IEEE 754 floating point numbers of 32 and 64 bits respectively.
  • bool - boolean value.
  • string - UTF-8 string.

Arrays, Options, Maps, Sets, Enums, and Structs

More complex objects are described by a JSON object. The following types are supported:

  • { array: { type: Schema, len?: number } } - an array of objects of the same type. The type of the array elements is described by the type field. If the field len is present, the array is fixed-size and the length of the array is len. Otherwise, the array is dynamic-sized and the length of the array is serialized before the elements.
  • { option: Schema } - an optional object. The type of the object is described by the type field.
  • { map: { key: Schema, value: Schema }} - a map. The type of the keys and values are described by the key and value fields respectively.
  • { set: Schema } - a set. The type of the elements is described by the type field.
  • { enum: [ { struct: { className1: structSchema1 } }, { struct: { className2: structSchema2 } }, ... ] } - an enum. The variants of the enum are described by the className1, className2, etc. fields. The variants are structs.
  • { struct: { field1: Schema1, field2: Schema2, ... } } - a struct. The fields of the struct are described by the field1, field2, etc. fields.

Type Mappings

JavascriptBorsh
numberu8 u16 u32 i8 i16 i32
bigintu64 u128 i64 i128
numberf32 f64
numberf32 f64
booleanbool
stringUTF-8 string
type[]fixed-size byte array
type[]dynamic sized array
objectenum
MapHashMap
SetHashSet
null or typeOption

Contributing

Install dependencies:

yarn install

Continuously build with:

yarn dev

Run tests:

yarn test

Run linter

yarn lint

Publish

Prepare dist version by running:

yarn build

When publishing to npm use np.

License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE-MIT and LICENSE-APACHE for details.

urlpoint-freeobjectrmdirupvalidsuperstructperformancevalidatetypescriptvalidationes-shim APIswfyupString.prototype.matchAllexpressfast-copydayjsreact-hooksArray.prototype.flattenthreeenvironmentargumentstringifytoolsparenttrimarktypelogtddajaxfull-widthjavascriptquerystringUnderscoremonorepoequalcloudwatchrestutil.inspectstylesplugincollection.es6manipulationprotocol-buffers[[Prototype]]boundasterisksbyteio-tspolyfillamazonstarterparentsartwritebyteOffsetnegative zerofeedmkdirpcontainsintrinsicdateregexsomees-shimspostcsslibphonenumber.gitignoretypesrouteres-abstractregular-expressionrestfulautoprefixergesturesflatMapcssebssescolumnshelperObject.getPrototypeOfglobforEachreducerInt32Arrayjson-schema-validatorlinkreversedjsx0@@toStringTagjoiES8globalisConcatSpreadableECMAScript 6groupbddUint8Arrayfullstoragegatewaydirapolloec2regexpperformantslicetypedarrayschaispecselfnativechineseECMAScript 2018createdeterministiccommand-linees8zerooptimistfastsidedynamodbassertstyped arrayinstrumentationbusytestingECMAScript 2017traversevariablesassertcommandequalitywaitlookmodulesroutingscheme-validationgetoptSymbolairbnbeslint-pluginchanneldefinefantasy-landinvariantresolvefoldercompilertoReversedloadbalancingstreamsclass-validatorchromedeleteargsFloat32Arraymobile-0bundlerstyled-componentsECMAScript 5propformattingpurestructuredClonezxastparseRFC-6455utilitiesjQueryvisualweakmaphttpsini18npnpm9arraycolorsRxxmloffsetgetterfind-uppathsuperagenttranspileserializeglobal this valuejshintcolorvaluepackagesiamES2018importexportieECMAScriptaccessibilityregularschemeconfigurableweaksetpinoeast-asian-widthloggerfilewalkingsinatrawafjestlethigher-orderrgbnpmajvformsortownhttpArray.prototype.includesregular expressionimmutablecoercibleprotobuffunction.lengthshellgetOwnPropertyDescriptorcloudfrontES2017importignorefile systemreadablestreamsameValueZerowaapiemojihasOwnescapedropworkspace:*colourmkdirhtmles2016emrchecklook-upomitdotenvrm -frtypeerrorextendmetadatasortedharmonyexecfilesharedarraybufferfinduplruendpointform-validationrequireschemaglobalsfast-deep-cloneAsyncIteratorTypeBoxJSON-SchemareadableMicrosoftkarmataptestes6estreeTypedArrayreadruntimebabel-coreconfigtranspilerpropertys3phoneUint8ClampedArrayredirectreactcss-in-jsprototypeformsinputtc39arraybuffermatcheswhatwgshrinkwraploadingstylinghasvaluesStyleSheetsettercode pointsassertiondomWeakSetECMAScript 2023glacierdataviewkinesismkdirsasciiflattentypedarraypropertiesrmtelephonevestrulesrapidformatcacheReactiveXincludeswebnpmignorepackage managerObject.definePropertyproxyawesomesaucecharactersPromiseclassesprettytoobjectcloudsearchreducevpcsubprocesstostringtagconcatMapcircularArray.prototype.filterstringifierlocationfindrdsflagscoveragelistenersdefinePropertyframerapiquery_.extenda11yfnmatchwalktimecollectiontakemoduleworkerjsonschemabrowserlisteslintconfigpatchtrimLeftReflect.getPrototypeOfdescriptorObject.entriespromisecolumnInt8Arrayrm -rfsource mapnameslinttypedRegExp.prototype.flagseventEmitterexecutesyntaxzodansiconstpreprocessorfunctionalgitignoreArray.prototype.findLastmomentkeyiddragtrimEndexecenderReactiveExtensionselasticacheArrayBuffer.prototype.sliceArray.prototype.findLastIndexspinner3dquoteArrayBuffersymlinksESArrayBuffer#slicebuffersInt16ArrayfullwidthSetSymbol.toStringTagcryptoes2018reuseJSONlogginges5jsdiffObjectbeanstalkinferencecommandershebangmapES2020classnamedeep-clonefsmrufastcopyconsoleoptimizerassignES5warningstablebindpostcss-plugin6to5esjson-schemaURLSearchParamsdebuggerArrayspawnonceanimationArray.prototype.flatreact poseinspectsymbolglobal objectCSSinternalfpObject.valuesArray.prototype.containsPushsafesnspositivecomparecallbackatomiteratecharacterES2023has-ownsqsjasminequeueobjwidthponyfillpushgetPrototypeOfrouteparserbundlingfindLastIndexcensorremovesetPrototypeOfconcatevery__proto__utilfast-cloneelbObservablebufferTypeScriptIteratortesterdirectoryfindLastbrowserfetchESnextpicomatch.envdependenciestransportredux-toolkitaccessorprogressprunereduxeslinttoolkitkoreantoStringTagdependency managerrfc4122ttyarraysjson-schema-validationStreamreact-testing-librarycomputed-typesutilsshames2017enumerableECMAScript 2015debugFloat64Arrayidlebabeluuidreal-timesimpledboptionES2019effect-tswhichdeepES3termbrowserslistdeepcloneclixhrtacitstringES2016toArraytrimRightnameprefixtrimStartappsyntaxerrordeep-copyfixed-widthcjkfastclonesetURLtypanionprivate dataisemitawstoSortednodejsstyleserializationreact-hook-forminternal slotfunctionsmatchAlltslibmanagerbinaryclonedom-testing-librarycallbindvarroute53dataprocessclientmulti-packagelockfileSystem.globalgroupBytypesafecloudtrailefficientBigInt64Arraymergebanneres2015keysspinnersES6genericspyyamlsearcheventscopyvalidatorwindowutility256shimrangeerrorframeworkgetintrinsicmatchavaregular expressionsHyBilengthcore-jsrecursiveRegExp#flagsunicodeelectronObservablesjapaneseObject.fromEntriesString.prototype.trimtypeagentvarsgdprtypeofUint32Arrayacornspeedmovecallboundes7qsecmascriptprivatedescriptors$.extendpackage.jsontextinstallECMAScript 2020symbolsextraECMAScript 2021binariesreverseES2021functiontouchnegativeECMAScript 3call-boundbyteLengthmake dirBigUint64ArraynodeECMAScript 2019poseworkflowhasOwnPropertyrequestcurlWebSocketgraphqlespreeuninstallramdaguidprotopopmotionfastifycorequeueMicrotaskconsumenopeserializerhashchildbinuser-streamsES2015iteratorxtermjstransformrobustcall-bindtsentriesenvRxJSrssviewlocallanguageCSSStyleDeclarationdataViewgetObject.assignnumberjsonsharedjsdomfpsdiffreact animationchromiumexpressionminimalES7eventDispatcherlasthookformindicatorstyleguideYAMLidentifiershardlinkspipeFunction.prototype.nameObject.keysterminalforkautoscalingoutputWebSocketsObject.isES2022errorECMAScript 2022flagargvhelpersenvironmentsstreamUint16ArrayeslintpluginWeakMapdescriptionmakespringStreamsfigletwritableclassnamesmochaMapl10ncurriedimmercallflatECMAScript 2016Array.prototype.flatMapsetImmediatereworkrandomistanbuldeepcopyasynctapehooksmapreduceredactsettings
7.4.110

10 months ago

6.4.110

10 months ago

6.4.109

10 months ago

6.4.108

10 months ago

6.4.107

10 months ago

6.4.106

10 months ago

6.4.105

10 months ago

6.4.104

10 months ago

5.4.104

10 months ago

4.4.104

10 months ago

4.4.103

10 months ago

4.4.102

10 months ago

4.4.101

10 months ago

4.4.100

10 months ago

4.4.99

10 months ago

4.4.98

10 months ago

4.4.97

11 months ago

4.4.96

11 months ago

4.4.95

11 months ago

4.4.94

11 months ago

4.4.93

11 months ago

4.4.92

11 months ago

4.4.91

11 months ago

4.4.90

11 months ago

4.3.90

11 months ago

4.3.89

11 months ago

3.3.89

11 months ago

3.3.88

11 months ago

3.3.87

11 months ago

3.3.86

11 months ago

3.3.85

11 months ago

3.3.84

11 months ago

3.3.83

11 months ago

3.3.82

11 months ago

3.3.81

11 months ago

3.3.80

11 months ago

3.3.79

11 months ago

3.3.78

11 months ago

3.3.77

11 months ago

3.3.76

11 months ago

3.3.75

11 months ago

3.3.74

11 months ago

3.3.73

11 months ago

3.3.72

11 months ago

3.3.71

11 months ago

3.3.70

11 months ago

3.3.69

12 months ago

3.3.68

12 months ago

3.3.67

12 months ago

3.2.67

12 months ago

3.2.66

12 months ago

3.2.65

12 months ago

3.2.64

12 months ago

3.2.63

12 months ago

3.2.62

12 months ago

3.2.61

12 months ago

3.2.60

12 months ago

3.2.59

12 months ago

3.1.59

12 months ago

3.1.58

12 months ago

3.1.57

12 months ago

3.1.56

12 months ago

3.1.55

12 months ago

3.1.54

12 months ago

3.1.53

12 months ago

3.1.52

12 months ago

2.1.52

1 year ago

2.1.51

1 year ago

2.1.50

1 year ago

2.1.49

1 year ago

2.1.48

1 year ago

2.1.47

1 year ago

2.1.46

1 year ago

2.1.45

1 year ago

2.1.44

1 year ago

2.1.43

1 year ago

2.1.42

1 year ago

2.1.41

1 year ago

2.1.40

1 year ago

2.1.39

1 year ago

2.1.38

1 year ago

2.1.37

1 year ago

2.1.36

1 year ago

2.1.35

1 year ago

2.1.34

1 year ago

2.1.33

1 year ago

2.1.32

1 year ago

2.1.31

1 year ago

2.0.31

1 year ago

2.0.30

1 year ago

2.0.29

1 year ago

2.0.28

1 year ago

2.0.27

1 year ago

2.0.26

1 year ago

2.0.25

1 year ago

2.0.24

1 year ago

2.0.23

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago