protobuf.js 219 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197
  1. /*!
  2. * protobuf.js v1.2.1 (c) 2016, daniel wirtz
  3. * compiled thu, 29 apr 2021 21:46:32 utc
  4. * licensed under the bsd-3-clause license
  5. * see: https://github.com/apollographql/protobuf.js for details
  6. */
  7. (function(undefined){"use strict";(function prelude(modules, cache, entries) {
  8. // This is the prelude used to bundle protobuf.js for the browser. Wraps up the CommonJS
  9. // sources through a conflict-free require shim and is again wrapped within an iife that
  10. // provides a minification-friendly `undefined` var plus a global "use strict" directive
  11. // so that minification can remove the directives of each module.
  12. function $require(name) {
  13. var $module = cache[name];
  14. if (!$module)
  15. modules[name][0].call($module = cache[name] = { exports: {} }, $require, $module, $module.exports);
  16. return $module.exports;
  17. }
  18. var protobuf = $require(entries[0]);
  19. // Expose globally
  20. protobuf.util.global.protobuf = protobuf;
  21. // Be nice to AMD
  22. if (typeof define === "function" && define.amd)
  23. define(["long"], function(Long) {
  24. if (Long && Long.isLong) {
  25. protobuf.util.Long = Long;
  26. protobuf.configure();
  27. }
  28. return protobuf;
  29. });
  30. // Be nice to CommonJS
  31. if (typeof module === "object" && module && module.exports)
  32. module.exports = protobuf;
  33. })/* end of prelude */({1:[function(require,module,exports){
  34. "use strict";
  35. module.exports = asPromise;
  36. /**
  37. * Callback as used by {@link util.asPromise}.
  38. * @typedef asPromiseCallback
  39. * @type {function}
  40. * @param {Error|null} error Error, if any
  41. * @param {...*} params Additional arguments
  42. * @returns {undefined}
  43. */
  44. /**
  45. * Returns a promise from a node-style callback function.
  46. * @memberof util
  47. * @param {asPromiseCallback} fn Function to call
  48. * @param {*} ctx Function context
  49. * @param {...*} params Function arguments
  50. * @returns {Promise<*>} Promisified function
  51. */
  52. function asPromise(fn, ctx/*, varargs */) {
  53. var params = new Array(arguments.length - 1),
  54. offset = 0,
  55. index = 2,
  56. pending = true;
  57. while (index < arguments.length)
  58. params[offset++] = arguments[index++];
  59. return new Promise(function executor(resolve, reject) {
  60. params[offset] = function callback(err/*, varargs */) {
  61. if (pending) {
  62. pending = false;
  63. if (err)
  64. reject(err);
  65. else {
  66. var params = new Array(arguments.length - 1),
  67. offset = 0;
  68. while (offset < params.length)
  69. params[offset++] = arguments[offset];
  70. resolve.apply(null, params);
  71. }
  72. }
  73. };
  74. try {
  75. fn.apply(ctx || null, params);
  76. } catch (err) {
  77. if (pending) {
  78. pending = false;
  79. reject(err);
  80. }
  81. }
  82. });
  83. }
  84. },{}],2:[function(require,module,exports){
  85. "use strict";
  86. /**
  87. * A minimal base64 implementation for number arrays.
  88. * @memberof util
  89. * @namespace
  90. */
  91. var base64 = exports;
  92. /**
  93. * Calculates the byte length of a base64 encoded string.
  94. * @param {string} string Base64 encoded string
  95. * @returns {number} Byte length
  96. */
  97. base64.length = function length(string) {
  98. var p = string.length;
  99. if (!p)
  100. return 0;
  101. var n = 0;
  102. while (--p % 4 > 1 && string.charAt(p) === "=")
  103. ++n;
  104. return Math.ceil(string.length * 3) / 4 - n;
  105. };
  106. // Base64 encoding table
  107. var b64 = new Array(64);
  108. // Base64 decoding table
  109. var s64 = new Array(123);
  110. // 65..90, 97..122, 48..57, 43, 47
  111. for (var i = 0; i < 64;)
  112. s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
  113. /**
  114. * Encodes a buffer to a base64 encoded string.
  115. * @param {Uint8Array} buffer Source buffer
  116. * @param {number} start Source start
  117. * @param {number} end Source end
  118. * @returns {string} Base64 encoded string
  119. */
  120. base64.encode = function encode(buffer, start, end) {
  121. var parts = null,
  122. chunk = [];
  123. var i = 0, // output index
  124. j = 0, // goto index
  125. t; // temporary
  126. while (start < end) {
  127. var b = buffer[start++];
  128. switch (j) {
  129. case 0:
  130. chunk[i++] = b64[b >> 2];
  131. t = (b & 3) << 4;
  132. j = 1;
  133. break;
  134. case 1:
  135. chunk[i++] = b64[t | b >> 4];
  136. t = (b & 15) << 2;
  137. j = 2;
  138. break;
  139. case 2:
  140. chunk[i++] = b64[t | b >> 6];
  141. chunk[i++] = b64[b & 63];
  142. j = 0;
  143. break;
  144. }
  145. if (i > 8191) {
  146. (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
  147. i = 0;
  148. }
  149. }
  150. if (j) {
  151. chunk[i++] = b64[t];
  152. chunk[i++] = 61;
  153. if (j === 1)
  154. chunk[i++] = 61;
  155. }
  156. if (parts) {
  157. if (i)
  158. parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
  159. return parts.join("");
  160. }
  161. return String.fromCharCode.apply(String, chunk.slice(0, i));
  162. };
  163. var invalidEncoding = "invalid encoding";
  164. /**
  165. * Decodes a base64 encoded string to a buffer.
  166. * @param {string} string Source string
  167. * @param {Uint8Array} buffer Destination buffer
  168. * @param {number} offset Destination offset
  169. * @returns {number} Number of bytes written
  170. * @throws {Error} If encoding is invalid
  171. */
  172. base64.decode = function decode(string, buffer, offset) {
  173. var start = offset;
  174. var j = 0, // goto index
  175. t; // temporary
  176. for (var i = 0; i < string.length;) {
  177. var c = string.charCodeAt(i++);
  178. if (c === 61 && j > 1)
  179. break;
  180. if ((c = s64[c]) === undefined)
  181. throw Error(invalidEncoding);
  182. switch (j) {
  183. case 0:
  184. t = c;
  185. j = 1;
  186. break;
  187. case 1:
  188. buffer[offset++] = t << 2 | (c & 48) >> 4;
  189. t = c;
  190. j = 2;
  191. break;
  192. case 2:
  193. buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
  194. t = c;
  195. j = 3;
  196. break;
  197. case 3:
  198. buffer[offset++] = (t & 3) << 6 | c;
  199. j = 0;
  200. break;
  201. }
  202. }
  203. if (j === 1)
  204. throw Error(invalidEncoding);
  205. return offset - start;
  206. };
  207. /**
  208. * Tests if the specified string appears to be base64 encoded.
  209. * @param {string} string String to test
  210. * @returns {boolean} `true` if probably base64 encoded, otherwise false
  211. */
  212. base64.test = function test(string) {
  213. return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(string);
  214. };
  215. },{}],3:[function(require,module,exports){
  216. "use strict";
  217. module.exports = codegen;
  218. /**
  219. * Begins generating a function.
  220. * @memberof util
  221. * @param {string[]} functionParams Function parameter names
  222. * @param {string} [functionName] Function name if not anonymous
  223. * @returns {Codegen} Appender that appends code to the function's body
  224. */
  225. function codegen(functionParams, functionName) {
  226. /* istanbul ignore if */
  227. if (typeof functionParams === "string") {
  228. functionName = functionParams;
  229. functionParams = undefined;
  230. }
  231. var body = [];
  232. /**
  233. * Appends code to the function's body or finishes generation.
  234. * @typedef Codegen
  235. * @type {function}
  236. * @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
  237. * @param {...*} [formatParams] Format parameters
  238. * @returns {Codegen|Function} Itself or the generated function if finished
  239. * @throws {Error} If format parameter counts do not match
  240. */
  241. function Codegen(formatStringOrScope) {
  242. // note that explicit array handling below makes this ~50% faster
  243. // finish the function
  244. if (typeof formatStringOrScope !== "string") {
  245. var source = toString();
  246. if (codegen.verbose)
  247. console.log("codegen: " + source); // eslint-disable-line no-console
  248. source = "return " + source;
  249. if (formatStringOrScope) {
  250. var scopeKeys = Object.keys(formatStringOrScope),
  251. scopeParams = new Array(scopeKeys.length + 1),
  252. scopeValues = new Array(scopeKeys.length),
  253. scopeOffset = 0;
  254. while (scopeOffset < scopeKeys.length) {
  255. scopeParams[scopeOffset] = scopeKeys[scopeOffset];
  256. scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
  257. }
  258. scopeParams[scopeOffset] = source;
  259. return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
  260. }
  261. return Function(source)(); // eslint-disable-line no-new-func
  262. }
  263. // otherwise append to body
  264. var formatParams = new Array(arguments.length - 1),
  265. formatOffset = 0;
  266. while (formatOffset < formatParams.length)
  267. formatParams[formatOffset] = arguments[++formatOffset];
  268. formatOffset = 0;
  269. formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
  270. var value = formatParams[formatOffset++];
  271. switch ($1) {
  272. case "d": case "f": return String(Number(value));
  273. case "i": return String(Math.floor(value));
  274. case "j": return JSON.stringify(value);
  275. case "s": return String(value);
  276. }
  277. return "%";
  278. });
  279. if (formatOffset !== formatParams.length)
  280. throw Error("parameter count mismatch");
  281. body.push(formatStringOrScope);
  282. return Codegen;
  283. }
  284. function toString(functionNameOverride) {
  285. return "function " + (functionNameOverride || functionName || "") + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
  286. }
  287. Codegen.toString = toString;
  288. return Codegen;
  289. }
  290. /**
  291. * Begins generating a function.
  292. * @memberof util
  293. * @function codegen
  294. * @param {string} [functionName] Function name if not anonymous
  295. * @returns {Codegen} Appender that appends code to the function's body
  296. * @variation 2
  297. */
  298. /**
  299. * When set to `true`, codegen will log generated code to console. Useful for debugging.
  300. * @name util.codegen.verbose
  301. * @type {boolean}
  302. */
  303. codegen.verbose = false;
  304. },{}],4:[function(require,module,exports){
  305. "use strict";
  306. module.exports = EventEmitter;
  307. /**
  308. * Constructs a new event emitter instance.
  309. * @classdesc A minimal event emitter.
  310. * @memberof util
  311. * @constructor
  312. */
  313. function EventEmitter() {
  314. /**
  315. * Registered listeners.
  316. * @type {Object.<string,*>}
  317. * @private
  318. */
  319. this._listeners = {};
  320. }
  321. /**
  322. * Registers an event listener.
  323. * @param {string} evt Event name
  324. * @param {function} fn Listener
  325. * @param {*} [ctx] Listener context
  326. * @returns {util.EventEmitter} `this`
  327. */
  328. EventEmitter.prototype.on = function on(evt, fn, ctx) {
  329. (this._listeners[evt] || (this._listeners[evt] = [])).push({
  330. fn : fn,
  331. ctx : ctx || this
  332. });
  333. return this;
  334. };
  335. /**
  336. * Removes an event listener or any matching listeners if arguments are omitted.
  337. * @param {string} [evt] Event name. Removes all listeners if omitted.
  338. * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
  339. * @returns {util.EventEmitter} `this`
  340. */
  341. EventEmitter.prototype.off = function off(evt, fn) {
  342. if (evt === undefined)
  343. this._listeners = {};
  344. else {
  345. if (fn === undefined)
  346. this._listeners[evt] = [];
  347. else {
  348. var listeners = this._listeners[evt];
  349. for (var i = 0; i < listeners.length;)
  350. if (listeners[i].fn === fn)
  351. listeners.splice(i, 1);
  352. else
  353. ++i;
  354. }
  355. }
  356. return this;
  357. };
  358. /**
  359. * Emits an event by calling its listeners with the specified arguments.
  360. * @param {string} evt Event name
  361. * @param {...*} args Arguments
  362. * @returns {util.EventEmitter} `this`
  363. */
  364. EventEmitter.prototype.emit = function emit(evt) {
  365. var listeners = this._listeners[evt];
  366. if (listeners) {
  367. var args = [],
  368. i = 1;
  369. for (; i < arguments.length;)
  370. args.push(arguments[i++]);
  371. for (i = 0; i < listeners.length;)
  372. listeners[i].fn.apply(listeners[i++].ctx, args);
  373. }
  374. return this;
  375. };
  376. },{}],5:[function(require,module,exports){
  377. "use strict";
  378. module.exports = fetch;
  379. var asPromise = require(1),
  380. inquire = require(7);
  381. var fs = inquire("fs");
  382. /**
  383. * Node-style callback as used by {@link util.fetch}.
  384. * @typedef FetchCallback
  385. * @type {function}
  386. * @param {?Error} error Error, if any, otherwise `null`
  387. * @param {string} [contents] File contents, if there hasn't been an error
  388. * @returns {undefined}
  389. */
  390. /**
  391. * Options as used by {@link util.fetch}.
  392. * @typedef FetchOptions
  393. * @type {Object}
  394. * @property {boolean} [binary=false] Whether expecting a binary response
  395. * @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
  396. */
  397. /**
  398. * Fetches the contents of a file.
  399. * @memberof util
  400. * @param {string} filename File path or url
  401. * @param {FetchOptions} options Fetch options
  402. * @param {FetchCallback} callback Callback function
  403. * @returns {undefined}
  404. */
  405. function fetch(filename, options, callback) {
  406. if (typeof options === "function") {
  407. callback = options;
  408. options = {};
  409. } else if (!options)
  410. options = {};
  411. if (!callback)
  412. return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
  413. // if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
  414. if (!options.xhr && fs && fs.readFile)
  415. return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
  416. return err && typeof XMLHttpRequest !== "undefined"
  417. ? fetch.xhr(filename, options, callback)
  418. : err
  419. ? callback(err)
  420. : callback(null, options.binary ? contents : contents.toString("utf8"));
  421. });
  422. // use the XHR version otherwise.
  423. return fetch.xhr(filename, options, callback);
  424. }
  425. /**
  426. * Fetches the contents of a file.
  427. * @name util.fetch
  428. * @function
  429. * @param {string} path File path or url
  430. * @param {FetchCallback} callback Callback function
  431. * @returns {undefined}
  432. * @variation 2
  433. */
  434. /**
  435. * Fetches the contents of a file.
  436. * @name util.fetch
  437. * @function
  438. * @param {string} path File path or url
  439. * @param {FetchOptions} [options] Fetch options
  440. * @returns {Promise<string|Uint8Array>} Promise
  441. * @variation 3
  442. */
  443. /**/
  444. fetch.xhr = function fetch_xhr(filename, options, callback) {
  445. var xhr = new XMLHttpRequest();
  446. xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
  447. if (xhr.readyState !== 4)
  448. return undefined;
  449. // local cors security errors return status 0 / empty string, too. afaik this cannot be
  450. // reliably distinguished from an actually empty file for security reasons. feel free
  451. // to send a pull request if you are aware of a solution.
  452. if (xhr.status !== 0 && xhr.status !== 200)
  453. return callback(Error("status " + xhr.status));
  454. // if binary data is expected, make sure that some sort of array is returned, even if
  455. // ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
  456. if (options.binary) {
  457. var buffer = xhr.response;
  458. if (!buffer) {
  459. buffer = [];
  460. for (var i = 0; i < xhr.responseText.length; ++i)
  461. buffer.push(xhr.responseText.charCodeAt(i) & 255);
  462. }
  463. return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
  464. }
  465. return callback(null, xhr.responseText);
  466. };
  467. if (options.binary) {
  468. // ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
  469. if ("overrideMimeType" in xhr)
  470. xhr.overrideMimeType("text/plain; charset=x-user-defined");
  471. xhr.responseType = "arraybuffer";
  472. }
  473. xhr.open("GET", filename);
  474. xhr.send();
  475. };
  476. },{"1":1,"7":7}],6:[function(require,module,exports){
  477. "use strict";
  478. module.exports = factory(factory);
  479. /**
  480. * Reads / writes floats / doubles from / to buffers.
  481. * @name util.float
  482. * @namespace
  483. */
  484. /**
  485. * Writes a 32 bit float to a buffer using little endian byte order.
  486. * @name util.float.writeFloatLE
  487. * @function
  488. * @param {number} val Value to write
  489. * @param {Uint8Array} buf Target buffer
  490. * @param {number} pos Target buffer offset
  491. * @returns {undefined}
  492. */
  493. /**
  494. * Writes a 32 bit float to a buffer using big endian byte order.
  495. * @name util.float.writeFloatBE
  496. * @function
  497. * @param {number} val Value to write
  498. * @param {Uint8Array} buf Target buffer
  499. * @param {number} pos Target buffer offset
  500. * @returns {undefined}
  501. */
  502. /**
  503. * Reads a 32 bit float from a buffer using little endian byte order.
  504. * @name util.float.readFloatLE
  505. * @function
  506. * @param {Uint8Array} buf Source buffer
  507. * @param {number} pos Source buffer offset
  508. * @returns {number} Value read
  509. */
  510. /**
  511. * Reads a 32 bit float from a buffer using big endian byte order.
  512. * @name util.float.readFloatBE
  513. * @function
  514. * @param {Uint8Array} buf Source buffer
  515. * @param {number} pos Source buffer offset
  516. * @returns {number} Value read
  517. */
  518. /**
  519. * Writes a 64 bit double to a buffer using little endian byte order.
  520. * @name util.float.writeDoubleLE
  521. * @function
  522. * @param {number} val Value to write
  523. * @param {Uint8Array} buf Target buffer
  524. * @param {number} pos Target buffer offset
  525. * @returns {undefined}
  526. */
  527. /**
  528. * Writes a 64 bit double to a buffer using big endian byte order.
  529. * @name util.float.writeDoubleBE
  530. * @function
  531. * @param {number} val Value to write
  532. * @param {Uint8Array} buf Target buffer
  533. * @param {number} pos Target buffer offset
  534. * @returns {undefined}
  535. */
  536. /**
  537. * Reads a 64 bit double from a buffer using little endian byte order.
  538. * @name util.float.readDoubleLE
  539. * @function
  540. * @param {Uint8Array} buf Source buffer
  541. * @param {number} pos Source buffer offset
  542. * @returns {number} Value read
  543. */
  544. /**
  545. * Reads a 64 bit double from a buffer using big endian byte order.
  546. * @name util.float.readDoubleBE
  547. * @function
  548. * @param {Uint8Array} buf Source buffer
  549. * @param {number} pos Source buffer offset
  550. * @returns {number} Value read
  551. */
  552. // Factory function for the purpose of node-based testing in modified global environments
  553. function factory(exports) {
  554. // float: typed array
  555. if (typeof Float32Array !== "undefined") (function() {
  556. var f32 = new Float32Array([ -0 ]),
  557. f8b = new Uint8Array(f32.buffer),
  558. le = f8b[3] === 128;
  559. function writeFloat_f32_cpy(val, buf, pos) {
  560. f32[0] = val;
  561. buf[pos ] = f8b[0];
  562. buf[pos + 1] = f8b[1];
  563. buf[pos + 2] = f8b[2];
  564. buf[pos + 3] = f8b[3];
  565. }
  566. function writeFloat_f32_rev(val, buf, pos) {
  567. f32[0] = val;
  568. buf[pos ] = f8b[3];
  569. buf[pos + 1] = f8b[2];
  570. buf[pos + 2] = f8b[1];
  571. buf[pos + 3] = f8b[0];
  572. }
  573. /* istanbul ignore next */
  574. exports.writeFloatLE = le ? writeFloat_f32_cpy : writeFloat_f32_rev;
  575. /* istanbul ignore next */
  576. exports.writeFloatBE = le ? writeFloat_f32_rev : writeFloat_f32_cpy;
  577. function readFloat_f32_cpy(buf, pos) {
  578. f8b[0] = buf[pos ];
  579. f8b[1] = buf[pos + 1];
  580. f8b[2] = buf[pos + 2];
  581. f8b[3] = buf[pos + 3];
  582. return f32[0];
  583. }
  584. function readFloat_f32_rev(buf, pos) {
  585. f8b[3] = buf[pos ];
  586. f8b[2] = buf[pos + 1];
  587. f8b[1] = buf[pos + 2];
  588. f8b[0] = buf[pos + 3];
  589. return f32[0];
  590. }
  591. /* istanbul ignore next */
  592. exports.readFloatLE = le ? readFloat_f32_cpy : readFloat_f32_rev;
  593. /* istanbul ignore next */
  594. exports.readFloatBE = le ? readFloat_f32_rev : readFloat_f32_cpy;
  595. // float: ieee754
  596. })(); else (function() {
  597. function writeFloat_ieee754(writeUint, val, buf, pos) {
  598. var sign = val < 0 ? 1 : 0;
  599. if (sign)
  600. val = -val;
  601. if (val === 0)
  602. writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos);
  603. else if (isNaN(val))
  604. writeUint(2143289344, buf, pos);
  605. else if (val > 3.4028234663852886e+38) // +-Infinity
  606. writeUint((sign << 31 | 2139095040) >>> 0, buf, pos);
  607. else if (val < 1.1754943508222875e-38) // denormal
  608. writeUint((sign << 31 | Math.round(val / 1.401298464324817e-45)) >>> 0, buf, pos);
  609. else {
  610. var exponent = Math.floor(Math.log(val) / Math.LN2),
  611. mantissa = Math.round(val * Math.pow(2, -exponent) * 8388608) & 8388607;
  612. writeUint((sign << 31 | exponent + 127 << 23 | mantissa) >>> 0, buf, pos);
  613. }
  614. }
  615. exports.writeFloatLE = writeFloat_ieee754.bind(null, writeUintLE);
  616. exports.writeFloatBE = writeFloat_ieee754.bind(null, writeUintBE);
  617. function readFloat_ieee754(readUint, buf, pos) {
  618. var uint = readUint(buf, pos),
  619. sign = (uint >> 31) * 2 + 1,
  620. exponent = uint >>> 23 & 255,
  621. mantissa = uint & 8388607;
  622. return exponent === 255
  623. ? mantissa
  624. ? NaN
  625. : sign * Infinity
  626. : exponent === 0 // denormal
  627. ? sign * 1.401298464324817e-45 * mantissa
  628. : sign * Math.pow(2, exponent - 150) * (mantissa + 8388608);
  629. }
  630. exports.readFloatLE = readFloat_ieee754.bind(null, readUintLE);
  631. exports.readFloatBE = readFloat_ieee754.bind(null, readUintBE);
  632. })();
  633. // double: typed array
  634. if (typeof Float64Array !== "undefined") (function() {
  635. var f64 = new Float64Array([-0]),
  636. f8b = new Uint8Array(f64.buffer),
  637. le = f8b[7] === 128;
  638. function writeDouble_f64_cpy(val, buf, pos) {
  639. f64[0] = val;
  640. buf[pos ] = f8b[0];
  641. buf[pos + 1] = f8b[1];
  642. buf[pos + 2] = f8b[2];
  643. buf[pos + 3] = f8b[3];
  644. buf[pos + 4] = f8b[4];
  645. buf[pos + 5] = f8b[5];
  646. buf[pos + 6] = f8b[6];
  647. buf[pos + 7] = f8b[7];
  648. }
  649. function writeDouble_f64_rev(val, buf, pos) {
  650. f64[0] = val;
  651. buf[pos ] = f8b[7];
  652. buf[pos + 1] = f8b[6];
  653. buf[pos + 2] = f8b[5];
  654. buf[pos + 3] = f8b[4];
  655. buf[pos + 4] = f8b[3];
  656. buf[pos + 5] = f8b[2];
  657. buf[pos + 6] = f8b[1];
  658. buf[pos + 7] = f8b[0];
  659. }
  660. /* istanbul ignore next */
  661. exports.writeDoubleLE = le ? writeDouble_f64_cpy : writeDouble_f64_rev;
  662. /* istanbul ignore next */
  663. exports.writeDoubleBE = le ? writeDouble_f64_rev : writeDouble_f64_cpy;
  664. function readDouble_f64_cpy(buf, pos) {
  665. f8b[0] = buf[pos ];
  666. f8b[1] = buf[pos + 1];
  667. f8b[2] = buf[pos + 2];
  668. f8b[3] = buf[pos + 3];
  669. f8b[4] = buf[pos + 4];
  670. f8b[5] = buf[pos + 5];
  671. f8b[6] = buf[pos + 6];
  672. f8b[7] = buf[pos + 7];
  673. return f64[0];
  674. }
  675. function readDouble_f64_rev(buf, pos) {
  676. f8b[7] = buf[pos ];
  677. f8b[6] = buf[pos + 1];
  678. f8b[5] = buf[pos + 2];
  679. f8b[4] = buf[pos + 3];
  680. f8b[3] = buf[pos + 4];
  681. f8b[2] = buf[pos + 5];
  682. f8b[1] = buf[pos + 6];
  683. f8b[0] = buf[pos + 7];
  684. return f64[0];
  685. }
  686. /* istanbul ignore next */
  687. exports.readDoubleLE = le ? readDouble_f64_cpy : readDouble_f64_rev;
  688. /* istanbul ignore next */
  689. exports.readDoubleBE = le ? readDouble_f64_rev : readDouble_f64_cpy;
  690. // double: ieee754
  691. })(); else (function() {
  692. function writeDouble_ieee754(writeUint, off0, off1, val, buf, pos) {
  693. var sign = val < 0 ? 1 : 0;
  694. if (sign)
  695. val = -val;
  696. if (val === 0) {
  697. writeUint(0, buf, pos + off0);
  698. writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos + off1);
  699. } else if (isNaN(val)) {
  700. writeUint(0, buf, pos + off0);
  701. writeUint(2146959360, buf, pos + off1);
  702. } else if (val > 1.7976931348623157e+308) { // +-Infinity
  703. writeUint(0, buf, pos + off0);
  704. writeUint((sign << 31 | 2146435072) >>> 0, buf, pos + off1);
  705. } else {
  706. var mantissa;
  707. if (val < 2.2250738585072014e-308) { // denormal
  708. mantissa = val / 5e-324;
  709. writeUint(mantissa >>> 0, buf, pos + off0);
  710. writeUint((sign << 31 | mantissa / 4294967296) >>> 0, buf, pos + off1);
  711. } else {
  712. var exponent = Math.floor(Math.log(val) / Math.LN2);
  713. if (exponent === 1024)
  714. exponent = 1023;
  715. mantissa = val * Math.pow(2, -exponent);
  716. writeUint(mantissa * 4503599627370496 >>> 0, buf, pos + off0);
  717. writeUint((sign << 31 | exponent + 1023 << 20 | mantissa * 1048576 & 1048575) >>> 0, buf, pos + off1);
  718. }
  719. }
  720. }
  721. exports.writeDoubleLE = writeDouble_ieee754.bind(null, writeUintLE, 0, 4);
  722. exports.writeDoubleBE = writeDouble_ieee754.bind(null, writeUintBE, 4, 0);
  723. function readDouble_ieee754(readUint, off0, off1, buf, pos) {
  724. var lo = readUint(buf, pos + off0),
  725. hi = readUint(buf, pos + off1);
  726. var sign = (hi >> 31) * 2 + 1,
  727. exponent = hi >>> 20 & 2047,
  728. mantissa = 4294967296 * (hi & 1048575) + lo;
  729. return exponent === 2047
  730. ? mantissa
  731. ? NaN
  732. : sign * Infinity
  733. : exponent === 0 // denormal
  734. ? sign * 5e-324 * mantissa
  735. : sign * Math.pow(2, exponent - 1075) * (mantissa + 4503599627370496);
  736. }
  737. exports.readDoubleLE = readDouble_ieee754.bind(null, readUintLE, 0, 4);
  738. exports.readDoubleBE = readDouble_ieee754.bind(null, readUintBE, 4, 0);
  739. })();
  740. return exports;
  741. }
  742. // uint helpers
  743. function writeUintLE(val, buf, pos) {
  744. buf[pos ] = val & 255;
  745. buf[pos + 1] = val >>> 8 & 255;
  746. buf[pos + 2] = val >>> 16 & 255;
  747. buf[pos + 3] = val >>> 24;
  748. }
  749. function writeUintBE(val, buf, pos) {
  750. buf[pos ] = val >>> 24;
  751. buf[pos + 1] = val >>> 16 & 255;
  752. buf[pos + 2] = val >>> 8 & 255;
  753. buf[pos + 3] = val & 255;
  754. }
  755. function readUintLE(buf, pos) {
  756. return (buf[pos ]
  757. | buf[pos + 1] << 8
  758. | buf[pos + 2] << 16
  759. | buf[pos + 3] << 24) >>> 0;
  760. }
  761. function readUintBE(buf, pos) {
  762. return (buf[pos ] << 24
  763. | buf[pos + 1] << 16
  764. | buf[pos + 2] << 8
  765. | buf[pos + 3]) >>> 0;
  766. }
  767. },{}],7:[function(require,module,exports){
  768. "use strict";
  769. module.exports = inquire;
  770. /**
  771. * Requires a module only if available.
  772. * @memberof util
  773. * @param {string} moduleName Module to require
  774. * @returns {?Object} Required module if available and not empty, otherwise `null`
  775. */
  776. function inquire(moduleName) {
  777. try {
  778. var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
  779. if (mod && (mod.length || Object.keys(mod).length))
  780. return mod;
  781. } catch (e) {} // eslint-disable-line no-empty
  782. return null;
  783. }
  784. },{}],8:[function(require,module,exports){
  785. "use strict";
  786. /**
  787. * A minimal path module to resolve Unix, Windows and URL paths alike.
  788. * @memberof util
  789. * @namespace
  790. */
  791. var path = exports;
  792. var isAbsolute =
  793. /**
  794. * Tests if the specified path is absolute.
  795. * @param {string} path Path to test
  796. * @returns {boolean} `true` if path is absolute
  797. */
  798. path.isAbsolute = function isAbsolute(path) {
  799. return /^(?:\/|\w+:)/.test(path);
  800. };
  801. var normalize =
  802. /**
  803. * Normalizes the specified path.
  804. * @param {string} path Path to normalize
  805. * @returns {string} Normalized path
  806. */
  807. path.normalize = function normalize(path) {
  808. path = path.replace(/\\/g, "/")
  809. .replace(/\/{2,}/g, "/");
  810. var parts = path.split("/"),
  811. absolute = isAbsolute(path),
  812. prefix = "";
  813. if (absolute)
  814. prefix = parts.shift() + "/";
  815. for (var i = 0; i < parts.length;) {
  816. if (parts[i] === "..") {
  817. if (i > 0 && parts[i - 1] !== "..")
  818. parts.splice(--i, 2);
  819. else if (absolute)
  820. parts.splice(i, 1);
  821. else
  822. ++i;
  823. } else if (parts[i] === ".")
  824. parts.splice(i, 1);
  825. else
  826. ++i;
  827. }
  828. return prefix + parts.join("/");
  829. };
  830. /**
  831. * Resolves the specified include path against the specified origin path.
  832. * @param {string} originPath Path to the origin file
  833. * @param {string} includePath Include path relative to origin path
  834. * @param {boolean} [alreadyNormalized=false] `true` if both paths are already known to be normalized
  835. * @returns {string} Path to the include file
  836. */
  837. path.resolve = function resolve(originPath, includePath, alreadyNormalized) {
  838. if (!alreadyNormalized)
  839. includePath = normalize(includePath);
  840. if (isAbsolute(includePath))
  841. return includePath;
  842. if (!alreadyNormalized)
  843. originPath = normalize(originPath);
  844. return (originPath = originPath.replace(/(?:\/|^)[^/]+$/, "")).length ? normalize(originPath + "/" + includePath) : includePath;
  845. };
  846. },{}],9:[function(require,module,exports){
  847. "use strict";
  848. module.exports = pool;
  849. /**
  850. * An allocator as used by {@link util.pool}.
  851. * @typedef PoolAllocator
  852. * @type {function}
  853. * @param {number} size Buffer size
  854. * @returns {Uint8Array} Buffer
  855. */
  856. /**
  857. * A slicer as used by {@link util.pool}.
  858. * @typedef PoolSlicer
  859. * @type {function}
  860. * @param {number} start Start offset
  861. * @param {number} end End offset
  862. * @returns {Uint8Array} Buffer slice
  863. * @this {Uint8Array}
  864. */
  865. /**
  866. * A general purpose buffer pool.
  867. * @memberof util
  868. * @function
  869. * @param {PoolAllocator} alloc Allocator
  870. * @param {PoolSlicer} slice Slicer
  871. * @param {number} [size=8192] Slab size
  872. * @returns {PoolAllocator} Pooled allocator
  873. */
  874. function pool(alloc, slice, size) {
  875. var SIZE = size || 8192;
  876. var MAX = SIZE >>> 1;
  877. var slab = null;
  878. var offset = SIZE;
  879. return function pool_alloc(size) {
  880. if (size < 1 || size > MAX)
  881. return alloc(size);
  882. if (offset + size > SIZE) {
  883. slab = alloc(SIZE);
  884. offset = 0;
  885. }
  886. var buf = slice.call(slab, offset, offset += size);
  887. if (offset & 7) // align to 32 bit
  888. offset = (offset | 7) + 1;
  889. return buf;
  890. };
  891. }
  892. },{}],10:[function(require,module,exports){
  893. "use strict";
  894. /**
  895. * A minimal UTF8 implementation for number arrays.
  896. * @memberof util
  897. * @namespace
  898. */
  899. var utf8 = exports;
  900. /**
  901. * Calculates the UTF8 byte length of a string.
  902. * @param {string} string String
  903. * @returns {number} Byte length
  904. */
  905. utf8.length = function utf8_length(string) {
  906. var len = 0,
  907. c = 0;
  908. for (var i = 0; i < string.length; ++i) {
  909. c = string.charCodeAt(i);
  910. if (c < 128)
  911. len += 1;
  912. else if (c < 2048)
  913. len += 2;
  914. else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
  915. ++i;
  916. len += 4;
  917. } else
  918. len += 3;
  919. }
  920. return len;
  921. };
  922. /**
  923. * Reads UTF8 bytes as a string.
  924. * @param {Uint8Array} buffer Source buffer
  925. * @param {number} start Source start
  926. * @param {number} end Source end
  927. * @returns {string} String read
  928. */
  929. utf8.read = function utf8_read(buffer, start, end) {
  930. var len = end - start;
  931. if (len < 1)
  932. return "";
  933. var parts = null,
  934. chunk = [],
  935. i = 0, // char offset
  936. t; // temporary
  937. while (start < end) {
  938. t = buffer[start++];
  939. if (t < 128)
  940. chunk[i++] = t;
  941. else if (t > 191 && t < 224)
  942. chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
  943. else if (t > 239 && t < 365) {
  944. t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
  945. chunk[i++] = 0xD800 + (t >> 10);
  946. chunk[i++] = 0xDC00 + (t & 1023);
  947. } else
  948. chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
  949. if (i > 8191) {
  950. (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
  951. i = 0;
  952. }
  953. }
  954. if (parts) {
  955. if (i)
  956. parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
  957. return parts.join("");
  958. }
  959. return String.fromCharCode.apply(String, chunk.slice(0, i));
  960. };
  961. /**
  962. * Writes a string as UTF8 bytes.
  963. * @param {string} string Source string
  964. * @param {Uint8Array} buffer Destination buffer
  965. * @param {number} offset Destination offset
  966. * @returns {number} Bytes written
  967. */
  968. utf8.write = function utf8_write(string, buffer, offset) {
  969. var start = offset,
  970. c1, // character 1
  971. c2; // character 2
  972. for (var i = 0; i < string.length; ++i) {
  973. c1 = string.charCodeAt(i);
  974. if (c1 < 128) {
  975. buffer[offset++] = c1;
  976. } else if (c1 < 2048) {
  977. buffer[offset++] = c1 >> 6 | 192;
  978. buffer[offset++] = c1 & 63 | 128;
  979. } else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
  980. c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
  981. ++i;
  982. buffer[offset++] = c1 >> 18 | 240;
  983. buffer[offset++] = c1 >> 12 & 63 | 128;
  984. buffer[offset++] = c1 >> 6 & 63 | 128;
  985. buffer[offset++] = c1 & 63 | 128;
  986. } else {
  987. buffer[offset++] = c1 >> 12 | 224;
  988. buffer[offset++] = c1 >> 6 & 63 | 128;
  989. buffer[offset++] = c1 & 63 | 128;
  990. }
  991. }
  992. return offset - start;
  993. };
  994. },{}],11:[function(require,module,exports){
  995. "use strict";
  996. /**
  997. * Runtime message from/to plain object converters.
  998. * @namespace
  999. */
  1000. var converter = exports;
  1001. var Enum = require(14),
  1002. util = require(33);
  1003. /**
  1004. * Generates a partial value fromObject conveter.
  1005. * @param {Codegen} gen Codegen instance
  1006. * @param {Field} field Reflected field
  1007. * @param {number} fieldIndex Field index
  1008. * @param {string} prop Property reference
  1009. * @returns {Codegen} Codegen instance
  1010. * @ignore
  1011. */
  1012. function genValuePartial_fromObject(gen, field, fieldIndex, prop, ref) {
  1013. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1014. if (ref === undefined) {
  1015. ref = "d" + prop;
  1016. }
  1017. if (field.resolvedType) {
  1018. if (field.resolvedType instanceof Enum) { gen
  1019. ("switch(%s){", ref);
  1020. for (var values = field.resolvedType.values, keys = Object.keys(values), i = 0; i < keys.length; ++i) {
  1021. if (field.repeated && values[keys[i]] === field.typeDefault) gen
  1022. ("default:");
  1023. gen
  1024. ("case%j:", keys[i])
  1025. ("case %i:", values[keys[i]])
  1026. ("m%s=%j", prop, values[keys[i]])
  1027. ("break");
  1028. } gen
  1029. ("}");
  1030. } else gen
  1031. ("if(typeof %s!==\"object\")", ref)
  1032. ("throw TypeError(%j)", field.fullName + ": object expected")
  1033. ("m%s=types[%i].fromObject(%s)", prop, fieldIndex, ref);
  1034. } else {
  1035. var isUnsigned = false;
  1036. switch (field.type) {
  1037. case "double":
  1038. case "float": gen
  1039. ("m%s=Number(%s)", prop, ref); // also catches "NaN", "Infinity"
  1040. break;
  1041. case "uint32":
  1042. case "fixed32": gen
  1043. ("m%s=%s>>>0", prop, ref);
  1044. break;
  1045. case "int32":
  1046. case "sint32":
  1047. case "sfixed32": gen
  1048. ("m%s=%s|0", prop, ref);
  1049. break;
  1050. case "uint64":
  1051. isUnsigned = true;
  1052. // eslint-disable-line no-fallthrough
  1053. case "int64":
  1054. case "sint64":
  1055. case "fixed64":
  1056. case "sfixed64": gen
  1057. ("if(util.Long)")
  1058. ("(m%s=util.Long.fromValue(%s)).unsigned=%j", prop, ref, isUnsigned)
  1059. ("else if(typeof %s===\"string\")", ref)
  1060. ("m%s=parseInt(%s,10)", prop, ref)
  1061. ("else if(typeof %s===\"number\")", ref)
  1062. ("m%s=%s", prop, ref)
  1063. ("else if(typeof %s===\"object\")", ref)
  1064. ("m%s=new util.LongBits(%s.low>>>0,%s.high>>>0).toNumber(%s)", prop, ref, ref, isUnsigned ? "true" : "");
  1065. break;
  1066. case "bytes": gen
  1067. ("if(typeof %s===\"string\")", ref)
  1068. ("util.base64.decode(%s,m%s=util.newBuffer(util.base64.length(%s)),0)", ref, prop, ref)
  1069. ("else if(%s.length)", ref)
  1070. ("m%s=%s", prop, ref);
  1071. break;
  1072. case "string": gen
  1073. ("m%s=String(%s)", prop, ref);
  1074. break;
  1075. case "bool": gen
  1076. ("m%s=Boolean(%s)", prop, ref);
  1077. break;
  1078. /* default: gen
  1079. ("m%s=%s", prop, ref);
  1080. break; */
  1081. }
  1082. }
  1083. return gen;
  1084. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1085. }
  1086. /**
  1087. * Generates a plain object to runtime message converter specific to the specified message type.
  1088. * @param {Type} mtype Message type
  1089. * @returns {Codegen} Codegen instance
  1090. */
  1091. converter.fromObject = function fromObject(mtype) {
  1092. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1093. var fields = mtype.fieldsArray;
  1094. var gen = util.codegen(["d"], mtype.name + "$fromObject")
  1095. ("if(d instanceof this.ctor)")
  1096. ("return d");
  1097. if (!fields.length) return gen
  1098. ("return new this.ctor");
  1099. gen
  1100. ("var m=new this.ctor");
  1101. for (var i = 0; i < fields.length; ++i) {
  1102. var field = fields[i].resolve(),
  1103. prop = util.safeProp(field.name);
  1104. // Map fields
  1105. if (field.map) { gen
  1106. ("if(d%s){", prop)
  1107. ("if(typeof d%s!==\"object\")", prop)
  1108. ("throw TypeError(%j)", field.fullName + ": object expected")
  1109. ("m%s={}", prop)
  1110. ("for(var ks=Object.keys(d%s),i=0;i<ks.length;++i){", prop);
  1111. genValuePartial_fromObject(gen, field, /* not sorted */ i, prop + "[ks[i]]")
  1112. ("}")
  1113. ("}");
  1114. // Repeated fields
  1115. } else if (field.repeated) {
  1116. gen("if(d%s){", prop);
  1117. var arrayRef = "d" + prop;
  1118. if (field.useToArray()) {
  1119. arrayRef = "array" + field.id;
  1120. gen("var %s", arrayRef);
  1121. gen("if (d%s!=null&&d%s.toArray) { %s = d%s.toArray() } else { %s = d%s }",
  1122. prop, prop, arrayRef, prop, arrayRef, prop);
  1123. }
  1124. gen
  1125. ("if(!Array.isArray(%s))", arrayRef)
  1126. ("throw TypeError(%j)", field.fullName + ": array expected")
  1127. ("m%s=[]", prop)
  1128. ("for(var i=0;i<%s.length;++i){", arrayRef);
  1129. genValuePartial_fromObject(gen, field, /* not sorted */ i, prop + "[i]", arrayRef + "[i]")
  1130. ("}")
  1131. ("}");
  1132. // Non-repeated fields
  1133. } else {
  1134. if (!(field.resolvedType instanceof Enum)) gen // no need to test for null/undefined if an enum (uses switch)
  1135. ("if(d%s!=null){", prop); // !== undefined && !== null
  1136. genValuePartial_fromObject(gen, field, /* not sorted */ i, prop);
  1137. if (!(field.resolvedType instanceof Enum)) gen
  1138. ("}");
  1139. }
  1140. } return gen
  1141. ("return m");
  1142. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1143. };
  1144. /**
  1145. * Generates a partial value toObject converter.
  1146. * @param {Codegen} gen Codegen instance
  1147. * @param {Field} field Reflected field
  1148. * @param {number} fieldIndex Field index
  1149. * @param {string} prop Property reference
  1150. * @returns {Codegen} Codegen instance
  1151. * @ignore
  1152. */
  1153. function genValuePartial_toObject(gen, field, fieldIndex, prop) {
  1154. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1155. if (field.resolvedType) {
  1156. if (field.resolvedType instanceof Enum) gen
  1157. ("d%s=o.enums===String?types[%i].values[m%s]:m%s", prop, fieldIndex, prop, prop);
  1158. else gen
  1159. ("d%s=types[%i].toObject(m%s,o)", prop, fieldIndex, prop);
  1160. } else {
  1161. var isUnsigned = false;
  1162. switch (field.type) {
  1163. case "double":
  1164. case "float": gen
  1165. ("d%s=o.json&&!isFinite(m%s)?String(m%s):m%s", prop, prop, prop, prop);
  1166. break;
  1167. case "uint64":
  1168. isUnsigned = true;
  1169. // eslint-disable-line no-fallthrough
  1170. case "int64":
  1171. case "sint64":
  1172. case "fixed64":
  1173. case "sfixed64": gen
  1174. ("if(typeof m%s===\"number\")", prop)
  1175. ("d%s=o.longs===String?String(m%s):m%s", prop, prop, prop)
  1176. ("else") // Long-like
  1177. ("d%s=o.longs===String?util.Long.prototype.toString.call(m%s):o.longs===Number?new util.LongBits(m%s.low>>>0,m%s.high>>>0).toNumber(%s):m%s", prop, prop, prop, prop, isUnsigned ? "true": "", prop);
  1178. break;
  1179. case "bytes": gen
  1180. ("d%s=o.bytes===String?util.base64.encode(m%s,0,m%s.length):o.bytes===Array?Array.prototype.slice.call(m%s):m%s", prop, prop, prop, prop, prop);
  1181. break;
  1182. default: gen
  1183. ("d%s=m%s", prop, prop);
  1184. break;
  1185. }
  1186. }
  1187. return gen;
  1188. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1189. }
  1190. /**
  1191. * Generates a runtime message to plain object converter specific to the specified message type.
  1192. * @param {Type} mtype Message type
  1193. * @returns {Codegen} Codegen instance
  1194. */
  1195. converter.toObject = function toObject(mtype) {
  1196. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1197. var fields = mtype.fieldsArray.slice().sort(util.compareFieldsById);
  1198. if (!fields.length)
  1199. return util.codegen()("return {}");
  1200. var gen = util.codegen(["m", "o"], mtype.name + "$toObject")
  1201. ("if(!o)")
  1202. ("o={}")
  1203. ("var d={}");
  1204. var repeatedFields = [],
  1205. mapFields = [],
  1206. normalFields = [],
  1207. i = 0;
  1208. for (; i < fields.length; ++i)
  1209. if (!fields[i].partOf)
  1210. ( fields[i].resolve().repeated ? repeatedFields
  1211. : fields[i].map ? mapFields
  1212. : normalFields).push(fields[i]);
  1213. if (repeatedFields.length) { gen
  1214. ("if(o.arrays||o.defaults){");
  1215. for (i = 0; i < repeatedFields.length; ++i) gen
  1216. ("d%s=[]", util.safeProp(repeatedFields[i].name));
  1217. gen
  1218. ("}");
  1219. }
  1220. if (mapFields.length) { gen
  1221. ("if(o.objects||o.defaults){");
  1222. for (i = 0; i < mapFields.length; ++i) gen
  1223. ("d%s={}", util.safeProp(mapFields[i].name));
  1224. gen
  1225. ("}");
  1226. }
  1227. if (normalFields.length) { gen
  1228. ("if(o.defaults){");
  1229. for (i = 0; i < normalFields.length; ++i) {
  1230. var field = normalFields[i],
  1231. prop = util.safeProp(field.name);
  1232. if (field.resolvedType instanceof Enum) gen
  1233. ("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);
  1234. else if (field.long) gen
  1235. ("if(util.Long){")
  1236. ("var n=new util.Long(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned)
  1237. ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop)
  1238. ("}else")
  1239. ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber());
  1240. else if (field.bytes) {
  1241. var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]";
  1242. gen
  1243. ("if(o.bytes===String)d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
  1244. ("else{")
  1245. ("d%s=%s", prop, arrayDefault)
  1246. ("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop)
  1247. ("}");
  1248. } else gen
  1249. ("d%s=%j", prop, field.typeDefault); // also messages (=null)
  1250. } gen
  1251. ("}");
  1252. }
  1253. var hasKs2 = false;
  1254. for (i = 0; i < fields.length; ++i) {
  1255. var field = fields[i],
  1256. index = mtype._fieldsArray.indexOf(field),
  1257. prop = util.safeProp(field.name);
  1258. if (field.map) {
  1259. if (!hasKs2) { hasKs2 = true; gen
  1260. ("var ks2");
  1261. } gen
  1262. ("if(m%s&&(ks2=Object.keys(m%s)).length){", prop, prop)
  1263. ("d%s={}", prop)
  1264. ("for(var j=0;j<ks2.length;++j){");
  1265. genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[ks2[j]]")
  1266. ("}");
  1267. } else if (field.repeated) { gen
  1268. ("if(m%s&&m%s.length){", prop, prop)
  1269. ("d%s=[]", prop)
  1270. ("for(var j=0;j<m%s.length;++j){", prop);
  1271. genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
  1272. ("}");
  1273. } else { gen
  1274. ("if(m%s!=null&&m.hasOwnProperty(%j)){", prop, field.name); // !== undefined && !== null
  1275. genValuePartial_toObject(gen, field, /* sorted */ index, prop);
  1276. if (field.partOf) gen
  1277. ("if(o.oneofs)")
  1278. ("d%s=%j", util.safeProp(field.partOf.name), field.name);
  1279. }
  1280. gen
  1281. ("}");
  1282. }
  1283. return gen
  1284. ("return d");
  1285. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1286. };
  1287. },{"14":14,"33":33}],12:[function(require,module,exports){
  1288. "use strict";
  1289. module.exports = decoder;
  1290. var Enum = require(14),
  1291. types = require(32),
  1292. util = require(33);
  1293. function missing(field) {
  1294. return "missing required '" + field.name + "'";
  1295. }
  1296. /**
  1297. * Generates a decoder specific to the specified message type.
  1298. * @param {Type} mtype Message type
  1299. * @returns {Codegen} Codegen instance
  1300. */
  1301. function decoder(mtype) {
  1302. /* eslint-disable no-unexpected-multiline */
  1303. var gen = util.codegen(["r", "l"], mtype.name + "$decode")
  1304. ("if(!(r instanceof Reader))")
  1305. ("r=Reader.create(r)")
  1306. ("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k" : ""))
  1307. ("while(r.pos<c){")
  1308. ("var t=r.uint32()");
  1309. if (mtype.group) gen
  1310. ("if((t&7)===4)")
  1311. ("break");
  1312. gen
  1313. ("switch(t>>>3){");
  1314. var i = 0;
  1315. for (; i < /* initializes */ mtype.fieldsArray.length; ++i) {
  1316. var field = mtype._fieldsArray[i].resolve(),
  1317. type = field.resolvedType instanceof Enum ? "int32" : field.type,
  1318. ref = "m" + util.safeProp(field.name); gen
  1319. ("case %i:", field.id);
  1320. // Map fields
  1321. if (field.map) { gen
  1322. ("r.skip().pos++") // assumes id 1 + key wireType
  1323. ("if(%s===util.emptyObject)", ref)
  1324. ("%s={}", ref)
  1325. ("k=r.%s()", field.keyType)
  1326. ("r.pos++"); // assumes id 2 + value wireType
  1327. if (types.long[field.keyType] !== undefined) {
  1328. if (types.basic[type] === undefined) gen
  1329. ("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
  1330. else gen
  1331. ("%s[typeof k===\"object\"?util.longToHash(k):k]=r.%s()", ref, type);
  1332. } else {
  1333. if (types.basic[type] === undefined) gen
  1334. ("%s[k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
  1335. else gen
  1336. ("%s[k]=r.%s()", ref, type);
  1337. }
  1338. // Repeated fields
  1339. } else if (field.repeated) { gen
  1340. ("if(!(%s&&%s.length))", ref, ref)
  1341. ("%s=[]", ref);
  1342. // Packable (always check for forward and backward compatiblity)
  1343. if (types.packed[type] !== undefined) gen
  1344. ("if((t&7)===2){")
  1345. ("var c2=r.uint32()+r.pos")
  1346. ("while(r.pos<c2)")
  1347. ("%s.push(r.%s())", ref, type)
  1348. ("}else");
  1349. // Non-packed
  1350. if (types.basic[type] === undefined) gen(field.resolvedType.group
  1351. ? "%s.push(types[%i].decode(r))"
  1352. : "%s.push(types[%i].decode(r,r.uint32()))", ref, i);
  1353. else gen
  1354. ("%s.push(r.%s())", ref, type);
  1355. // Non-repeated
  1356. } else if (types.basic[type] === undefined) gen(field.resolvedType.group
  1357. ? "%s=types[%i].decode(r)"
  1358. : "%s=types[%i].decode(r,r.uint32())", ref, i);
  1359. else gen
  1360. ("%s=r.%s()", ref, type);
  1361. gen
  1362. ("break");
  1363. // Unknown fields
  1364. } gen
  1365. ("default:")
  1366. ("r.skipType(t&7)")
  1367. ("break")
  1368. ("}")
  1369. ("}");
  1370. // Field presence
  1371. for (i = 0; i < mtype._fieldsArray.length; ++i) {
  1372. var rfield = mtype._fieldsArray[i];
  1373. if (rfield.required) gen
  1374. ("if(!m.hasOwnProperty(%j))", rfield.name)
  1375. ("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
  1376. }
  1377. return gen
  1378. ("return m");
  1379. /* eslint-enable no-unexpected-multiline */
  1380. }
  1381. },{"14":14,"32":32,"33":33}],13:[function(require,module,exports){
  1382. "use strict";
  1383. module.exports = encoder;
  1384. var Enum = require(14),
  1385. types = require(32),
  1386. util = require(33);
  1387. /**
  1388. * Generates a partial message type encoder.
  1389. * @param {Codegen} gen Codegen instance
  1390. * @param {Field} field Reflected field
  1391. * @param {number} fieldIndex Field index
  1392. * @param {string} ref Variable reference
  1393. * @returns {Codegen} Codegen instance
  1394. * @ignore
  1395. */
  1396. function genTypePartial(gen, field, fieldIndex, ref) {
  1397. /* eslint-disable no-unexpected-multiline */
  1398. if (field.resolvedType.group) {
  1399. gen("types[%i].encode(%s,w.uint32(%i)).uint32(%i)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0);
  1400. return;
  1401. }
  1402. var key = (field.id << 3 | 2) >>> 0;
  1403. if (field.preEncoded()) {
  1404. gen("if (%s instanceof Uint8Array) {", ref)
  1405. ("w.uint32(%i)", key)
  1406. ("w.bytes(%s)", ref)
  1407. ("} else {");
  1408. }
  1409. gen("types[%i].encode(%s,w.uint32(%i).fork()).ldelim()", fieldIndex, ref, key);
  1410. if (field.preEncoded()) {
  1411. gen("}")
  1412. }
  1413. }
  1414. /**
  1415. * Generates an encoder specific to the specified message type.
  1416. * @param {Type} mtype Message type
  1417. * @returns {Codegen} Codegen instance
  1418. */
  1419. function encoder(mtype) {
  1420. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1421. var gen = util.codegen(["m", "w"], mtype.name + "$encode")
  1422. ("if(!w)")
  1423. ("w=Writer.create()");
  1424. var i, ref;
  1425. // "when a message is serialized its known fields should be written sequentially by field number"
  1426. var fields = /* initializes */ mtype.fieldsArray.slice().sort(util.compareFieldsById);
  1427. for (var i = 0; i < fields.length; ++i) {
  1428. var field = fields[i].resolve(),
  1429. index = mtype._fieldsArray.indexOf(field),
  1430. type = field.resolvedType instanceof Enum ? "int32" : field.type,
  1431. wireType = types.basic[type];
  1432. ref = "m" + util.safeProp(field.name);
  1433. // Map fields
  1434. if (field.map) {
  1435. gen
  1436. ("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name) // !== undefined && !== null
  1437. ("for(var ks=Object.keys(%s),i=0;i<ks.length;++i){", ref)
  1438. ("w.uint32(%i).fork().uint32(%i).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[field.keyType], field.keyType);
  1439. if (wireType === undefined) gen
  1440. ("types[%i].encode(%s[ks[i]],w.uint32(18).fork()).ldelim().ldelim()", index, ref); // can't be groups
  1441. else gen
  1442. (".uint32(%i).%s(%s[ks[i]]).ldelim()", 16 | wireType, type, ref);
  1443. gen
  1444. ("}")
  1445. ("}");
  1446. // Repeated fields
  1447. } else if (field.repeated) {
  1448. var arrayRef = ref;
  1449. if (field.useToArray()) {
  1450. arrayRef = "array" + field.id;
  1451. gen("var %s", arrayRef);
  1452. gen("if (%s!=null&&%s.toArray) { %s = %s.toArray() } else { %s = %s }",
  1453. ref, ref, arrayRef, ref, arrayRef, ref);
  1454. }
  1455. gen("if(%s!=null&&%s.length){", arrayRef, arrayRef); // !== undefined && !== null
  1456. // Packed repeated
  1457. if (field.packed && types.packed[type] !== undefined) { gen
  1458. ("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
  1459. ("for(var i=0;i<%s.length;++i)", arrayRef)
  1460. ("w.%s(%s[i])", type, arrayRef)
  1461. ("w.ldelim()");
  1462. // Non-packed
  1463. } else { gen
  1464. ("for(var i=0;i<%s.length;++i)", arrayRef);
  1465. if (wireType === undefined)
  1466. genTypePartial(gen, field, index, arrayRef + "[i]");
  1467. else gen
  1468. ("w.uint32(%i).%s(%s[i])", (field.id << 3 | wireType) >>> 0, type, arrayRef);
  1469. } gen
  1470. ("}");
  1471. // Non-repeated
  1472. } else {
  1473. if (field.optional) gen
  1474. ("if(%s!=null&&Object.hasOwnProperty.call(m,%j))", ref, field.name); // !== undefined && !== null
  1475. if (wireType === undefined)
  1476. genTypePartial(gen, field, index, ref);
  1477. else gen
  1478. ("w.uint32(%i).%s(%s)", (field.id << 3 | wireType) >>> 0, type, ref);
  1479. }
  1480. }
  1481. return gen
  1482. ("return w");
  1483. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  1484. }
  1485. },{"14":14,"32":32,"33":33}],14:[function(require,module,exports){
  1486. "use strict";
  1487. module.exports = Enum;
  1488. // extends ReflectionObject
  1489. var ReflectionObject = require(22);
  1490. ((Enum.prototype = Object.create(ReflectionObject.prototype)).constructor = Enum).className = "Enum";
  1491. var Namespace = require(21),
  1492. util = require(33);
  1493. /**
  1494. * Constructs a new enum instance.
  1495. * @classdesc Reflected enum.
  1496. * @extends ReflectionObject
  1497. * @constructor
  1498. * @param {string} name Unique name within its namespace
  1499. * @param {Object.<string,number>} [values] Enum values as an object, by name
  1500. * @param {Object.<string,*>} [options] Declared options
  1501. * @param {string} [comment] The comment for this enum
  1502. * @param {Object.<string,string>} [comments] The value comments for this enum
  1503. */
  1504. function Enum(name, values, options, comment, comments) {
  1505. ReflectionObject.call(this, name, options);
  1506. if (values && typeof values !== "object")
  1507. throw TypeError("values must be an object");
  1508. /**
  1509. * Enum values by id.
  1510. * @type {Object.<number,string>}
  1511. */
  1512. this.valuesById = {};
  1513. /**
  1514. * Enum values by name.
  1515. * @type {Object.<string,number>}
  1516. */
  1517. this.values = Object.create(this.valuesById); // toJSON, marker
  1518. /**
  1519. * Enum comment text.
  1520. * @type {string|null}
  1521. */
  1522. this.comment = comment;
  1523. /**
  1524. * Value comment texts, if any.
  1525. * @type {Object.<string,string>}
  1526. */
  1527. this.comments = comments || {};
  1528. /**
  1529. * Reserved ranges, if any.
  1530. * @type {Array.<number[]|string>}
  1531. */
  1532. this.reserved = undefined; // toJSON
  1533. // Note that values inherit valuesById on their prototype which makes them a TypeScript-
  1534. // compatible enum. This is used by pbts to write actual enum definitions that work for
  1535. // static and reflection code alike instead of emitting generic object definitions.
  1536. if (values)
  1537. for (var keys = Object.keys(values), i = 0; i < keys.length; ++i)
  1538. if (typeof values[keys[i]] === "number") // use forward entries only
  1539. this.valuesById[ this.values[keys[i]] = values[keys[i]] ] = keys[i];
  1540. }
  1541. /**
  1542. * Enum descriptor.
  1543. * @interface IEnum
  1544. * @property {Object.<string,number>} values Enum values
  1545. * @property {Object.<string,*>} [options] Enum options
  1546. */
  1547. /**
  1548. * Constructs an enum from an enum descriptor.
  1549. * @param {string} name Enum name
  1550. * @param {IEnum} json Enum descriptor
  1551. * @returns {Enum} Created enum
  1552. * @throws {TypeError} If arguments are invalid
  1553. */
  1554. Enum.fromJSON = function fromJSON(name, json) {
  1555. var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
  1556. enm.reserved = json.reserved;
  1557. return enm;
  1558. };
  1559. /**
  1560. * Converts this enum to an enum descriptor.
  1561. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  1562. * @returns {IEnum} Enum descriptor
  1563. */
  1564. Enum.prototype.toJSON = function toJSON(toJSONOptions) {
  1565. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  1566. return util.toObject([
  1567. "options" , this.options,
  1568. "values" , this.values,
  1569. "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
  1570. "comment" , keepComments ? this.comment : undefined,
  1571. "comments" , keepComments ? this.comments : undefined
  1572. ]);
  1573. };
  1574. /**
  1575. * Adds a value to this enum.
  1576. * @param {string} name Value name
  1577. * @param {number} id Value id
  1578. * @param {string} [comment] Comment, if any
  1579. * @returns {Enum} `this`
  1580. * @throws {TypeError} If arguments are invalid
  1581. * @throws {Error} If there is already a value with this name or id
  1582. */
  1583. Enum.prototype.add = function add(name, id, comment) {
  1584. // utilized by the parser but not by .fromJSON
  1585. if (!util.isString(name))
  1586. throw TypeError("name must be a string");
  1587. if (!util.isInteger(id))
  1588. throw TypeError("id must be an integer");
  1589. if (this.values[name] !== undefined)
  1590. throw Error("duplicate name '" + name + "' in " + this);
  1591. if (this.isReservedId(id))
  1592. throw Error("id " + id + " is reserved in " + this);
  1593. if (this.isReservedName(name))
  1594. throw Error("name '" + name + "' is reserved in " + this);
  1595. if (this.valuesById[id] !== undefined) {
  1596. if (!(this.options && this.options.allow_alias))
  1597. throw Error("duplicate id " + id + " in " + this);
  1598. this.values[name] = id;
  1599. } else
  1600. this.valuesById[this.values[name] = id] = name;
  1601. this.comments[name] = comment || null;
  1602. return this;
  1603. };
  1604. /**
  1605. * Removes a value from this enum
  1606. * @param {string} name Value name
  1607. * @returns {Enum} `this`
  1608. * @throws {TypeError} If arguments are invalid
  1609. * @throws {Error} If `name` is not a name of this enum
  1610. */
  1611. Enum.prototype.remove = function remove(name) {
  1612. if (!util.isString(name))
  1613. throw TypeError("name must be a string");
  1614. var val = this.values[name];
  1615. if (val == null)
  1616. throw Error("name '" + name + "' does not exist in " + this);
  1617. delete this.valuesById[val];
  1618. delete this.values[name];
  1619. delete this.comments[name];
  1620. return this;
  1621. };
  1622. /**
  1623. * Tests if the specified id is reserved.
  1624. * @param {number} id Id to test
  1625. * @returns {boolean} `true` if reserved, otherwise `false`
  1626. */
  1627. Enum.prototype.isReservedId = function isReservedId(id) {
  1628. return Namespace.isReservedId(this.reserved, id);
  1629. };
  1630. /**
  1631. * Tests if the specified name is reserved.
  1632. * @param {string} name Name to test
  1633. * @returns {boolean} `true` if reserved, otherwise `false`
  1634. */
  1635. Enum.prototype.isReservedName = function isReservedName(name) {
  1636. return Namespace.isReservedName(this.reserved, name);
  1637. };
  1638. },{"21":21,"22":22,"33":33}],15:[function(require,module,exports){
  1639. "use strict";
  1640. module.exports = Field;
  1641. // extends ReflectionObject
  1642. var ReflectionObject = require(22);
  1643. ((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
  1644. var Enum = require(14),
  1645. types = require(32),
  1646. util = require(33);
  1647. var Type; // cyclic
  1648. var ruleRe = /^required|optional|repeated$/;
  1649. /**
  1650. * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
  1651. * @name Field
  1652. * @classdesc Reflected message field.
  1653. * @extends FieldBase
  1654. * @constructor
  1655. * @param {string} name Unique name within its namespace
  1656. * @param {number} id Unique id within its namespace
  1657. * @param {string} type Value type
  1658. * @param {string|Object.<string,*>} [rule="optional"] Field rule
  1659. * @param {string|Object.<string,*>} [extend] Extended type if different from parent
  1660. * @param {Object.<string,*>} [options] Declared options
  1661. */
  1662. /**
  1663. * Constructs a field from a field descriptor.
  1664. * @param {string} name Field name
  1665. * @param {IField} json Field descriptor
  1666. * @returns {Field} Created field
  1667. * @throws {TypeError} If arguments are invalid
  1668. */
  1669. Field.fromJSON = function fromJSON(name, json) {
  1670. return new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
  1671. };
  1672. /**
  1673. * Not an actual constructor. Use {@link Field} instead.
  1674. * @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
  1675. * @exports FieldBase
  1676. * @extends ReflectionObject
  1677. * @constructor
  1678. * @param {string} name Unique name within its namespace
  1679. * @param {number} id Unique id within its namespace
  1680. * @param {string} type Value type
  1681. * @param {string|Object.<string,*>} [rule="optional"] Field rule
  1682. * @param {string|Object.<string,*>} [extend] Extended type if different from parent
  1683. * @param {Object.<string,*>} [options] Declared options
  1684. * @param {string} [comment] Comment associated with this field
  1685. */
  1686. function Field(name, id, type, rule, extend, options, comment) {
  1687. if (util.isObject(rule)) {
  1688. comment = extend;
  1689. options = rule;
  1690. rule = extend = undefined;
  1691. } else if (util.isObject(extend)) {
  1692. comment = options;
  1693. options = extend;
  1694. extend = undefined;
  1695. }
  1696. ReflectionObject.call(this, name, options);
  1697. if (!util.isInteger(id) || id < 0)
  1698. throw TypeError("id must be a non-negative integer");
  1699. if (!util.isString(type))
  1700. throw TypeError("type must be a string");
  1701. if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
  1702. throw TypeError("rule must be a string rule");
  1703. if (extend !== undefined && !util.isString(extend))
  1704. throw TypeError("extend must be a string");
  1705. /**
  1706. * Field rule, if any.
  1707. * @type {string|undefined}
  1708. */
  1709. this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
  1710. /**
  1711. * Field type.
  1712. * @type {string}
  1713. */
  1714. this.type = type; // toJSON
  1715. /**
  1716. * Unique field id.
  1717. * @type {number}
  1718. */
  1719. this.id = id; // toJSON, marker
  1720. /**
  1721. * Extended type if different from parent.
  1722. * @type {string|undefined}
  1723. */
  1724. this.extend = extend || undefined; // toJSON
  1725. /**
  1726. * Whether this field is required.
  1727. * @type {boolean}
  1728. */
  1729. this.required = rule === "required";
  1730. /**
  1731. * Whether this field is optional.
  1732. * @type {boolean}
  1733. */
  1734. this.optional = !this.required;
  1735. /**
  1736. * Whether this field is repeated.
  1737. * @type {boolean}
  1738. */
  1739. this.repeated = rule === "repeated";
  1740. /**
  1741. * Whether this field is a map or not.
  1742. * @type {boolean}
  1743. */
  1744. this.map = false;
  1745. /**
  1746. * Message this field belongs to.
  1747. * @type {Type|null}
  1748. */
  1749. this.message = null;
  1750. /**
  1751. * OneOf this field belongs to, if any,
  1752. * @type {OneOf|null}
  1753. */
  1754. this.partOf = null;
  1755. /**
  1756. * The field type's default value.
  1757. * @type {*}
  1758. */
  1759. this.typeDefault = null;
  1760. /**
  1761. * The field's default value on prototypes.
  1762. * @type {*}
  1763. */
  1764. this.defaultValue = null;
  1765. /**
  1766. * Whether this field's value should be treated as a long.
  1767. * @type {boolean}
  1768. */
  1769. this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
  1770. /**
  1771. * Whether this field's value is a buffer.
  1772. * @type {boolean}
  1773. */
  1774. this.bytes = type === "bytes";
  1775. /**
  1776. * Resolved type if not a basic type.
  1777. * @type {Type|Enum|null}
  1778. */
  1779. this.resolvedType = null;
  1780. /**
  1781. * Sister-field within the extended type if a declaring extension field.
  1782. * @type {Field|null}
  1783. */
  1784. this.extensionField = null;
  1785. /**
  1786. * Sister-field within the declaring namespace if an extended field.
  1787. * @type {Field|null}
  1788. */
  1789. this.declaringField = null;
  1790. /**
  1791. * Internally remembers whether this field is packed.
  1792. * @type {boolean|null}
  1793. * @private
  1794. */
  1795. this._packed = null;
  1796. /**
  1797. * Comment for this field.
  1798. * @type {string|null}
  1799. */
  1800. this.comment = comment;
  1801. }
  1802. /**
  1803. * Determines whether this field is packed. Only relevant when repeated and working with proto2.
  1804. * @name Field#packed
  1805. * @type {boolean}
  1806. * @readonly
  1807. */
  1808. Object.defineProperty(Field.prototype, "packed", {
  1809. get: function() {
  1810. // defaults to packed=true if not explicity set to false
  1811. if (this._packed === null)
  1812. this._packed = this.getOption("packed") !== false;
  1813. return this._packed;
  1814. }
  1815. });
  1816. /**
  1817. * @override
  1818. */
  1819. Field.prototype.setOption = function setOption(name, value, ifNotSet) {
  1820. if (name === "packed") // clear cached before setting
  1821. this._packed = null;
  1822. return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
  1823. };
  1824. /**
  1825. * Field descriptor.
  1826. * @interface IField
  1827. * @property {string} [rule="optional"] Field rule
  1828. * @property {string} type Field type
  1829. * @property {number} id Field id
  1830. * @property {Object.<string,*>} [options] Field options
  1831. */
  1832. /**
  1833. * Extension field descriptor.
  1834. * @interface IExtensionField
  1835. * @extends IField
  1836. * @property {string} extend Extended type
  1837. */
  1838. /**
  1839. * Converts this field to a field descriptor.
  1840. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  1841. * @returns {IField} Field descriptor
  1842. */
  1843. Field.prototype.toJSON = function toJSON(toJSONOptions) {
  1844. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  1845. return util.toObject([
  1846. "rule" , this.rule !== "optional" && this.rule || undefined,
  1847. "type" , this.type,
  1848. "id" , this.id,
  1849. "extend" , this.extend,
  1850. "options" , this.options,
  1851. "comment" , keepComments ? this.comment : undefined
  1852. ]);
  1853. };
  1854. /**
  1855. * Resolves this field's type references.
  1856. * @returns {Field} `this`
  1857. * @throws {Error} If any reference cannot be resolved
  1858. */
  1859. Field.prototype.resolve = function resolve() {
  1860. if (this.resolved)
  1861. return this;
  1862. if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
  1863. this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
  1864. if (this.resolvedType instanceof Type)
  1865. this.typeDefault = null;
  1866. else // instanceof Enum
  1867. this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
  1868. }
  1869. // use explicitly set default value if present
  1870. if (this.options && this.options["default"] != null) {
  1871. this.typeDefault = this.options["default"];
  1872. if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
  1873. this.typeDefault = this.resolvedType.values[this.typeDefault];
  1874. }
  1875. // remove unnecessary options
  1876. if (this.options) {
  1877. if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
  1878. delete this.options.packed;
  1879. if (!Object.keys(this.options).length)
  1880. this.options = undefined;
  1881. }
  1882. // convert to internal data type if necesssary
  1883. if (this.long) {
  1884. this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
  1885. /* istanbul ignore else */
  1886. if (Object.freeze)
  1887. Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
  1888. } else if (this.bytes && typeof this.typeDefault === "string") {
  1889. var buf;
  1890. if (util.base64.test(this.typeDefault))
  1891. util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
  1892. else
  1893. util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
  1894. this.typeDefault = buf;
  1895. }
  1896. // take special care of maps and repeated fields
  1897. if (this.map)
  1898. this.defaultValue = util.emptyObject;
  1899. else if (this.repeated)
  1900. this.defaultValue = util.emptyArray;
  1901. else
  1902. this.defaultValue = this.typeDefault;
  1903. // ensure proper value on prototype
  1904. if (this.parent instanceof Type)
  1905. this.parent.ctor.prototype[this.name] = this.defaultValue;
  1906. return ReflectionObject.prototype.resolve.call(this);
  1907. };
  1908. Field.prototype.useToArray = function useToArray() {
  1909. return !!this.getOption("(js_use_toArray)");
  1910. };
  1911. Field.prototype.preEncoded = function preEncoded() {
  1912. return !!this.getOption("(js_preEncoded)");
  1913. };
  1914. /**
  1915. * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
  1916. * @typedef FieldDecorator
  1917. * @type {function}
  1918. * @param {Object} prototype Target prototype
  1919. * @param {string} fieldName Field name
  1920. * @returns {undefined}
  1921. */
  1922. /**
  1923. * Field decorator (TypeScript).
  1924. * @name Field.d
  1925. * @function
  1926. * @param {number} fieldId Field id
  1927. * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
  1928. * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
  1929. * @param {T} [defaultValue] Default value
  1930. * @returns {FieldDecorator} Decorator function
  1931. * @template T extends number | number[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
  1932. */
  1933. Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
  1934. // submessage: decorate the submessage and use its name as the type
  1935. if (typeof fieldType === "function")
  1936. fieldType = util.decorateType(fieldType).name;
  1937. // enum reference: create a reflected copy of the enum and keep reuseing it
  1938. else if (fieldType && typeof fieldType === "object")
  1939. fieldType = util.decorateEnum(fieldType).name;
  1940. return function fieldDecorator(prototype, fieldName) {
  1941. util.decorateType(prototype.constructor)
  1942. .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
  1943. };
  1944. };
  1945. /**
  1946. * Field decorator (TypeScript).
  1947. * @name Field.d
  1948. * @function
  1949. * @param {number} fieldId Field id
  1950. * @param {Constructor<T>|string} fieldType Field type
  1951. * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
  1952. * @returns {FieldDecorator} Decorator function
  1953. * @template T extends Message<T>
  1954. * @variation 2
  1955. */
  1956. // like Field.d but without a default value
  1957. // Sets up cyclic dependencies (called in index-light)
  1958. Field._configure = function configure(Type_) {
  1959. Type = Type_;
  1960. };
  1961. },{"14":14,"22":22,"32":32,"33":33}],16:[function(require,module,exports){
  1962. "use strict";
  1963. var protobuf = module.exports = require(17);
  1964. protobuf.build = "light";
  1965. /**
  1966. * A node-style callback as used by {@link load} and {@link Root#load}.
  1967. * @typedef LoadCallback
  1968. * @type {function}
  1969. * @param {Error|null} error Error, if any, otherwise `null`
  1970. * @param {Root} [root] Root, if there hasn't been an error
  1971. * @returns {undefined}
  1972. */
  1973. /**
  1974. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
  1975. * @param {string|string[]} filename One or multiple files to load
  1976. * @param {Root} root Root namespace, defaults to create a new one if omitted.
  1977. * @param {LoadCallback} callback Callback function
  1978. * @returns {undefined}
  1979. * @see {@link Root#load}
  1980. */
  1981. function load(filename, root, callback) {
  1982. if (typeof root === "function") {
  1983. callback = root;
  1984. root = new protobuf.Root();
  1985. } else if (!root)
  1986. root = new protobuf.Root();
  1987. return root.load(filename, callback);
  1988. }
  1989. /**
  1990. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
  1991. * @name load
  1992. * @function
  1993. * @param {string|string[]} filename One or multiple files to load
  1994. * @param {LoadCallback} callback Callback function
  1995. * @returns {undefined}
  1996. * @see {@link Root#load}
  1997. * @variation 2
  1998. */
  1999. // function load(filename:string, callback:LoadCallback):undefined
  2000. /**
  2001. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and returns a promise.
  2002. * @name load
  2003. * @function
  2004. * @param {string|string[]} filename One or multiple files to load
  2005. * @param {Root} [root] Root namespace, defaults to create a new one if omitted.
  2006. * @returns {Promise<Root>} Promise
  2007. * @see {@link Root#load}
  2008. * @variation 3
  2009. */
  2010. // function load(filename:string, [root:Root]):Promise<Root>
  2011. protobuf.load = load;
  2012. /**
  2013. * Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace (node only).
  2014. * @param {string|string[]} filename One or multiple files to load
  2015. * @param {Root} [root] Root namespace, defaults to create a new one if omitted.
  2016. * @returns {Root} Root namespace
  2017. * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
  2018. * @see {@link Root#loadSync}
  2019. */
  2020. function loadSync(filename, root) {
  2021. if (!root)
  2022. root = new protobuf.Root();
  2023. return root.loadSync(filename);
  2024. }
  2025. protobuf.loadSync = loadSync;
  2026. // Serialization
  2027. protobuf.encoder = require(13);
  2028. protobuf.decoder = require(12);
  2029. protobuf.verifier = require(36);
  2030. protobuf.converter = require(11);
  2031. // Reflection
  2032. protobuf.ReflectionObject = require(22);
  2033. protobuf.Namespace = require(21);
  2034. protobuf.Root = require(26);
  2035. protobuf.Enum = require(14);
  2036. protobuf.Type = require(31);
  2037. protobuf.Field = require(15);
  2038. protobuf.OneOf = require(23);
  2039. protobuf.MapField = require(18);
  2040. protobuf.Service = require(30);
  2041. protobuf.Method = require(20);
  2042. // Runtime
  2043. protobuf.Message = require(19);
  2044. protobuf.wrappers = require(37);
  2045. // Utility
  2046. protobuf.types = require(32);
  2047. protobuf.util = require(33);
  2048. // Set up possibly cyclic reflection dependencies
  2049. protobuf.ReflectionObject._configure(protobuf.Root);
  2050. protobuf.Namespace._configure(protobuf.Type, protobuf.Service, protobuf.Enum);
  2051. protobuf.Root._configure(protobuf.Type);
  2052. protobuf.Field._configure(protobuf.Type);
  2053. },{"11":11,"12":12,"13":13,"14":14,"15":15,"17":17,"18":18,"19":19,"20":20,"21":21,"22":22,"23":23,"26":26,"30":30,"31":31,"32":32,"33":33,"36":36,"37":37}],17:[function(require,module,exports){
  2054. "use strict";
  2055. var protobuf = exports;
  2056. /**
  2057. * Build type, one of `"full"`, `"light"` or `"minimal"`.
  2058. * @name build
  2059. * @type {string}
  2060. * @const
  2061. */
  2062. protobuf.build = "minimal";
  2063. // Serialization
  2064. protobuf.Writer = require(38);
  2065. protobuf.BufferWriter = require(39);
  2066. protobuf.Reader = require(24);
  2067. protobuf.BufferReader = require(25);
  2068. // Utility
  2069. protobuf.util = require(35);
  2070. protobuf.rpc = require(28);
  2071. protobuf.roots = require(27);
  2072. protobuf.configure = configure;
  2073. /* istanbul ignore next */
  2074. /**
  2075. * Reconfigures the library according to the environment.
  2076. * @returns {undefined}
  2077. */
  2078. function configure() {
  2079. protobuf.Reader._configure(protobuf.BufferReader);
  2080. protobuf.util._configure();
  2081. }
  2082. // Set up buffer utility according to the environment
  2083. protobuf.Writer._configure(protobuf.BufferWriter);
  2084. configure();
  2085. },{"24":24,"25":25,"27":27,"28":28,"35":35,"38":38,"39":39}],18:[function(require,module,exports){
  2086. "use strict";
  2087. module.exports = MapField;
  2088. // extends Field
  2089. var Field = require(15);
  2090. ((MapField.prototype = Object.create(Field.prototype)).constructor = MapField).className = "MapField";
  2091. var types = require(32),
  2092. util = require(33);
  2093. /**
  2094. * Constructs a new map field instance.
  2095. * @classdesc Reflected map field.
  2096. * @extends FieldBase
  2097. * @constructor
  2098. * @param {string} name Unique name within its namespace
  2099. * @param {number} id Unique id within its namespace
  2100. * @param {string} keyType Key type
  2101. * @param {string} type Value type
  2102. * @param {Object.<string,*>} [options] Declared options
  2103. * @param {string} [comment] Comment associated with this field
  2104. */
  2105. function MapField(name, id, keyType, type, options, comment) {
  2106. Field.call(this, name, id, type, undefined, undefined, options, comment);
  2107. /* istanbul ignore if */
  2108. if (!util.isString(keyType))
  2109. throw TypeError("keyType must be a string");
  2110. /**
  2111. * Key type.
  2112. * @type {string}
  2113. */
  2114. this.keyType = keyType; // toJSON, marker
  2115. /**
  2116. * Resolved key type if not a basic type.
  2117. * @type {ReflectionObject|null}
  2118. */
  2119. this.resolvedKeyType = null;
  2120. // Overrides Field#map
  2121. this.map = true;
  2122. }
  2123. /**
  2124. * Map field descriptor.
  2125. * @interface IMapField
  2126. * @extends {IField}
  2127. * @property {string} keyType Key type
  2128. */
  2129. /**
  2130. * Extension map field descriptor.
  2131. * @interface IExtensionMapField
  2132. * @extends IMapField
  2133. * @property {string} extend Extended type
  2134. */
  2135. /**
  2136. * Constructs a map field from a map field descriptor.
  2137. * @param {string} name Field name
  2138. * @param {IMapField} json Map field descriptor
  2139. * @returns {MapField} Created map field
  2140. * @throws {TypeError} If arguments are invalid
  2141. */
  2142. MapField.fromJSON = function fromJSON(name, json) {
  2143. return new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);
  2144. };
  2145. /**
  2146. * Converts this map field to a map field descriptor.
  2147. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  2148. * @returns {IMapField} Map field descriptor
  2149. */
  2150. MapField.prototype.toJSON = function toJSON(toJSONOptions) {
  2151. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  2152. return util.toObject([
  2153. "keyType" , this.keyType,
  2154. "type" , this.type,
  2155. "id" , this.id,
  2156. "extend" , this.extend,
  2157. "options" , this.options,
  2158. "comment" , keepComments ? this.comment : undefined
  2159. ]);
  2160. };
  2161. /**
  2162. * @override
  2163. */
  2164. MapField.prototype.resolve = function resolve() {
  2165. if (this.resolved)
  2166. return this;
  2167. // Besides a value type, map fields have a key type that may be "any scalar type except for floating point types and bytes"
  2168. if (types.mapKey[this.keyType] === undefined)
  2169. throw Error("invalid key type: " + this.keyType);
  2170. return Field.prototype.resolve.call(this);
  2171. };
  2172. /**
  2173. * Map field decorator (TypeScript).
  2174. * @name MapField.d
  2175. * @function
  2176. * @param {number} fieldId Field id
  2177. * @param {"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"} fieldKeyType Field key type
  2178. * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"|"bytes"|Object|Constructor<{}>} fieldValueType Field value type
  2179. * @returns {FieldDecorator} Decorator function
  2180. * @template T extends { [key: string]: number | string | boolean | Uint8Array | Buffer | number[] | Message<{}> }
  2181. */
  2182. MapField.d = function decorateMapField(fieldId, fieldKeyType, fieldValueType) {
  2183. // submessage value: decorate the submessage and use its name as the type
  2184. if (typeof fieldValueType === "function")
  2185. fieldValueType = util.decorateType(fieldValueType).name;
  2186. // enum reference value: create a reflected copy of the enum and keep reuseing it
  2187. else if (fieldValueType && typeof fieldValueType === "object")
  2188. fieldValueType = util.decorateEnum(fieldValueType).name;
  2189. return function mapFieldDecorator(prototype, fieldName) {
  2190. util.decorateType(prototype.constructor)
  2191. .add(new MapField(fieldName, fieldId, fieldKeyType, fieldValueType));
  2192. };
  2193. };
  2194. },{"15":15,"32":32,"33":33}],19:[function(require,module,exports){
  2195. "use strict";
  2196. module.exports = Message;
  2197. var util = require(35);
  2198. /**
  2199. * Constructs a new message instance.
  2200. * @classdesc Abstract runtime message.
  2201. * @constructor
  2202. * @param {Properties<T>} [properties] Properties to set
  2203. * @template T extends object = object
  2204. */
  2205. function Message(properties) {
  2206. // not used internally
  2207. if (properties)
  2208. for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
  2209. this[keys[i]] = properties[keys[i]];
  2210. }
  2211. /**
  2212. * Reference to the reflected type.
  2213. * @name Message.$type
  2214. * @type {Type}
  2215. * @readonly
  2216. */
  2217. /**
  2218. * Reference to the reflected type.
  2219. * @name Message#$type
  2220. * @type {Type}
  2221. * @readonly
  2222. */
  2223. /*eslint-disable valid-jsdoc*/
  2224. /**
  2225. * Creates a new message of this type using the specified properties.
  2226. * @param {Object.<string,*>} [properties] Properties to set
  2227. * @returns {Message<T>} Message instance
  2228. * @template T extends Message<T>
  2229. * @this Constructor<T>
  2230. */
  2231. Message.create = function create(properties) {
  2232. return this.$type.create(properties);
  2233. };
  2234. /**
  2235. * Encodes a message of this type.
  2236. * @param {T|Object.<string,*>} message Message to encode
  2237. * @param {Writer} [writer] Writer to use
  2238. * @returns {Writer} Writer
  2239. * @template T extends Message<T>
  2240. * @this Constructor<T>
  2241. */
  2242. Message.encode = function encode(message, writer) {
  2243. return this.$type.encode(message, writer);
  2244. };
  2245. /**
  2246. * Encodes a message of this type preceeded by its length as a varint.
  2247. * @param {T|Object.<string,*>} message Message to encode
  2248. * @param {Writer} [writer] Writer to use
  2249. * @returns {Writer} Writer
  2250. * @template T extends Message<T>
  2251. * @this Constructor<T>
  2252. */
  2253. Message.encodeDelimited = function encodeDelimited(message, writer) {
  2254. return this.$type.encodeDelimited(message, writer);
  2255. };
  2256. /**
  2257. * Decodes a message of this type.
  2258. * @name Message.decode
  2259. * @function
  2260. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  2261. * @returns {T} Decoded message
  2262. * @template T extends Message<T>
  2263. * @this Constructor<T>
  2264. */
  2265. Message.decode = function decode(reader) {
  2266. return this.$type.decode(reader);
  2267. };
  2268. /**
  2269. * Decodes a message of this type preceeded by its length as a varint.
  2270. * @name Message.decodeDelimited
  2271. * @function
  2272. * @param {Reader|Uint8Array} reader Reader or buffer to decode
  2273. * @returns {T} Decoded message
  2274. * @template T extends Message<T>
  2275. * @this Constructor<T>
  2276. */
  2277. Message.decodeDelimited = function decodeDelimited(reader) {
  2278. return this.$type.decodeDelimited(reader);
  2279. };
  2280. /**
  2281. * Verifies a message of this type.
  2282. * @name Message.verify
  2283. * @function
  2284. * @param {Object.<string,*>} message Plain object to verify
  2285. * @returns {string|null} `null` if valid, otherwise the reason why it is not
  2286. */
  2287. Message.verify = function verify(message) {
  2288. return this.$type.verify(message);
  2289. };
  2290. /**
  2291. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  2292. * @param {Object.<string,*>} object Plain object
  2293. * @returns {T} Message instance
  2294. * @template T extends Message<T>
  2295. * @this Constructor<T>
  2296. */
  2297. Message.fromObject = function fromObject(object) {
  2298. return this.$type.fromObject(object);
  2299. };
  2300. /**
  2301. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  2302. * @param {T} message Message instance
  2303. * @param {IConversionOptions} [options] Conversion options
  2304. * @returns {Object.<string,*>} Plain object
  2305. * @template T extends Message<T>
  2306. * @this Constructor<T>
  2307. */
  2308. Message.toObject = function toObject(message, options) {
  2309. return this.$type.toObject(message, options);
  2310. };
  2311. /**
  2312. * Converts this message to JSON.
  2313. * @returns {Object.<string,*>} JSON object
  2314. */
  2315. Message.prototype.toJSON = function toJSON() {
  2316. return this.$type.toObject(this, util.toJSONOptions);
  2317. };
  2318. /*eslint-enable valid-jsdoc*/
  2319. },{"35":35}],20:[function(require,module,exports){
  2320. "use strict";
  2321. module.exports = Method;
  2322. // extends ReflectionObject
  2323. var ReflectionObject = require(22);
  2324. ((Method.prototype = Object.create(ReflectionObject.prototype)).constructor = Method).className = "Method";
  2325. var util = require(33);
  2326. /**
  2327. * Constructs a new service method instance.
  2328. * @classdesc Reflected service method.
  2329. * @extends ReflectionObject
  2330. * @constructor
  2331. * @param {string} name Method name
  2332. * @param {string|undefined} type Method type, usually `"rpc"`
  2333. * @param {string} requestType Request message type
  2334. * @param {string} responseType Response message type
  2335. * @param {boolean|Object.<string,*>} [requestStream] Whether the request is streamed
  2336. * @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
  2337. * @param {Object.<string,*>} [options] Declared options
  2338. * @param {string} [comment] The comment for this method
  2339. */
  2340. function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment) {
  2341. /* istanbul ignore next */
  2342. if (util.isObject(requestStream)) {
  2343. options = requestStream;
  2344. requestStream = responseStream = undefined;
  2345. } else if (util.isObject(responseStream)) {
  2346. options = responseStream;
  2347. responseStream = undefined;
  2348. }
  2349. /* istanbul ignore if */
  2350. if (!(type === undefined || util.isString(type)))
  2351. throw TypeError("type must be a string");
  2352. /* istanbul ignore if */
  2353. if (!util.isString(requestType))
  2354. throw TypeError("requestType must be a string");
  2355. /* istanbul ignore if */
  2356. if (!util.isString(responseType))
  2357. throw TypeError("responseType must be a string");
  2358. ReflectionObject.call(this, name, options);
  2359. /**
  2360. * Method type.
  2361. * @type {string}
  2362. */
  2363. this.type = type || "rpc"; // toJSON
  2364. /**
  2365. * Request type.
  2366. * @type {string}
  2367. */
  2368. this.requestType = requestType; // toJSON, marker
  2369. /**
  2370. * Whether requests are streamed or not.
  2371. * @type {boolean|undefined}
  2372. */
  2373. this.requestStream = requestStream ? true : undefined; // toJSON
  2374. /**
  2375. * Response type.
  2376. * @type {string}
  2377. */
  2378. this.responseType = responseType; // toJSON
  2379. /**
  2380. * Whether responses are streamed or not.
  2381. * @type {boolean|undefined}
  2382. */
  2383. this.responseStream = responseStream ? true : undefined; // toJSON
  2384. /**
  2385. * Resolved request type.
  2386. * @type {Type|null}
  2387. */
  2388. this.resolvedRequestType = null;
  2389. /**
  2390. * Resolved response type.
  2391. * @type {Type|null}
  2392. */
  2393. this.resolvedResponseType = null;
  2394. /**
  2395. * Comment for this method
  2396. * @type {string|null}
  2397. */
  2398. this.comment = comment;
  2399. }
  2400. /**
  2401. * Method descriptor.
  2402. * @interface IMethod
  2403. * @property {string} [type="rpc"] Method type
  2404. * @property {string} requestType Request type
  2405. * @property {string} responseType Response type
  2406. * @property {boolean} [requestStream=false] Whether requests are streamed
  2407. * @property {boolean} [responseStream=false] Whether responses are streamed
  2408. * @property {Object.<string,*>} [options] Method options
  2409. */
  2410. /**
  2411. * Constructs a method from a method descriptor.
  2412. * @param {string} name Method name
  2413. * @param {IMethod} json Method descriptor
  2414. * @returns {Method} Created method
  2415. * @throws {TypeError} If arguments are invalid
  2416. */
  2417. Method.fromJSON = function fromJSON(name, json) {
  2418. return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment);
  2419. };
  2420. /**
  2421. * Converts this method to a method descriptor.
  2422. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  2423. * @returns {IMethod} Method descriptor
  2424. */
  2425. Method.prototype.toJSON = function toJSON(toJSONOptions) {
  2426. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  2427. return util.toObject([
  2428. "type" , this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
  2429. "requestType" , this.requestType,
  2430. "requestStream" , this.requestStream,
  2431. "responseType" , this.responseType,
  2432. "responseStream" , this.responseStream,
  2433. "options" , this.options,
  2434. "comment" , keepComments ? this.comment : undefined
  2435. ]);
  2436. };
  2437. /**
  2438. * @override
  2439. */
  2440. Method.prototype.resolve = function resolve() {
  2441. /* istanbul ignore if */
  2442. if (this.resolved)
  2443. return this;
  2444. this.resolvedRequestType = this.parent.lookupType(this.requestType);
  2445. this.resolvedResponseType = this.parent.lookupType(this.responseType);
  2446. return ReflectionObject.prototype.resolve.call(this);
  2447. };
  2448. },{"22":22,"33":33}],21:[function(require,module,exports){
  2449. "use strict";
  2450. module.exports = Namespace;
  2451. // extends ReflectionObject
  2452. var ReflectionObject = require(22);
  2453. ((Namespace.prototype = Object.create(ReflectionObject.prototype)).constructor = Namespace).className = "Namespace";
  2454. var Field = require(15),
  2455. util = require(33);
  2456. var Type, // cyclic
  2457. Service,
  2458. Enum;
  2459. /**
  2460. * Constructs a new namespace instance.
  2461. * @name Namespace
  2462. * @classdesc Reflected namespace.
  2463. * @extends NamespaceBase
  2464. * @constructor
  2465. * @param {string} name Namespace name
  2466. * @param {Object.<string,*>} [options] Declared options
  2467. */
  2468. /**
  2469. * Constructs a namespace from JSON.
  2470. * @memberof Namespace
  2471. * @function
  2472. * @param {string} name Namespace name
  2473. * @param {Object.<string,*>} json JSON object
  2474. * @returns {Namespace} Created namespace
  2475. * @throws {TypeError} If arguments are invalid
  2476. */
  2477. Namespace.fromJSON = function fromJSON(name, json) {
  2478. return new Namespace(name, json.options).addJSON(json.nested);
  2479. };
  2480. /**
  2481. * Converts an array of reflection objects to JSON.
  2482. * @memberof Namespace
  2483. * @param {ReflectionObject[]} array Object array
  2484. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  2485. * @returns {Object.<string,*>|undefined} JSON object or `undefined` when array is empty
  2486. */
  2487. function arrayToJSON(array, toJSONOptions) {
  2488. if (!(array && array.length))
  2489. return undefined;
  2490. var obj = {};
  2491. for (var i = 0; i < array.length; ++i)
  2492. obj[array[i].name] = array[i].toJSON(toJSONOptions);
  2493. return obj;
  2494. }
  2495. Namespace.arrayToJSON = arrayToJSON;
  2496. /**
  2497. * Tests if the specified id is reserved.
  2498. * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
  2499. * @param {number} id Id to test
  2500. * @returns {boolean} `true` if reserved, otherwise `false`
  2501. */
  2502. Namespace.isReservedId = function isReservedId(reserved, id) {
  2503. if (reserved)
  2504. for (var i = 0; i < reserved.length; ++i)
  2505. if (typeof reserved[i] !== "string" && reserved[i][0] <= id && reserved[i][1] > id)
  2506. return true;
  2507. return false;
  2508. };
  2509. /**
  2510. * Tests if the specified name is reserved.
  2511. * @param {Array.<number[]|string>|undefined} reserved Array of reserved ranges and names
  2512. * @param {string} name Name to test
  2513. * @returns {boolean} `true` if reserved, otherwise `false`
  2514. */
  2515. Namespace.isReservedName = function isReservedName(reserved, name) {
  2516. if (reserved)
  2517. for (var i = 0; i < reserved.length; ++i)
  2518. if (reserved[i] === name)
  2519. return true;
  2520. return false;
  2521. };
  2522. /**
  2523. * Not an actual constructor. Use {@link Namespace} instead.
  2524. * @classdesc Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions.
  2525. * @exports NamespaceBase
  2526. * @extends ReflectionObject
  2527. * @abstract
  2528. * @constructor
  2529. * @param {string} name Namespace name
  2530. * @param {Object.<string,*>} [options] Declared options
  2531. * @see {@link Namespace}
  2532. */
  2533. function Namespace(name, options) {
  2534. ReflectionObject.call(this, name, options);
  2535. /**
  2536. * Nested objects by name.
  2537. * @type {Object.<string,ReflectionObject>|undefined}
  2538. */
  2539. this.nested = undefined; // toJSON
  2540. /**
  2541. * Cached nested objects as an array.
  2542. * @type {ReflectionObject[]|null}
  2543. * @private
  2544. */
  2545. this._nestedArray = null;
  2546. }
  2547. function clearCache(namespace) {
  2548. namespace._nestedArray = null;
  2549. return namespace;
  2550. }
  2551. /**
  2552. * Nested objects of this namespace as an array for iteration.
  2553. * @name NamespaceBase#nestedArray
  2554. * @type {ReflectionObject[]}
  2555. * @readonly
  2556. */
  2557. Object.defineProperty(Namespace.prototype, "nestedArray", {
  2558. get: function() {
  2559. return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
  2560. }
  2561. });
  2562. /**
  2563. * Namespace descriptor.
  2564. * @interface INamespace
  2565. * @property {Object.<string,*>} [options] Namespace options
  2566. * @property {Object.<string,AnyNestedObject>} [nested] Nested object descriptors
  2567. */
  2568. /**
  2569. * Any extension field descriptor.
  2570. * @typedef AnyExtensionField
  2571. * @type {IExtensionField|IExtensionMapField}
  2572. */
  2573. /**
  2574. * Any nested object descriptor.
  2575. * @typedef AnyNestedObject
  2576. * @type {IEnum|IType|IService|AnyExtensionField|INamespace}
  2577. */
  2578. // ^ BEWARE: VSCode hangs forever when using more than 5 types (that's why AnyExtensionField exists in the first place)
  2579. /**
  2580. * Converts this namespace to a namespace descriptor.
  2581. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  2582. * @returns {INamespace} Namespace descriptor
  2583. */
  2584. Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
  2585. return util.toObject([
  2586. "options" , this.options,
  2587. "nested" , arrayToJSON(this.nestedArray, toJSONOptions)
  2588. ]);
  2589. };
  2590. /**
  2591. * Adds nested objects to this namespace from nested object descriptors.
  2592. * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
  2593. * @returns {Namespace} `this`
  2594. */
  2595. Namespace.prototype.addJSON = function addJSON(nestedJson) {
  2596. var ns = this;
  2597. /* istanbul ignore else */
  2598. if (nestedJson) {
  2599. for (var names = Object.keys(nestedJson), i = 0, nested; i < names.length; ++i) {
  2600. nested = nestedJson[names[i]];
  2601. ns.add( // most to least likely
  2602. ( nested.fields !== undefined
  2603. ? Type.fromJSON
  2604. : nested.values !== undefined
  2605. ? Enum.fromJSON
  2606. : nested.methods !== undefined
  2607. ? Service.fromJSON
  2608. : nested.id !== undefined
  2609. ? Field.fromJSON
  2610. : Namespace.fromJSON )(names[i], nested)
  2611. );
  2612. }
  2613. }
  2614. return this;
  2615. };
  2616. /**
  2617. * Gets the nested object of the specified name.
  2618. * @param {string} name Nested object name
  2619. * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
  2620. */
  2621. Namespace.prototype.get = function get(name) {
  2622. return this.nested && this.nested[name]
  2623. || null;
  2624. };
  2625. /**
  2626. * Gets the values of the nested {@link Enum|enum} of the specified name.
  2627. * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
  2628. * @param {string} name Nested enum name
  2629. * @returns {Object.<string,number>} Enum values
  2630. * @throws {Error} If there is no such enum
  2631. */
  2632. Namespace.prototype.getEnum = function getEnum(name) {
  2633. if (this.nested && this.nested[name] instanceof Enum)
  2634. return this.nested[name].values;
  2635. throw Error("no such enum: " + name);
  2636. };
  2637. /**
  2638. * Adds a nested object to this namespace.
  2639. * @param {ReflectionObject} object Nested object to add
  2640. * @returns {Namespace} `this`
  2641. * @throws {TypeError} If arguments are invalid
  2642. * @throws {Error} If there is already a nested object with this name
  2643. */
  2644. Namespace.prototype.add = function add(object) {
  2645. if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof Enum || object instanceof Service || object instanceof Namespace))
  2646. throw TypeError("object must be a valid nested object");
  2647. if (!this.nested)
  2648. this.nested = {};
  2649. else {
  2650. var prev = this.get(object.name);
  2651. if (prev) {
  2652. if (prev instanceof Namespace && object instanceof Namespace && !(prev instanceof Type || prev instanceof Service)) {
  2653. // replace plain namespace but keep existing nested elements and options
  2654. var nested = prev.nestedArray;
  2655. for (var i = 0; i < nested.length; ++i)
  2656. object.add(nested[i]);
  2657. this.remove(prev);
  2658. if (!this.nested)
  2659. this.nested = {};
  2660. object.setOptions(prev.options, true);
  2661. } else
  2662. throw Error("duplicate name '" + object.name + "' in " + this);
  2663. }
  2664. }
  2665. this.nested[object.name] = object;
  2666. object.onAdd(this);
  2667. return clearCache(this);
  2668. };
  2669. /**
  2670. * Removes a nested object from this namespace.
  2671. * @param {ReflectionObject} object Nested object to remove
  2672. * @returns {Namespace} `this`
  2673. * @throws {TypeError} If arguments are invalid
  2674. * @throws {Error} If `object` is not a member of this namespace
  2675. */
  2676. Namespace.prototype.remove = function remove(object) {
  2677. if (!(object instanceof ReflectionObject))
  2678. throw TypeError("object must be a ReflectionObject");
  2679. if (object.parent !== this)
  2680. throw Error(object + " is not a member of " + this);
  2681. delete this.nested[object.name];
  2682. if (!Object.keys(this.nested).length)
  2683. this.nested = undefined;
  2684. object.onRemove(this);
  2685. return clearCache(this);
  2686. };
  2687. /**
  2688. * Defines additial namespaces within this one if not yet existing.
  2689. * @param {string|string[]} path Path to create
  2690. * @param {*} [json] Nested types to create from JSON
  2691. * @returns {Namespace} Pointer to the last namespace created or `this` if path is empty
  2692. */
  2693. Namespace.prototype.define = function define(path, json) {
  2694. if (util.isString(path))
  2695. path = path.split(".");
  2696. else if (!Array.isArray(path))
  2697. throw TypeError("illegal path");
  2698. if (path && path.length && path[0] === "")
  2699. throw Error("path must be relative");
  2700. var ptr = this;
  2701. while (path.length > 0) {
  2702. var part = path.shift();
  2703. if (ptr.nested && ptr.nested[part]) {
  2704. ptr = ptr.nested[part];
  2705. if (!(ptr instanceof Namespace))
  2706. throw Error("path conflicts with non-namespace objects");
  2707. } else
  2708. ptr.add(ptr = new Namespace(part));
  2709. }
  2710. if (json)
  2711. ptr.addJSON(json);
  2712. return ptr;
  2713. };
  2714. /**
  2715. * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
  2716. * @returns {Namespace} `this`
  2717. */
  2718. Namespace.prototype.resolveAll = function resolveAll() {
  2719. var nested = this.nestedArray, i = 0;
  2720. while (i < nested.length)
  2721. if (nested[i] instanceof Namespace)
  2722. nested[i++].resolveAll();
  2723. else
  2724. nested[i++].resolve();
  2725. return this.resolve();
  2726. };
  2727. /**
  2728. * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
  2729. * @param {string|string[]} path Path to look up
  2730. * @param {*|Array.<*>} filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
  2731. * @param {boolean} [parentAlreadyChecked=false] If known, whether the parent has already been checked
  2732. * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
  2733. */
  2734. Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChecked) {
  2735. /* istanbul ignore next */
  2736. if (typeof filterTypes === "boolean") {
  2737. parentAlreadyChecked = filterTypes;
  2738. filterTypes = undefined;
  2739. } else if (filterTypes && !Array.isArray(filterTypes))
  2740. filterTypes = [ filterTypes ];
  2741. if (util.isString(path) && path.length) {
  2742. if (path === ".")
  2743. return this.root;
  2744. path = path.split(".");
  2745. } else if (!path.length)
  2746. return this;
  2747. // Start at root if path is absolute
  2748. if (path[0] === "")
  2749. return this.root.lookup(path.slice(1), filterTypes);
  2750. // Test if the first part matches any nested object, and if so, traverse if path contains more
  2751. var found = this.get(path[0]);
  2752. if (found) {
  2753. if (path.length === 1) {
  2754. if (!filterTypes || filterTypes.indexOf(found.constructor) > -1)
  2755. return found;
  2756. } else if (found instanceof Namespace && (found = found.lookup(path.slice(1), filterTypes, true)))
  2757. return found;
  2758. // Otherwise try each nested namespace
  2759. } else
  2760. for (var i = 0; i < this.nestedArray.length; ++i)
  2761. if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i].lookup(path, filterTypes, true)))
  2762. return found;
  2763. // If there hasn't been a match, try again at the parent
  2764. if (this.parent === null || parentAlreadyChecked)
  2765. return null;
  2766. return this.parent.lookup(path, filterTypes);
  2767. };
  2768. /**
  2769. * Looks up the reflection object at the specified path, relative to this namespace.
  2770. * @name NamespaceBase#lookup
  2771. * @function
  2772. * @param {string|string[]} path Path to look up
  2773. * @param {boolean} [parentAlreadyChecked=false] Whether the parent has already been checked
  2774. * @returns {ReflectionObject|null} Looked up object or `null` if none could be found
  2775. * @variation 2
  2776. */
  2777. // lookup(path: string, [parentAlreadyChecked: boolean])
  2778. /**
  2779. * Looks up the {@link Type|type} at the specified path, relative to this namespace.
  2780. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  2781. * @param {string|string[]} path Path to look up
  2782. * @returns {Type} Looked up type
  2783. * @throws {Error} If `path` does not point to a type
  2784. */
  2785. Namespace.prototype.lookupType = function lookupType(path) {
  2786. var found = this.lookup(path, [ Type ]);
  2787. if (!found)
  2788. throw Error("no such type: " + path);
  2789. return found;
  2790. };
  2791. /**
  2792. * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
  2793. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  2794. * @param {string|string[]} path Path to look up
  2795. * @returns {Enum} Looked up enum
  2796. * @throws {Error} If `path` does not point to an enum
  2797. */
  2798. Namespace.prototype.lookupEnum = function lookupEnum(path) {
  2799. var found = this.lookup(path, [ Enum ]);
  2800. if (!found)
  2801. throw Error("no such Enum '" + path + "' in " + this);
  2802. return found;
  2803. };
  2804. /**
  2805. * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
  2806. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  2807. * @param {string|string[]} path Path to look up
  2808. * @returns {Type} Looked up type or enum
  2809. * @throws {Error} If `path` does not point to a type or enum
  2810. */
  2811. Namespace.prototype.lookupTypeOrEnum = function lookupTypeOrEnum(path) {
  2812. var found = this.lookup(path, [ Type, Enum ]);
  2813. if (!found)
  2814. throw Error("no such Type or Enum '" + path + "' in " + this);
  2815. return found;
  2816. };
  2817. /**
  2818. * Looks up the {@link Service|service} at the specified path, relative to this namespace.
  2819. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  2820. * @param {string|string[]} path Path to look up
  2821. * @returns {Service} Looked up service
  2822. * @throws {Error} If `path` does not point to a service
  2823. */
  2824. Namespace.prototype.lookupService = function lookupService(path) {
  2825. var found = this.lookup(path, [ Service ]);
  2826. if (!found)
  2827. throw Error("no such Service '" + path + "' in " + this);
  2828. return found;
  2829. };
  2830. // Sets up cyclic dependencies (called in index-light)
  2831. Namespace._configure = function(Type_, Service_, Enum_) {
  2832. Type = Type_;
  2833. Service = Service_;
  2834. Enum = Enum_;
  2835. };
  2836. },{"15":15,"22":22,"33":33}],22:[function(require,module,exports){
  2837. "use strict";
  2838. module.exports = ReflectionObject;
  2839. ReflectionObject.className = "ReflectionObject";
  2840. var util = require(33);
  2841. var Root; // cyclic
  2842. /**
  2843. * Constructs a new reflection object instance.
  2844. * @classdesc Base class of all reflection objects.
  2845. * @constructor
  2846. * @param {string} name Object name
  2847. * @param {Object.<string,*>} [options] Declared options
  2848. * @abstract
  2849. */
  2850. function ReflectionObject(name, options) {
  2851. if (!util.isString(name))
  2852. throw TypeError("name must be a string");
  2853. if (options && !util.isObject(options))
  2854. throw TypeError("options must be an object");
  2855. /**
  2856. * Options.
  2857. * @type {Object.<string,*>|undefined}
  2858. */
  2859. this.options = options; // toJSON
  2860. /**
  2861. * Unique name within its namespace.
  2862. * @type {string}
  2863. */
  2864. this.name = name;
  2865. /**
  2866. * Parent namespace.
  2867. * @type {Namespace|null}
  2868. */
  2869. this.parent = null;
  2870. /**
  2871. * Whether already resolved or not.
  2872. * @type {boolean}
  2873. */
  2874. this.resolved = false;
  2875. /**
  2876. * Comment text, if any.
  2877. * @type {string|null}
  2878. */
  2879. this.comment = null;
  2880. /**
  2881. * Defining file name.
  2882. * @type {string|null}
  2883. */
  2884. this.filename = null;
  2885. }
  2886. Object.defineProperties(ReflectionObject.prototype, {
  2887. /**
  2888. * Reference to the root namespace.
  2889. * @name ReflectionObject#root
  2890. * @type {Root}
  2891. * @readonly
  2892. */
  2893. root: {
  2894. get: function() {
  2895. var ptr = this;
  2896. while (ptr.parent !== null)
  2897. ptr = ptr.parent;
  2898. return ptr;
  2899. }
  2900. },
  2901. /**
  2902. * Full name including leading dot.
  2903. * @name ReflectionObject#fullName
  2904. * @type {string}
  2905. * @readonly
  2906. */
  2907. fullName: {
  2908. get: function() {
  2909. var path = [ this.name ],
  2910. ptr = this.parent;
  2911. while (ptr) {
  2912. path.unshift(ptr.name);
  2913. ptr = ptr.parent;
  2914. }
  2915. return path.join(".");
  2916. }
  2917. }
  2918. });
  2919. /**
  2920. * Converts this reflection object to its descriptor representation.
  2921. * @returns {Object.<string,*>} Descriptor
  2922. * @abstract
  2923. */
  2924. ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
  2925. throw Error(); // not implemented, shouldn't happen
  2926. };
  2927. /**
  2928. * Called when this object is added to a parent.
  2929. * @param {ReflectionObject} parent Parent added to
  2930. * @returns {undefined}
  2931. */
  2932. ReflectionObject.prototype.onAdd = function onAdd(parent) {
  2933. if (this.parent && this.parent !== parent)
  2934. this.parent.remove(this);
  2935. this.parent = parent;
  2936. this.resolved = false;
  2937. var root = parent.root;
  2938. if (root instanceof Root)
  2939. root._handleAdd(this);
  2940. };
  2941. /**
  2942. * Called when this object is removed from a parent.
  2943. * @param {ReflectionObject} parent Parent removed from
  2944. * @returns {undefined}
  2945. */
  2946. ReflectionObject.prototype.onRemove = function onRemove(parent) {
  2947. var root = parent.root;
  2948. if (root instanceof Root)
  2949. root._handleRemove(this);
  2950. this.parent = null;
  2951. this.resolved = false;
  2952. };
  2953. /**
  2954. * Resolves this objects type references.
  2955. * @returns {ReflectionObject} `this`
  2956. */
  2957. ReflectionObject.prototype.resolve = function resolve() {
  2958. if (this.resolved)
  2959. return this;
  2960. if (this.root instanceof Root)
  2961. this.resolved = true; // only if part of a root
  2962. return this;
  2963. };
  2964. /**
  2965. * Gets an option value.
  2966. * @param {string} name Option name
  2967. * @returns {*} Option value or `undefined` if not set
  2968. */
  2969. ReflectionObject.prototype.getOption = function getOption(name) {
  2970. if (this.options)
  2971. return this.options[name];
  2972. return undefined;
  2973. };
  2974. /**
  2975. * Sets an option.
  2976. * @param {string} name Option name
  2977. * @param {*} value Option value
  2978. * @param {boolean} [ifNotSet] Sets the option only if it isn't currently set
  2979. * @returns {ReflectionObject} `this`
  2980. */
  2981. ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
  2982. if (!ifNotSet || !this.options || this.options[name] === undefined)
  2983. (this.options || (this.options = {}))[name] = value;
  2984. return this;
  2985. };
  2986. /**
  2987. * Sets multiple options.
  2988. * @param {Object.<string,*>} options Options to set
  2989. * @param {boolean} [ifNotSet] Sets an option only if it isn't currently set
  2990. * @returns {ReflectionObject} `this`
  2991. */
  2992. ReflectionObject.prototype.setOptions = function setOptions(options, ifNotSet) {
  2993. if (options)
  2994. for (var keys = Object.keys(options), i = 0; i < keys.length; ++i)
  2995. this.setOption(keys[i], options[keys[i]], ifNotSet);
  2996. return this;
  2997. };
  2998. /**
  2999. * Converts this instance to its string representation.
  3000. * @returns {string} Class name[, space, full name]
  3001. */
  3002. ReflectionObject.prototype.toString = function toString() {
  3003. var className = this.constructor.className,
  3004. fullName = this.fullName;
  3005. if (fullName.length)
  3006. return className + " " + fullName;
  3007. return className;
  3008. };
  3009. // Sets up cyclic dependencies (called in index-light)
  3010. ReflectionObject._configure = function(Root_) {
  3011. Root = Root_;
  3012. };
  3013. },{"33":33}],23:[function(require,module,exports){
  3014. "use strict";
  3015. module.exports = OneOf;
  3016. // extends ReflectionObject
  3017. var ReflectionObject = require(22);
  3018. ((OneOf.prototype = Object.create(ReflectionObject.prototype)).constructor = OneOf).className = "OneOf";
  3019. var Field = require(15),
  3020. util = require(33);
  3021. /**
  3022. * Constructs a new oneof instance.
  3023. * @classdesc Reflected oneof.
  3024. * @extends ReflectionObject
  3025. * @constructor
  3026. * @param {string} name Oneof name
  3027. * @param {string[]|Object.<string,*>} [fieldNames] Field names
  3028. * @param {Object.<string,*>} [options] Declared options
  3029. * @param {string} [comment] Comment associated with this field
  3030. */
  3031. function OneOf(name, fieldNames, options, comment) {
  3032. if (!Array.isArray(fieldNames)) {
  3033. options = fieldNames;
  3034. fieldNames = undefined;
  3035. }
  3036. ReflectionObject.call(this, name, options);
  3037. /* istanbul ignore if */
  3038. if (!(fieldNames === undefined || Array.isArray(fieldNames)))
  3039. throw TypeError("fieldNames must be an Array");
  3040. /**
  3041. * Field names that belong to this oneof.
  3042. * @type {string[]}
  3043. */
  3044. this.oneof = fieldNames || []; // toJSON, marker
  3045. /**
  3046. * Fields that belong to this oneof as an array for iteration.
  3047. * @type {Field[]}
  3048. * @readonly
  3049. */
  3050. this.fieldsArray = []; // declared readonly for conformance, possibly not yet added to parent
  3051. /**
  3052. * Comment for this field.
  3053. * @type {string|null}
  3054. */
  3055. this.comment = comment;
  3056. }
  3057. /**
  3058. * Oneof descriptor.
  3059. * @interface IOneOf
  3060. * @property {Array.<string>} oneof Oneof field names
  3061. * @property {Object.<string,*>} [options] Oneof options
  3062. */
  3063. /**
  3064. * Constructs a oneof from a oneof descriptor.
  3065. * @param {string} name Oneof name
  3066. * @param {IOneOf} json Oneof descriptor
  3067. * @returns {OneOf} Created oneof
  3068. * @throws {TypeError} If arguments are invalid
  3069. */
  3070. OneOf.fromJSON = function fromJSON(name, json) {
  3071. return new OneOf(name, json.oneof, json.options, json.comment);
  3072. };
  3073. /**
  3074. * Converts this oneof to a oneof descriptor.
  3075. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  3076. * @returns {IOneOf} Oneof descriptor
  3077. */
  3078. OneOf.prototype.toJSON = function toJSON(toJSONOptions) {
  3079. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  3080. return util.toObject([
  3081. "options" , this.options,
  3082. "oneof" , this.oneof,
  3083. "comment" , keepComments ? this.comment : undefined
  3084. ]);
  3085. };
  3086. /**
  3087. * Adds the fields of the specified oneof to the parent if not already done so.
  3088. * @param {OneOf} oneof The oneof
  3089. * @returns {undefined}
  3090. * @inner
  3091. * @ignore
  3092. */
  3093. function addFieldsToParent(oneof) {
  3094. if (oneof.parent)
  3095. for (var i = 0; i < oneof.fieldsArray.length; ++i)
  3096. if (!oneof.fieldsArray[i].parent)
  3097. oneof.parent.add(oneof.fieldsArray[i]);
  3098. }
  3099. /**
  3100. * Adds a field to this oneof and removes it from its current parent, if any.
  3101. * @param {Field} field Field to add
  3102. * @returns {OneOf} `this`
  3103. */
  3104. OneOf.prototype.add = function add(field) {
  3105. /* istanbul ignore if */
  3106. if (!(field instanceof Field))
  3107. throw TypeError("field must be a Field");
  3108. if (field.parent && field.parent !== this.parent)
  3109. field.parent.remove(field);
  3110. this.oneof.push(field.name);
  3111. this.fieldsArray.push(field);
  3112. field.partOf = this; // field.parent remains null
  3113. addFieldsToParent(this);
  3114. return this;
  3115. };
  3116. /**
  3117. * Removes a field from this oneof and puts it back to the oneof's parent.
  3118. * @param {Field} field Field to remove
  3119. * @returns {OneOf} `this`
  3120. */
  3121. OneOf.prototype.remove = function remove(field) {
  3122. /* istanbul ignore if */
  3123. if (!(field instanceof Field))
  3124. throw TypeError("field must be a Field");
  3125. var index = this.fieldsArray.indexOf(field);
  3126. /* istanbul ignore if */
  3127. if (index < 0)
  3128. throw Error(field + " is not a member of " + this);
  3129. this.fieldsArray.splice(index, 1);
  3130. index = this.oneof.indexOf(field.name);
  3131. /* istanbul ignore else */
  3132. if (index > -1) // theoretical
  3133. this.oneof.splice(index, 1);
  3134. field.partOf = null;
  3135. return this;
  3136. };
  3137. /**
  3138. * @override
  3139. */
  3140. OneOf.prototype.onAdd = function onAdd(parent) {
  3141. ReflectionObject.prototype.onAdd.call(this, parent);
  3142. var self = this;
  3143. // Collect present fields
  3144. for (var i = 0; i < this.oneof.length; ++i) {
  3145. var field = parent.get(this.oneof[i]);
  3146. if (field && !field.partOf) {
  3147. field.partOf = self;
  3148. self.fieldsArray.push(field);
  3149. }
  3150. }
  3151. // Add not yet present fields
  3152. addFieldsToParent(this);
  3153. };
  3154. /**
  3155. * @override
  3156. */
  3157. OneOf.prototype.onRemove = function onRemove(parent) {
  3158. for (var i = 0, field; i < this.fieldsArray.length; ++i)
  3159. if ((field = this.fieldsArray[i]).parent)
  3160. field.parent.remove(field);
  3161. ReflectionObject.prototype.onRemove.call(this, parent);
  3162. };
  3163. /**
  3164. * Decorator function as returned by {@link OneOf.d} (TypeScript).
  3165. * @typedef OneOfDecorator
  3166. * @type {function}
  3167. * @param {Object} prototype Target prototype
  3168. * @param {string} oneofName OneOf name
  3169. * @returns {undefined}
  3170. */
  3171. /**
  3172. * OneOf decorator (TypeScript).
  3173. * @function
  3174. * @param {...string} fieldNames Field names
  3175. * @returns {OneOfDecorator} Decorator function
  3176. * @template T extends string
  3177. */
  3178. OneOf.d = function decorateOneOf() {
  3179. var fieldNames = new Array(arguments.length),
  3180. index = 0;
  3181. while (index < arguments.length)
  3182. fieldNames[index] = arguments[index++];
  3183. return function oneOfDecorator(prototype, oneofName) {
  3184. util.decorateType(prototype.constructor)
  3185. .add(new OneOf(oneofName, fieldNames));
  3186. Object.defineProperty(prototype, oneofName, {
  3187. get: util.oneOfGetter(fieldNames),
  3188. set: util.oneOfSetter(fieldNames)
  3189. });
  3190. };
  3191. };
  3192. },{"15":15,"22":22,"33":33}],24:[function(require,module,exports){
  3193. "use strict";
  3194. module.exports = Reader;
  3195. var util = require(35);
  3196. var BufferReader; // cyclic
  3197. var LongBits = util.LongBits,
  3198. utf8 = util.utf8;
  3199. /* istanbul ignore next */
  3200. function indexOutOfRange(reader, writeLength) {
  3201. return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
  3202. }
  3203. /**
  3204. * Constructs a new reader instance using the specified buffer.
  3205. * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
  3206. * @constructor
  3207. * @param {Uint8Array} buffer Buffer to read from
  3208. */
  3209. function Reader(buffer) {
  3210. /**
  3211. * Read buffer.
  3212. * @type {Uint8Array}
  3213. */
  3214. this.buf = buffer;
  3215. /**
  3216. * Read buffer position.
  3217. * @type {number}
  3218. */
  3219. this.pos = 0;
  3220. /**
  3221. * Read buffer length.
  3222. * @type {number}
  3223. */
  3224. this.len = buffer.length;
  3225. }
  3226. var create_array = typeof Uint8Array !== "undefined"
  3227. ? function create_typed_array(buffer) {
  3228. if (buffer instanceof Uint8Array || Array.isArray(buffer))
  3229. return new Reader(buffer);
  3230. throw Error("illegal buffer");
  3231. }
  3232. /* istanbul ignore next */
  3233. : function create_array(buffer) {
  3234. if (Array.isArray(buffer))
  3235. return new Reader(buffer);
  3236. throw Error("illegal buffer");
  3237. };
  3238. /**
  3239. * Creates a new reader using the specified buffer.
  3240. * @function
  3241. * @param {Uint8Array|Buffer} buffer Buffer to read from
  3242. * @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
  3243. * @throws {Error} If `buffer` is not a valid buffer
  3244. */
  3245. Reader.create = util.Buffer
  3246. ? function create_buffer_setup(buffer) {
  3247. return (Reader.create = function create_buffer(buffer) {
  3248. return util.Buffer.isBuffer(buffer)
  3249. ? new BufferReader(buffer)
  3250. /* istanbul ignore next */
  3251. : create_array(buffer);
  3252. })(buffer);
  3253. }
  3254. /* istanbul ignore next */
  3255. : create_array;
  3256. Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
  3257. /**
  3258. * Reads a varint as an unsigned 32 bit value.
  3259. * @function
  3260. * @returns {number} Value read
  3261. */
  3262. Reader.prototype.uint32 = (function read_uint32_setup() {
  3263. var value = 4294967295; // optimizer type-hint, tends to deopt otherwise (?!)
  3264. return function read_uint32() {
  3265. value = ( this.buf[this.pos] & 127 ) >>> 0; if (this.buf[this.pos++] < 128) return value;
  3266. value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; if (this.buf[this.pos++] < 128) return value;
  3267. value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; if (this.buf[this.pos++] < 128) return value;
  3268. value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; if (this.buf[this.pos++] < 128) return value;
  3269. value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; if (this.buf[this.pos++] < 128) return value;
  3270. /* istanbul ignore if */
  3271. if ((this.pos += 5) > this.len) {
  3272. this.pos = this.len;
  3273. throw indexOutOfRange(this, 10);
  3274. }
  3275. return value;
  3276. };
  3277. })();
  3278. /**
  3279. * Reads a varint as a signed 32 bit value.
  3280. * @returns {number} Value read
  3281. */
  3282. Reader.prototype.int32 = function read_int32() {
  3283. return this.uint32() | 0;
  3284. };
  3285. /**
  3286. * Reads a zig-zag encoded varint as a signed 32 bit value.
  3287. * @returns {number} Value read
  3288. */
  3289. Reader.prototype.sint32 = function read_sint32() {
  3290. var value = this.uint32();
  3291. return value >>> 1 ^ -(value & 1) | 0;
  3292. };
  3293. /* eslint-disable no-invalid-this */
  3294. function readLongVarint() {
  3295. // tends to deopt with local vars for octet etc.
  3296. var bits = new LongBits(0, 0);
  3297. var i = 0;
  3298. if (this.len - this.pos > 4) { // fast route (lo)
  3299. for (; i < 4; ++i) {
  3300. // 1st..4th
  3301. bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
  3302. if (this.buf[this.pos++] < 128)
  3303. return bits;
  3304. }
  3305. // 5th
  3306. bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
  3307. bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
  3308. if (this.buf[this.pos++] < 128)
  3309. return bits;
  3310. i = 0;
  3311. } else {
  3312. for (; i < 3; ++i) {
  3313. /* istanbul ignore if */
  3314. if (this.pos >= this.len)
  3315. throw indexOutOfRange(this);
  3316. // 1st..3th
  3317. bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
  3318. if (this.buf[this.pos++] < 128)
  3319. return bits;
  3320. }
  3321. // 4th
  3322. bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
  3323. return bits;
  3324. }
  3325. if (this.len - this.pos > 4) { // fast route (hi)
  3326. for (; i < 5; ++i) {
  3327. // 6th..10th
  3328. bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
  3329. if (this.buf[this.pos++] < 128)
  3330. return bits;
  3331. }
  3332. } else {
  3333. for (; i < 5; ++i) {
  3334. /* istanbul ignore if */
  3335. if (this.pos >= this.len)
  3336. throw indexOutOfRange(this);
  3337. // 6th..10th
  3338. bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
  3339. if (this.buf[this.pos++] < 128)
  3340. return bits;
  3341. }
  3342. }
  3343. /* istanbul ignore next */
  3344. throw Error("invalid varint encoding");
  3345. }
  3346. /* eslint-enable no-invalid-this */
  3347. /*
  3348. * Reads a varint as a signed 64 bit value.
  3349. * @name Reader#int64
  3350. * @function
  3351. * @returns {Long} Value read
  3352. */
  3353. /*
  3354. * Reads a varint as an unsigned 64 bit value.
  3355. * @name Reader#uint64
  3356. * @function
  3357. * @returns {Long} Value read
  3358. */
  3359. /*
  3360. * Reads a zig-zag encoded varint as a signed 64 bit value.
  3361. * @name Reader#sint64
  3362. * @function
  3363. * @returns {Long} Value read
  3364. */
  3365. /**
  3366. * Reads a varint as a boolean.
  3367. * @returns {boolean} Value read
  3368. */
  3369. Reader.prototype.bool = function read_bool() {
  3370. return this.uint32() !== 0;
  3371. };
  3372. function readFixed32_end(buf, end) { // note that this uses `end`, not `pos`
  3373. return (buf[end - 4]
  3374. | buf[end - 3] << 8
  3375. | buf[end - 2] << 16
  3376. | buf[end - 1] << 24) >>> 0;
  3377. }
  3378. /**
  3379. * Reads fixed 32 bits as an unsigned 32 bit integer.
  3380. * @returns {number} Value read
  3381. */
  3382. Reader.prototype.fixed32 = function read_fixed32() {
  3383. /* istanbul ignore if */
  3384. if (this.pos + 4 > this.len)
  3385. throw indexOutOfRange(this, 4);
  3386. return readFixed32_end(this.buf, this.pos += 4);
  3387. };
  3388. /**
  3389. * Reads fixed 32 bits as a signed 32 bit integer.
  3390. * @returns {number} Value read
  3391. */
  3392. Reader.prototype.sfixed32 = function read_sfixed32() {
  3393. /* istanbul ignore if */
  3394. if (this.pos + 4 > this.len)
  3395. throw indexOutOfRange(this, 4);
  3396. return readFixed32_end(this.buf, this.pos += 4) | 0;
  3397. };
  3398. /* eslint-disable no-invalid-this */
  3399. function readFixed64(/* this: Reader */) {
  3400. /* istanbul ignore if */
  3401. if (this.pos + 8 > this.len)
  3402. throw indexOutOfRange(this, 8);
  3403. return new LongBits(readFixed32_end(this.buf, this.pos += 4), readFixed32_end(this.buf, this.pos += 4));
  3404. }
  3405. /* eslint-enable no-invalid-this */
  3406. /*
  3407. * Reads fixed 64 bits.
  3408. * @name Reader#fixed64
  3409. * @function
  3410. * @returns {Long} Value read
  3411. */
  3412. /*
  3413. * Reads zig-zag encoded fixed 64 bits.
  3414. * @name Reader#sfixed64
  3415. * @function
  3416. * @returns {Long} Value read
  3417. */
  3418. /**
  3419. * Reads a float (32 bit) as a number.
  3420. * @function
  3421. * @returns {number} Value read
  3422. */
  3423. Reader.prototype.float = function read_float() {
  3424. /* istanbul ignore if */
  3425. if (this.pos + 4 > this.len)
  3426. throw indexOutOfRange(this, 4);
  3427. var value = util.float.readFloatLE(this.buf, this.pos);
  3428. this.pos += 4;
  3429. return value;
  3430. };
  3431. /**
  3432. * Reads a double (64 bit float) as a number.
  3433. * @function
  3434. * @returns {number} Value read
  3435. */
  3436. Reader.prototype.double = function read_double() {
  3437. /* istanbul ignore if */
  3438. if (this.pos + 8 > this.len)
  3439. throw indexOutOfRange(this, 4);
  3440. var value = util.float.readDoubleLE(this.buf, this.pos);
  3441. this.pos += 8;
  3442. return value;
  3443. };
  3444. /**
  3445. * Reads a sequence of bytes preceeded by its length as a varint.
  3446. * @returns {Uint8Array} Value read
  3447. */
  3448. Reader.prototype.bytes = function read_bytes() {
  3449. var length = this.uint32(),
  3450. start = this.pos,
  3451. end = this.pos + length;
  3452. /* istanbul ignore if */
  3453. if (end > this.len)
  3454. throw indexOutOfRange(this, length);
  3455. this.pos += length;
  3456. if (Array.isArray(this.buf)) // plain array
  3457. return this.buf.slice(start, end);
  3458. return start === end // fix for IE 10/Win8 and others' subarray returning array of size 1
  3459. ? new this.buf.constructor(0)
  3460. : this._slice.call(this.buf, start, end);
  3461. };
  3462. /**
  3463. * Reads a string preceeded by its byte length as a varint.
  3464. * @returns {string} Value read
  3465. */
  3466. Reader.prototype.string = function read_string() {
  3467. var bytes = this.bytes();
  3468. return utf8.read(bytes, 0, bytes.length);
  3469. };
  3470. /**
  3471. * Skips the specified number of bytes if specified, otherwise skips a varint.
  3472. * @param {number} [length] Length if known, otherwise a varint is assumed
  3473. * @returns {Reader} `this`
  3474. */
  3475. Reader.prototype.skip = function skip(length) {
  3476. if (typeof length === "number") {
  3477. /* istanbul ignore if */
  3478. if (this.pos + length > this.len)
  3479. throw indexOutOfRange(this, length);
  3480. this.pos += length;
  3481. } else {
  3482. do {
  3483. /* istanbul ignore if */
  3484. if (this.pos >= this.len)
  3485. throw indexOutOfRange(this);
  3486. } while (this.buf[this.pos++] & 128);
  3487. }
  3488. return this;
  3489. };
  3490. /**
  3491. * Skips the next element of the specified wire type.
  3492. * @param {number} wireType Wire type received
  3493. * @returns {Reader} `this`
  3494. */
  3495. Reader.prototype.skipType = function(wireType) {
  3496. switch (wireType) {
  3497. case 0:
  3498. this.skip();
  3499. break;
  3500. case 1:
  3501. this.skip(8);
  3502. break;
  3503. case 2:
  3504. this.skip(this.uint32());
  3505. break;
  3506. case 3:
  3507. while ((wireType = this.uint32() & 7) !== 4) {
  3508. this.skipType(wireType);
  3509. }
  3510. break;
  3511. case 5:
  3512. this.skip(4);
  3513. break;
  3514. /* istanbul ignore next */
  3515. default:
  3516. throw Error("invalid wire type " + wireType + " at offset " + this.pos);
  3517. }
  3518. return this;
  3519. };
  3520. Reader._configure = function(BufferReader_) {
  3521. BufferReader = BufferReader_;
  3522. var fn = util.Long ? "toLong" : /* istanbul ignore next */ "toNumber";
  3523. util.merge(Reader.prototype, {
  3524. int64: function read_int64() {
  3525. return readLongVarint.call(this)[fn](false);
  3526. },
  3527. uint64: function read_uint64() {
  3528. return readLongVarint.call(this)[fn](true);
  3529. },
  3530. sint64: function read_sint64() {
  3531. return readLongVarint.call(this).zzDecode()[fn](false);
  3532. },
  3533. fixed64: function read_fixed64() {
  3534. return readFixed64.call(this)[fn](true);
  3535. },
  3536. sfixed64: function read_sfixed64() {
  3537. return readFixed64.call(this)[fn](false);
  3538. }
  3539. });
  3540. };
  3541. },{"35":35}],25:[function(require,module,exports){
  3542. "use strict";
  3543. module.exports = BufferReader;
  3544. // extends Reader
  3545. var Reader = require(24);
  3546. (BufferReader.prototype = Object.create(Reader.prototype)).constructor = BufferReader;
  3547. var util = require(35);
  3548. /**
  3549. * Constructs a new buffer reader instance.
  3550. * @classdesc Wire format reader using node buffers.
  3551. * @extends Reader
  3552. * @constructor
  3553. * @param {Buffer} buffer Buffer to read from
  3554. */
  3555. function BufferReader(buffer) {
  3556. Reader.call(this, buffer);
  3557. /**
  3558. * Read buffer.
  3559. * @name BufferReader#buf
  3560. * @type {Buffer}
  3561. */
  3562. }
  3563. /* istanbul ignore else */
  3564. if (util.Buffer)
  3565. BufferReader.prototype._slice = util.Buffer.prototype.slice;
  3566. /**
  3567. * @override
  3568. */
  3569. BufferReader.prototype.string = function read_string_buffer() {
  3570. var len = this.uint32(); // modifies pos
  3571. return this.buf.utf8Slice(this.pos, this.pos = Math.min(this.pos + len, this.len));
  3572. };
  3573. /**
  3574. * Reads a sequence of bytes preceeded by its length as a varint.
  3575. * @name BufferReader#bytes
  3576. * @function
  3577. * @returns {Buffer} Value read
  3578. */
  3579. },{"24":24,"35":35}],26:[function(require,module,exports){
  3580. "use strict";
  3581. module.exports = Root;
  3582. // extends Namespace
  3583. var Namespace = require(21);
  3584. ((Root.prototype = Object.create(Namespace.prototype)).constructor = Root).className = "Root";
  3585. var Field = require(15),
  3586. Enum = require(14),
  3587. OneOf = require(23),
  3588. util = require(33);
  3589. var Type, // cyclic
  3590. parse, // might be excluded
  3591. common; // "
  3592. /**
  3593. * Constructs a new root namespace instance.
  3594. * @classdesc Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together.
  3595. * @extends NamespaceBase
  3596. * @constructor
  3597. * @param {Object.<string,*>} [options] Top level options
  3598. */
  3599. function Root(options) {
  3600. Namespace.call(this, "", options);
  3601. /**
  3602. * Deferred extension fields.
  3603. * @type {Field[]}
  3604. */
  3605. this.deferred = [];
  3606. /**
  3607. * Resolved file names of loaded files.
  3608. * @type {string[]}
  3609. */
  3610. this.files = [];
  3611. }
  3612. /**
  3613. * Loads a namespace descriptor into a root namespace.
  3614. * @param {INamespace} json Nameespace descriptor
  3615. * @param {Root} [root] Root namespace, defaults to create a new one if omitted
  3616. * @returns {Root} Root namespace
  3617. */
  3618. Root.fromJSON = function fromJSON(json, root) {
  3619. if (!root)
  3620. root = new Root();
  3621. if (json.options)
  3622. root.setOptions(json.options);
  3623. return root.addJSON(json.nested);
  3624. };
  3625. /**
  3626. * Resolves the path of an imported file, relative to the importing origin.
  3627. * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
  3628. * @function
  3629. * @param {string} origin The file name of the importing file
  3630. * @param {string} target The file name being imported
  3631. * @returns {string|null} Resolved path to `target` or `null` to skip the file
  3632. */
  3633. Root.prototype.resolvePath = util.path.resolve;
  3634. // A symbol-like function to safely signal synchronous loading
  3635. /* istanbul ignore next */
  3636. function SYNC() {} // eslint-disable-line no-empty-function
  3637. /**
  3638. * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
  3639. * @param {string|string[]} filename Names of one or multiple files to load
  3640. * @param {IParseOptions} options Parse options
  3641. * @param {LoadCallback} callback Callback function
  3642. * @returns {undefined}
  3643. */
  3644. Root.prototype.load = function load(filename, options, callback) {
  3645. if (typeof options === "function") {
  3646. callback = options;
  3647. options = undefined;
  3648. }
  3649. var self = this;
  3650. if (!callback)
  3651. return util.asPromise(load, self, filename, options);
  3652. var sync = callback === SYNC; // undocumented
  3653. // Finishes loading by calling the callback (exactly once)
  3654. function finish(err, root) {
  3655. /* istanbul ignore if */
  3656. if (!callback)
  3657. return;
  3658. var cb = callback;
  3659. callback = null;
  3660. if (sync)
  3661. throw err;
  3662. cb(err, root);
  3663. }
  3664. // Bundled definition existence checking
  3665. function getBundledFileName(filename) {
  3666. var idx = filename.lastIndexOf("google/protobuf/");
  3667. if (idx > -1) {
  3668. var altname = filename.substring(idx);
  3669. if (altname in common) return altname;
  3670. }
  3671. return null;
  3672. }
  3673. // Processes a single file
  3674. function process(filename, source) {
  3675. try {
  3676. if (util.isString(source) && source.charAt(0) === "{")
  3677. source = JSON.parse(source);
  3678. if (!util.isString(source))
  3679. self.setOptions(source.options).addJSON(source.nested);
  3680. else {
  3681. parse.filename = filename;
  3682. var parsed = parse(source, self, options),
  3683. resolved,
  3684. i = 0;
  3685. if (parsed.imports)
  3686. for (; i < parsed.imports.length; ++i)
  3687. if (resolved = (getBundledFileName(parsed.imports[i]) || self.resolvePath(filename, parsed.imports[i])))
  3688. fetch(resolved);
  3689. if (parsed.weakImports)
  3690. for (i = 0; i < parsed.weakImports.length; ++i)
  3691. if (resolved = (getBundledFileName(parsed.weakImports[i]) || self.resolvePath(filename, parsed.weakImports[i])))
  3692. fetch(resolved, true);
  3693. }
  3694. } catch (err) {
  3695. finish(err);
  3696. }
  3697. if (!sync && !queued)
  3698. finish(null, self); // only once anyway
  3699. }
  3700. // Fetches a single file
  3701. function fetch(filename, weak) {
  3702. // Skip if already loaded / attempted
  3703. if (self.files.indexOf(filename) > -1)
  3704. return;
  3705. self.files.push(filename);
  3706. // Shortcut bundled definitions
  3707. if (filename in common) {
  3708. if (sync)
  3709. process(filename, common[filename]);
  3710. else {
  3711. ++queued;
  3712. setTimeout(function() {
  3713. --queued;
  3714. process(filename, common[filename]);
  3715. });
  3716. }
  3717. return;
  3718. }
  3719. // Otherwise fetch from disk or network
  3720. if (sync) {
  3721. var source;
  3722. try {
  3723. source = util.fs.readFileSync(filename).toString("utf8");
  3724. } catch (err) {
  3725. if (!weak)
  3726. finish(err);
  3727. return;
  3728. }
  3729. process(filename, source);
  3730. } else {
  3731. ++queued;
  3732. util.fetch(filename, function(err, source) {
  3733. --queued;
  3734. /* istanbul ignore if */
  3735. if (!callback)
  3736. return; // terminated meanwhile
  3737. if (err) {
  3738. /* istanbul ignore else */
  3739. if (!weak)
  3740. finish(err);
  3741. else if (!queued) // can't be covered reliably
  3742. finish(null, self);
  3743. return;
  3744. }
  3745. process(filename, source);
  3746. });
  3747. }
  3748. }
  3749. var queued = 0;
  3750. // Assembling the root namespace doesn't require working type
  3751. // references anymore, so we can load everything in parallel
  3752. if (util.isString(filename))
  3753. filename = [ filename ];
  3754. for (var i = 0, resolved; i < filename.length; ++i)
  3755. if (resolved = self.resolvePath("", filename[i]))
  3756. fetch(resolved);
  3757. if (sync)
  3758. return self;
  3759. if (!queued)
  3760. finish(null, self);
  3761. return undefined;
  3762. };
  3763. // function load(filename:string, options:IParseOptions, callback:LoadCallback):undefined
  3764. /**
  3765. * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
  3766. * @function Root#load
  3767. * @param {string|string[]} filename Names of one or multiple files to load
  3768. * @param {LoadCallback} callback Callback function
  3769. * @returns {undefined}
  3770. * @variation 2
  3771. */
  3772. // function load(filename:string, callback:LoadCallback):undefined
  3773. /**
  3774. * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
  3775. * @function Root#load
  3776. * @param {string|string[]} filename Names of one or multiple files to load
  3777. * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  3778. * @returns {Promise<Root>} Promise
  3779. * @variation 3
  3780. */
  3781. // function load(filename:string, [options:IParseOptions]):Promise<Root>
  3782. /**
  3783. * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
  3784. * @function Root#loadSync
  3785. * @param {string|string[]} filename Names of one or multiple files to load
  3786. * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  3787. * @returns {Root} Root namespace
  3788. * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
  3789. */
  3790. Root.prototype.loadSync = function loadSync(filename, options) {
  3791. if (!util.isNode)
  3792. throw Error("not supported");
  3793. return this.load(filename, options, SYNC);
  3794. };
  3795. /**
  3796. * @override
  3797. */
  3798. Root.prototype.resolveAll = function resolveAll() {
  3799. if (this.deferred.length)
  3800. throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
  3801. return "'extend " + field.extend + "' in " + field.parent.fullName;
  3802. }).join(", "));
  3803. return Namespace.prototype.resolveAll.call(this);
  3804. };
  3805. // only uppercased (and thus conflict-free) children are exposed, see below
  3806. var exposeRe = /^[A-Z]/;
  3807. /**
  3808. * Handles a deferred declaring extension field by creating a sister field to represent it within its extended type.
  3809. * @param {Root} root Root instance
  3810. * @param {Field} field Declaring extension field witin the declaring type
  3811. * @returns {boolean} `true` if successfully added to the extended type, `false` otherwise
  3812. * @inner
  3813. * @ignore
  3814. */
  3815. function tryHandleExtension(root, field) {
  3816. var extendedType = field.parent.lookup(field.extend);
  3817. if (extendedType) {
  3818. var sisterField = new Field(field.fullName, field.id, field.type, field.rule, undefined, field.options);
  3819. sisterField.declaringField = field;
  3820. field.extensionField = sisterField;
  3821. extendedType.add(sisterField);
  3822. return true;
  3823. }
  3824. return false;
  3825. }
  3826. /**
  3827. * Called when any object is added to this root or its sub-namespaces.
  3828. * @param {ReflectionObject} object Object added
  3829. * @returns {undefined}
  3830. * @private
  3831. */
  3832. Root.prototype._handleAdd = function _handleAdd(object) {
  3833. if (object instanceof Field) {
  3834. if (/* an extension field (implies not part of a oneof) */ object.extend !== undefined && /* not already handled */ !object.extensionField)
  3835. if (!tryHandleExtension(this, object))
  3836. this.deferred.push(object);
  3837. } else if (object instanceof Enum) {
  3838. if (exposeRe.test(object.name))
  3839. object.parent[object.name] = object.values; // expose enum values as property of its parent
  3840. } else if (!(object instanceof OneOf)) /* everything else is a namespace */ {
  3841. if (object instanceof Type) // Try to handle any deferred extensions
  3842. for (var i = 0; i < this.deferred.length;)
  3843. if (tryHandleExtension(this, this.deferred[i]))
  3844. this.deferred.splice(i, 1);
  3845. else
  3846. ++i;
  3847. for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
  3848. this._handleAdd(object._nestedArray[j]);
  3849. if (exposeRe.test(object.name))
  3850. object.parent[object.name] = object; // expose namespace as property of its parent
  3851. }
  3852. // The above also adds uppercased (and thus conflict-free) nested types, services and enums as
  3853. // properties of namespaces just like static code does. This allows using a .d.ts generated for
  3854. // a static module with reflection-based solutions where the condition is met.
  3855. };
  3856. /**
  3857. * Called when any object is removed from this root or its sub-namespaces.
  3858. * @param {ReflectionObject} object Object removed
  3859. * @returns {undefined}
  3860. * @private
  3861. */
  3862. Root.prototype._handleRemove = function _handleRemove(object) {
  3863. if (object instanceof Field) {
  3864. if (/* an extension field */ object.extend !== undefined) {
  3865. if (/* already handled */ object.extensionField) { // remove its sister field
  3866. object.extensionField.parent.remove(object.extensionField);
  3867. object.extensionField = null;
  3868. } else { // cancel the extension
  3869. var index = this.deferred.indexOf(object);
  3870. /* istanbul ignore else */
  3871. if (index > -1)
  3872. this.deferred.splice(index, 1);
  3873. }
  3874. }
  3875. } else if (object instanceof Enum) {
  3876. if (exposeRe.test(object.name))
  3877. delete object.parent[object.name]; // unexpose enum values
  3878. } else if (object instanceof Namespace) {
  3879. for (var i = 0; i < /* initializes */ object.nestedArray.length; ++i) // recurse into the namespace
  3880. this._handleRemove(object._nestedArray[i]);
  3881. if (exposeRe.test(object.name))
  3882. delete object.parent[object.name]; // unexpose namespaces
  3883. }
  3884. };
  3885. // Sets up cyclic dependencies (called in index-light)
  3886. Root._configure = function(Type_, parse_, common_) {
  3887. Type = Type_;
  3888. parse = parse_;
  3889. common = common_;
  3890. };
  3891. },{"14":14,"15":15,"21":21,"23":23,"33":33}],27:[function(require,module,exports){
  3892. "use strict";
  3893. module.exports = {};
  3894. /**
  3895. * Named roots.
  3896. * This is where pbjs stores generated structures (the option `-r, --root` specifies a name).
  3897. * Can also be used manually to make roots available accross modules.
  3898. * @name roots
  3899. * @type {Object.<string,Root>}
  3900. * @example
  3901. * // pbjs -r myroot -o compiled.js ...
  3902. *
  3903. * // in another module:
  3904. * require("./compiled.js");
  3905. *
  3906. * // in any subsequent module:
  3907. * var root = protobuf.roots["myroot"];
  3908. */
  3909. },{}],28:[function(require,module,exports){
  3910. "use strict";
  3911. /**
  3912. * Streaming RPC helpers.
  3913. * @namespace
  3914. */
  3915. var rpc = exports;
  3916. /**
  3917. * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
  3918. * @typedef RPCImpl
  3919. * @type {function}
  3920. * @param {Method|rpc.ServiceMethod<Message<{}>,Message<{}>>} method Reflected or static method being called
  3921. * @param {Uint8Array} requestData Request data
  3922. * @param {RPCImplCallback} callback Callback function
  3923. * @returns {undefined}
  3924. * @example
  3925. * function rpcImpl(method, requestData, callback) {
  3926. * if (protobuf.util.lcFirst(method.name) !== "myMethod") // compatible with static code
  3927. * throw Error("no such method");
  3928. * asynchronouslyObtainAResponse(requestData, function(err, responseData) {
  3929. * callback(err, responseData);
  3930. * });
  3931. * }
  3932. */
  3933. /**
  3934. * Node-style callback as used by {@link RPCImpl}.
  3935. * @typedef RPCImplCallback
  3936. * @type {function}
  3937. * @param {Error|null} error Error, if any, otherwise `null`
  3938. * @param {Uint8Array|null} [response] Response data or `null` to signal end of stream, if there hasn't been an error
  3939. * @returns {undefined}
  3940. */
  3941. rpc.Service = require(29);
  3942. },{"29":29}],29:[function(require,module,exports){
  3943. "use strict";
  3944. module.exports = Service;
  3945. var util = require(35);
  3946. // Extends EventEmitter
  3947. (Service.prototype = Object.create(util.EventEmitter.prototype)).constructor = Service;
  3948. /**
  3949. * A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}.
  3950. *
  3951. * Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`.
  3952. * @typedef rpc.ServiceMethodCallback
  3953. * @template TRes extends Message<TRes>
  3954. * @type {function}
  3955. * @param {Error|null} error Error, if any
  3956. * @param {TRes} [response] Response message
  3957. * @returns {undefined}
  3958. */
  3959. /**
  3960. * A service method part of a {@link rpc.Service} as created by {@link Service.create}.
  3961. * @typedef rpc.ServiceMethod
  3962. * @template TReq extends Message<TReq>
  3963. * @template TRes extends Message<TRes>
  3964. * @type {function}
  3965. * @param {TReq|Properties<TReq>} request Request message or plain object
  3966. * @param {rpc.ServiceMethodCallback<TRes>} [callback] Node-style callback called with the error, if any, and the response message
  3967. * @returns {Promise<Message<TRes>>} Promise if `callback` has been omitted, otherwise `undefined`
  3968. */
  3969. /**
  3970. * Constructs a new RPC service instance.
  3971. * @classdesc An RPC service as returned by {@link Service#create}.
  3972. * @exports rpc.Service
  3973. * @extends util.EventEmitter
  3974. * @constructor
  3975. * @param {RPCImpl} rpcImpl RPC implementation
  3976. * @param {boolean} [requestDelimited=false] Whether requests are length-delimited
  3977. * @param {boolean} [responseDelimited=false] Whether responses are length-delimited
  3978. */
  3979. function Service(rpcImpl, requestDelimited, responseDelimited) {
  3980. if (typeof rpcImpl !== "function")
  3981. throw TypeError("rpcImpl must be a function");
  3982. util.EventEmitter.call(this);
  3983. /**
  3984. * RPC implementation. Becomes `null` once the service is ended.
  3985. * @type {RPCImpl|null}
  3986. */
  3987. this.rpcImpl = rpcImpl;
  3988. /**
  3989. * Whether requests are length-delimited.
  3990. * @type {boolean}
  3991. */
  3992. this.requestDelimited = Boolean(requestDelimited);
  3993. /**
  3994. * Whether responses are length-delimited.
  3995. * @type {boolean}
  3996. */
  3997. this.responseDelimited = Boolean(responseDelimited);
  3998. }
  3999. /**
  4000. * Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}.
  4001. * @param {Method|rpc.ServiceMethod<TReq,TRes>} method Reflected or static method
  4002. * @param {Constructor<TReq>} requestCtor Request constructor
  4003. * @param {Constructor<TRes>} responseCtor Response constructor
  4004. * @param {TReq|Properties<TReq>} request Request message or plain object
  4005. * @param {rpc.ServiceMethodCallback<TRes>} callback Service callback
  4006. * @returns {undefined}
  4007. * @template TReq extends Message<TReq>
  4008. * @template TRes extends Message<TRes>
  4009. */
  4010. Service.prototype.rpcCall = function rpcCall(method, requestCtor, responseCtor, request, callback) {
  4011. if (!request)
  4012. throw TypeError("request must be specified");
  4013. var self = this;
  4014. if (!callback)
  4015. return util.asPromise(rpcCall, self, method, requestCtor, responseCtor, request);
  4016. if (!self.rpcImpl) {
  4017. setTimeout(function() { callback(Error("already ended")); }, 0);
  4018. return undefined;
  4019. }
  4020. try {
  4021. return self.rpcImpl(
  4022. method,
  4023. requestCtor[self.requestDelimited ? "encodeDelimited" : "encode"](request).finish(),
  4024. function rpcCallback(err, response) {
  4025. if (err) {
  4026. self.emit("error", err, method);
  4027. return callback(err);
  4028. }
  4029. if (response === null) {
  4030. self.end(/* endedByRPC */ true);
  4031. return undefined;
  4032. }
  4033. if (!(response instanceof responseCtor)) {
  4034. try {
  4035. response = responseCtor[self.responseDelimited ? "decodeDelimited" : "decode"](response);
  4036. } catch (err) {
  4037. self.emit("error", err, method);
  4038. return callback(err);
  4039. }
  4040. }
  4041. self.emit("data", response, method);
  4042. return callback(null, response);
  4043. }
  4044. );
  4045. } catch (err) {
  4046. self.emit("error", err, method);
  4047. setTimeout(function() { callback(err); }, 0);
  4048. return undefined;
  4049. }
  4050. };
  4051. /**
  4052. * Ends this service and emits the `end` event.
  4053. * @param {boolean} [endedByRPC=false] Whether the service has been ended by the RPC implementation.
  4054. * @returns {rpc.Service} `this`
  4055. */
  4056. Service.prototype.end = function end(endedByRPC) {
  4057. if (this.rpcImpl) {
  4058. if (!endedByRPC) // signal end to rpcImpl
  4059. this.rpcImpl(null, null, null);
  4060. this.rpcImpl = null;
  4061. this.emit("end").off();
  4062. }
  4063. return this;
  4064. };
  4065. },{"35":35}],30:[function(require,module,exports){
  4066. "use strict";
  4067. module.exports = Service;
  4068. // extends Namespace
  4069. var Namespace = require(21);
  4070. ((Service.prototype = Object.create(Namespace.prototype)).constructor = Service).className = "Service";
  4071. var Method = require(20),
  4072. util = require(33),
  4073. rpc = require(28);
  4074. /**
  4075. * Constructs a new service instance.
  4076. * @classdesc Reflected service.
  4077. * @extends NamespaceBase
  4078. * @constructor
  4079. * @param {string} name Service name
  4080. * @param {Object.<string,*>} [options] Service options
  4081. * @throws {TypeError} If arguments are invalid
  4082. */
  4083. function Service(name, options) {
  4084. Namespace.call(this, name, options);
  4085. /**
  4086. * Service methods.
  4087. * @type {Object.<string,Method>}
  4088. */
  4089. this.methods = {}; // toJSON, marker
  4090. /**
  4091. * Cached methods as an array.
  4092. * @type {Method[]|null}
  4093. * @private
  4094. */
  4095. this._methodsArray = null;
  4096. }
  4097. /**
  4098. * Service descriptor.
  4099. * @interface IService
  4100. * @extends INamespace
  4101. * @property {Object.<string,IMethod>} methods Method descriptors
  4102. */
  4103. /**
  4104. * Constructs a service from a service descriptor.
  4105. * @param {string} name Service name
  4106. * @param {IService} json Service descriptor
  4107. * @returns {Service} Created service
  4108. * @throws {TypeError} If arguments are invalid
  4109. */
  4110. Service.fromJSON = function fromJSON(name, json) {
  4111. var service = new Service(name, json.options);
  4112. /* istanbul ignore else */
  4113. if (json.methods)
  4114. for (var names = Object.keys(json.methods), i = 0; i < names.length; ++i)
  4115. service.add(Method.fromJSON(names[i], json.methods[names[i]]));
  4116. if (json.nested)
  4117. service.addJSON(json.nested);
  4118. service.comment = json.comment;
  4119. return service;
  4120. };
  4121. /**
  4122. * Converts this service to a service descriptor.
  4123. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  4124. * @returns {IService} Service descriptor
  4125. */
  4126. Service.prototype.toJSON = function toJSON(toJSONOptions) {
  4127. var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
  4128. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  4129. return util.toObject([
  4130. "options" , inherited && inherited.options || undefined,
  4131. "methods" , Namespace.arrayToJSON(this.methodsArray, toJSONOptions) || /* istanbul ignore next */ {},
  4132. "nested" , inherited && inherited.nested || undefined,
  4133. "comment" , keepComments ? this.comment : undefined
  4134. ]);
  4135. };
  4136. /**
  4137. * Methods of this service as an array for iteration.
  4138. * @name Service#methodsArray
  4139. * @type {Method[]}
  4140. * @readonly
  4141. */
  4142. Object.defineProperty(Service.prototype, "methodsArray", {
  4143. get: function() {
  4144. return this._methodsArray || (this._methodsArray = util.toArray(this.methods));
  4145. }
  4146. });
  4147. function clearCache(service) {
  4148. service._methodsArray = null;
  4149. return service;
  4150. }
  4151. /**
  4152. * @override
  4153. */
  4154. Service.prototype.get = function get(name) {
  4155. return this.methods[name]
  4156. || Namespace.prototype.get.call(this, name);
  4157. };
  4158. /**
  4159. * @override
  4160. */
  4161. Service.prototype.resolveAll = function resolveAll() {
  4162. var methods = this.methodsArray;
  4163. for (var i = 0; i < methods.length; ++i)
  4164. methods[i].resolve();
  4165. return Namespace.prototype.resolve.call(this);
  4166. };
  4167. /**
  4168. * @override
  4169. */
  4170. Service.prototype.add = function add(object) {
  4171. /* istanbul ignore if */
  4172. if (this.get(object.name))
  4173. throw Error("duplicate name '" + object.name + "' in " + this);
  4174. if (object instanceof Method) {
  4175. this.methods[object.name] = object;
  4176. object.parent = this;
  4177. return clearCache(this);
  4178. }
  4179. return Namespace.prototype.add.call(this, object);
  4180. };
  4181. /**
  4182. * @override
  4183. */
  4184. Service.prototype.remove = function remove(object) {
  4185. if (object instanceof Method) {
  4186. /* istanbul ignore if */
  4187. if (this.methods[object.name] !== object)
  4188. throw Error(object + " is not a member of " + this);
  4189. delete this.methods[object.name];
  4190. object.parent = null;
  4191. return clearCache(this);
  4192. }
  4193. return Namespace.prototype.remove.call(this, object);
  4194. };
  4195. /**
  4196. * Creates a runtime service using the specified rpc implementation.
  4197. * @param {RPCImpl} rpcImpl RPC implementation
  4198. * @param {boolean} [requestDelimited=false] Whether requests are length-delimited
  4199. * @param {boolean} [responseDelimited=false] Whether responses are length-delimited
  4200. * @returns {rpc.Service} RPC service. Useful where requests and/or responses are streamed.
  4201. */
  4202. Service.prototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {
  4203. var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);
  4204. for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {
  4205. var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");
  4206. rpcService[methodName] = util.codegen(["r","c"], util.isReserved(methodName) ? methodName + "_" : methodName)("return this.rpcCall(m,q,s,r,c)")({
  4207. m: method,
  4208. q: method.resolvedRequestType.ctor,
  4209. s: method.resolvedResponseType.ctor
  4210. });
  4211. }
  4212. return rpcService;
  4213. };
  4214. },{"20":20,"21":21,"28":28,"33":33}],31:[function(require,module,exports){
  4215. "use strict";
  4216. module.exports = Type;
  4217. // extends Namespace
  4218. var Namespace = require(21);
  4219. ((Type.prototype = Object.create(Namespace.prototype)).constructor = Type).className = "Type";
  4220. var Enum = require(14),
  4221. OneOf = require(23),
  4222. Field = require(15),
  4223. MapField = require(18),
  4224. Service = require(30),
  4225. Message = require(19),
  4226. Reader = require(24),
  4227. Writer = require(38),
  4228. util = require(33),
  4229. encoder = require(13),
  4230. decoder = require(12),
  4231. verifier = require(36),
  4232. converter = require(11),
  4233. wrappers = require(37);
  4234. /**
  4235. * Constructs a new reflected message type instance.
  4236. * @classdesc Reflected message type.
  4237. * @extends NamespaceBase
  4238. * @constructor
  4239. * @param {string} name Message name
  4240. * @param {Object.<string,*>} [options] Declared options
  4241. */
  4242. function Type(name, options) {
  4243. Namespace.call(this, name, options);
  4244. /**
  4245. * Message fields.
  4246. * @type {Object.<string,Field>}
  4247. */
  4248. this.fields = {}; // toJSON, marker
  4249. /**
  4250. * Oneofs declared within this namespace, if any.
  4251. * @type {Object.<string,OneOf>}
  4252. */
  4253. this.oneofs = undefined; // toJSON
  4254. /**
  4255. * Extension ranges, if any.
  4256. * @type {number[][]}
  4257. */
  4258. this.extensions = undefined; // toJSON
  4259. /**
  4260. * Reserved ranges, if any.
  4261. * @type {Array.<number[]|string>}
  4262. */
  4263. this.reserved = undefined; // toJSON
  4264. /*?
  4265. * Whether this type is a legacy group.
  4266. * @type {boolean|undefined}
  4267. */
  4268. this.group = undefined; // toJSON
  4269. /**
  4270. * Cached fields by id.
  4271. * @type {Object.<number,Field>|null}
  4272. * @private
  4273. */
  4274. this._fieldsById = null;
  4275. /**
  4276. * Cached fields as an array.
  4277. * @type {Field[]|null}
  4278. * @private
  4279. */
  4280. this._fieldsArray = null;
  4281. /**
  4282. * Cached oneofs as an array.
  4283. * @type {OneOf[]|null}
  4284. * @private
  4285. */
  4286. this._oneofsArray = null;
  4287. /**
  4288. * Cached constructor.
  4289. * @type {Constructor<{}>}
  4290. * @private
  4291. */
  4292. this._ctor = null;
  4293. }
  4294. Object.defineProperties(Type.prototype, {
  4295. /**
  4296. * Message fields by id.
  4297. * @name Type#fieldsById
  4298. * @type {Object.<number,Field>}
  4299. * @readonly
  4300. */
  4301. fieldsById: {
  4302. get: function() {
  4303. /* istanbul ignore if */
  4304. if (this._fieldsById)
  4305. return this._fieldsById;
  4306. this._fieldsById = {};
  4307. for (var names = Object.keys(this.fields), i = 0; i < names.length; ++i) {
  4308. var field = this.fields[names[i]],
  4309. id = field.id;
  4310. /* istanbul ignore if */
  4311. if (this._fieldsById[id])
  4312. throw Error("duplicate id " + id + " in " + this);
  4313. this._fieldsById[id] = field;
  4314. }
  4315. return this._fieldsById;
  4316. }
  4317. },
  4318. /**
  4319. * Fields of this message as an array for iteration.
  4320. * @name Type#fieldsArray
  4321. * @type {Field[]}
  4322. * @readonly
  4323. */
  4324. fieldsArray: {
  4325. get: function() {
  4326. return this._fieldsArray || (this._fieldsArray = util.toArray(this.fields));
  4327. }
  4328. },
  4329. /**
  4330. * Oneofs of this message as an array for iteration.
  4331. * @name Type#oneofsArray
  4332. * @type {OneOf[]}
  4333. * @readonly
  4334. */
  4335. oneofsArray: {
  4336. get: function() {
  4337. return this._oneofsArray || (this._oneofsArray = util.toArray(this.oneofs));
  4338. }
  4339. },
  4340. /**
  4341. * The registered constructor, if any registered, otherwise a generic constructor.
  4342. * Assigning a function replaces the internal constructor. If the function does not extend {@link Message} yet, its prototype will be setup accordingly and static methods will be populated. If it already extends {@link Message}, it will just replace the internal constructor.
  4343. * @name Type#ctor
  4344. * @type {Constructor<{}>}
  4345. */
  4346. ctor: {
  4347. get: function() {
  4348. return this._ctor || (this.ctor = Type.generateConstructor(this)());
  4349. },
  4350. set: function(ctor) {
  4351. // Ensure proper prototype
  4352. var prototype = ctor.prototype;
  4353. if (!(prototype instanceof Message)) {
  4354. (ctor.prototype = new Message()).constructor = ctor;
  4355. util.merge(ctor.prototype, prototype);
  4356. }
  4357. // Classes and messages reference their reflected type
  4358. ctor.$type = ctor.prototype.$type = this;
  4359. // Mix in static methods
  4360. util.merge(ctor, Message, true);
  4361. this._ctor = ctor;
  4362. // Messages have non-enumerable default values on their prototype
  4363. var i = 0;
  4364. for (; i < /* initializes */ this.fieldsArray.length; ++i)
  4365. this._fieldsArray[i].resolve(); // ensures a proper value
  4366. // Messages have non-enumerable getters and setters for each virtual oneof field
  4367. var ctorProperties = {};
  4368. for (i = 0; i < /* initializes */ this.oneofsArray.length; ++i)
  4369. ctorProperties[this._oneofsArray[i].resolve().name] = {
  4370. get: util.oneOfGetter(this._oneofsArray[i].oneof),
  4371. set: util.oneOfSetter(this._oneofsArray[i].oneof)
  4372. };
  4373. if (i)
  4374. Object.defineProperties(ctor.prototype, ctorProperties);
  4375. }
  4376. }
  4377. });
  4378. /**
  4379. * Generates a constructor function for the specified type.
  4380. * @param {Type} mtype Message type
  4381. * @returns {Codegen} Codegen instance
  4382. */
  4383. Type.generateConstructor = function generateConstructor(mtype) {
  4384. /* eslint-disable no-unexpected-multiline */
  4385. var gen = util.codegen(["p"], mtype.name);
  4386. // explicitly initialize mutable object/array fields so that these aren't just inherited from the prototype
  4387. for (var i = 0, field; i < mtype.fieldsArray.length; ++i)
  4388. if ((field = mtype._fieldsArray[i]).map) gen
  4389. ("this%s={}", util.safeProp(field.name));
  4390. else if (field.repeated) gen
  4391. ("this%s=[]", util.safeProp(field.name));
  4392. return gen
  4393. ("if(p)for(var ks=Object.keys(p),i=0;i<ks.length;++i)if(p[ks[i]]!=null)") // omit undefined or null
  4394. ("this[ks[i]]=p[ks[i]]");
  4395. /* eslint-enable no-unexpected-multiline */
  4396. };
  4397. function clearCache(type) {
  4398. type._fieldsById = type._fieldsArray = type._oneofsArray = null;
  4399. delete type.encode;
  4400. delete type.decode;
  4401. delete type.verify;
  4402. return type;
  4403. }
  4404. /**
  4405. * Message type descriptor.
  4406. * @interface IType
  4407. * @extends INamespace
  4408. * @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
  4409. * @property {Object.<string,IField>} fields Field descriptors
  4410. * @property {number[][]} [extensions] Extension ranges
  4411. * @property {number[][]} [reserved] Reserved ranges
  4412. * @property {boolean} [group=false] Whether a legacy group or not
  4413. */
  4414. /**
  4415. * Creates a message type from a message type descriptor.
  4416. * @param {string} name Message name
  4417. * @param {IType} json Message type descriptor
  4418. * @returns {Type} Created message type
  4419. */
  4420. Type.fromJSON = function fromJSON(name, json) {
  4421. var type = new Type(name, json.options);
  4422. type.extensions = json.extensions;
  4423. type.reserved = json.reserved;
  4424. var names = Object.keys(json.fields),
  4425. i = 0;
  4426. for (; i < names.length; ++i)
  4427. type.add(
  4428. ( typeof json.fields[names[i]].keyType !== "undefined"
  4429. ? MapField.fromJSON
  4430. : Field.fromJSON )(names[i], json.fields[names[i]])
  4431. );
  4432. if (json.oneofs)
  4433. for (names = Object.keys(json.oneofs), i = 0; i < names.length; ++i)
  4434. type.add(OneOf.fromJSON(names[i], json.oneofs[names[i]]));
  4435. if (json.nested)
  4436. for (names = Object.keys(json.nested), i = 0; i < names.length; ++i) {
  4437. var nested = json.nested[names[i]];
  4438. type.add( // most to least likely
  4439. ( nested.id !== undefined
  4440. ? Field.fromJSON
  4441. : nested.fields !== undefined
  4442. ? Type.fromJSON
  4443. : nested.values !== undefined
  4444. ? Enum.fromJSON
  4445. : nested.methods !== undefined
  4446. ? Service.fromJSON
  4447. : Namespace.fromJSON )(names[i], nested)
  4448. );
  4449. }
  4450. if (json.extensions && json.extensions.length)
  4451. type.extensions = json.extensions;
  4452. if (json.reserved && json.reserved.length)
  4453. type.reserved = json.reserved;
  4454. if (json.group)
  4455. type.group = true;
  4456. if (json.comment)
  4457. type.comment = json.comment;
  4458. return type;
  4459. };
  4460. /**
  4461. * Converts this message type to a message type descriptor.
  4462. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  4463. * @returns {IType} Message type descriptor
  4464. */
  4465. Type.prototype.toJSON = function toJSON(toJSONOptions) {
  4466. var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);
  4467. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  4468. return util.toObject([
  4469. "options" , inherited && inherited.options || undefined,
  4470. "oneofs" , Namespace.arrayToJSON(this.oneofsArray, toJSONOptions),
  4471. "fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; }), toJSONOptions) || {},
  4472. "extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
  4473. "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
  4474. "group" , this.group || undefined,
  4475. "nested" , inherited && inherited.nested || undefined,
  4476. "comment" , keepComments ? this.comment : undefined
  4477. ]);
  4478. };
  4479. /**
  4480. * @override
  4481. */
  4482. Type.prototype.resolveAll = function resolveAll() {
  4483. var fields = this.fieldsArray, i = 0;
  4484. while (i < fields.length)
  4485. fields[i++].resolve();
  4486. var oneofs = this.oneofsArray; i = 0;
  4487. while (i < oneofs.length)
  4488. oneofs[i++].resolve();
  4489. return Namespace.prototype.resolveAll.call(this);
  4490. };
  4491. /**
  4492. * @override
  4493. */
  4494. Type.prototype.get = function get(name) {
  4495. return this.fields[name]
  4496. || this.oneofs && this.oneofs[name]
  4497. || this.nested && this.nested[name]
  4498. || null;
  4499. };
  4500. /**
  4501. * Adds a nested object to this type.
  4502. * @param {ReflectionObject} object Nested object to add
  4503. * @returns {Type} `this`
  4504. * @throws {TypeError} If arguments are invalid
  4505. * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id
  4506. */
  4507. Type.prototype.add = function add(object) {
  4508. if (this.get(object.name))
  4509. throw Error("duplicate name '" + object.name + "' in " + this);
  4510. if (object instanceof Field && object.extend === undefined) {
  4511. // NOTE: Extension fields aren't actual fields on the declaring type, but nested objects.
  4512. // The root object takes care of adding distinct sister-fields to the respective extended
  4513. // type instead.
  4514. // avoids calling the getter if not absolutely necessary because it's called quite frequently
  4515. if (this._fieldsById ? /* istanbul ignore next */ this._fieldsById[object.id] : this.fieldsById[object.id])
  4516. throw Error("duplicate id " + object.id + " in " + this);
  4517. if (this.isReservedId(object.id))
  4518. throw Error("id " + object.id + " is reserved in " + this);
  4519. if (this.isReservedName(object.name))
  4520. throw Error("name '" + object.name + "' is reserved in " + this);
  4521. if (object.parent)
  4522. object.parent.remove(object);
  4523. this.fields[object.name] = object;
  4524. object.message = this;
  4525. object.onAdd(this);
  4526. return clearCache(this);
  4527. }
  4528. if (object instanceof OneOf) {
  4529. if (!this.oneofs)
  4530. this.oneofs = {};
  4531. this.oneofs[object.name] = object;
  4532. object.onAdd(this);
  4533. return clearCache(this);
  4534. }
  4535. return Namespace.prototype.add.call(this, object);
  4536. };
  4537. /**
  4538. * Removes a nested object from this type.
  4539. * @param {ReflectionObject} object Nested object to remove
  4540. * @returns {Type} `this`
  4541. * @throws {TypeError} If arguments are invalid
  4542. * @throws {Error} If `object` is not a member of this type
  4543. */
  4544. Type.prototype.remove = function remove(object) {
  4545. if (object instanceof Field && object.extend === undefined) {
  4546. // See Type#add for the reason why extension fields are excluded here.
  4547. /* istanbul ignore if */
  4548. if (!this.fields || this.fields[object.name] !== object)
  4549. throw Error(object + " is not a member of " + this);
  4550. delete this.fields[object.name];
  4551. object.parent = null;
  4552. object.onRemove(this);
  4553. return clearCache(this);
  4554. }
  4555. if (object instanceof OneOf) {
  4556. /* istanbul ignore if */
  4557. if (!this.oneofs || this.oneofs[object.name] !== object)
  4558. throw Error(object + " is not a member of " + this);
  4559. delete this.oneofs[object.name];
  4560. object.parent = null;
  4561. object.onRemove(this);
  4562. return clearCache(this);
  4563. }
  4564. return Namespace.prototype.remove.call(this, object);
  4565. };
  4566. /**
  4567. * Tests if the specified id is reserved.
  4568. * @param {number} id Id to test
  4569. * @returns {boolean} `true` if reserved, otherwise `false`
  4570. */
  4571. Type.prototype.isReservedId = function isReservedId(id) {
  4572. return Namespace.isReservedId(this.reserved, id);
  4573. };
  4574. /**
  4575. * Tests if the specified name is reserved.
  4576. * @param {string} name Name to test
  4577. * @returns {boolean} `true` if reserved, otherwise `false`
  4578. */
  4579. Type.prototype.isReservedName = function isReservedName(name) {
  4580. return Namespace.isReservedName(this.reserved, name);
  4581. };
  4582. /**
  4583. * Creates a new message of this type using the specified properties.
  4584. * @param {Object.<string,*>} [properties] Properties to set
  4585. * @returns {Message<{}>} Message instance
  4586. */
  4587. Type.prototype.create = function create(properties) {
  4588. return new this.ctor(properties);
  4589. };
  4590. /**
  4591. * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}.
  4592. * @returns {Type} `this`
  4593. */
  4594. Type.prototype.setup = function setup() {
  4595. // Sets up everything at once so that the prototype chain does not have to be re-evaluated
  4596. // multiple times (V8, soft-deopt prototype-check).
  4597. var fullName = this.fullName,
  4598. types = [];
  4599. for (var i = 0; i < /* initializes */ this.fieldsArray.length; ++i)
  4600. types.push(this._fieldsArray[i].resolve().resolvedType);
  4601. // Replace setup methods with type-specific generated functions
  4602. this.encode = encoder(this)({
  4603. Writer : Writer,
  4604. types : types,
  4605. util : util
  4606. });
  4607. this.decode = decoder(this)({
  4608. Reader : Reader,
  4609. types : types,
  4610. util : util
  4611. });
  4612. this.verify = verifier(this)({
  4613. types : types,
  4614. util : util
  4615. });
  4616. this.fromObject = converter.fromObject(this)({
  4617. types : types,
  4618. util : util
  4619. });
  4620. this.toObject = converter.toObject(this)({
  4621. types : types,
  4622. util : util
  4623. });
  4624. // Inject custom wrappers for common types
  4625. var wrapper = wrappers[fullName];
  4626. if (wrapper) {
  4627. var originalThis = Object.create(this);
  4628. // if (wrapper.fromObject) {
  4629. originalThis.fromObject = this.fromObject;
  4630. this.fromObject = wrapper.fromObject.bind(originalThis);
  4631. // }
  4632. // if (wrapper.toObject) {
  4633. originalThis.toObject = this.toObject;
  4634. this.toObject = wrapper.toObject.bind(originalThis);
  4635. // }
  4636. }
  4637. return this;
  4638. };
  4639. /**
  4640. * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages.
  4641. * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
  4642. * @param {Writer} [writer] Writer to encode to
  4643. * @returns {Writer} writer
  4644. */
  4645. Type.prototype.encode = function encode_setup(message, writer) {
  4646. return this.setup().encode(message, writer); // overrides this method
  4647. };
  4648. /**
  4649. * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages.
  4650. * @param {Message<{}>|Object.<string,*>} message Message instance or plain object
  4651. * @param {Writer} [writer] Writer to encode to
  4652. * @returns {Writer} writer
  4653. */
  4654. Type.prototype.encodeDelimited = function encodeDelimited(message, writer) {
  4655. return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
  4656. };
  4657. /**
  4658. * Decodes a message of this type.
  4659. * @param {Reader|Uint8Array} reader Reader or buffer to decode from
  4660. * @param {number} [length] Length of the message, if known beforehand
  4661. * @returns {Message<{}>} Decoded message
  4662. * @throws {Error} If the payload is not a reader or valid buffer
  4663. * @throws {util.ProtocolError<{}>} If required fields are missing
  4664. */
  4665. Type.prototype.decode = function decode_setup(reader, length) {
  4666. return this.setup().decode(reader, length); // overrides this method
  4667. };
  4668. /**
  4669. * Decodes a message of this type preceeded by its byte length as a varint.
  4670. * @param {Reader|Uint8Array} reader Reader or buffer to decode from
  4671. * @returns {Message<{}>} Decoded message
  4672. * @throws {Error} If the payload is not a reader or valid buffer
  4673. * @throws {util.ProtocolError} If required fields are missing
  4674. */
  4675. Type.prototype.decodeDelimited = function decodeDelimited(reader) {
  4676. if (!(reader instanceof Reader))
  4677. reader = Reader.create(reader);
  4678. return this.decode(reader, reader.uint32());
  4679. };
  4680. /**
  4681. * Verifies that field values are valid and that required fields are present.
  4682. * @param {Object.<string,*>} message Plain object to verify
  4683. * @returns {null|string} `null` if valid, otherwise the reason why it is not
  4684. */
  4685. Type.prototype.verify = function verify_setup(message) {
  4686. return this.setup().verify(message); // overrides this method
  4687. };
  4688. /**
  4689. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  4690. * @param {Object.<string,*>} object Plain object to convert
  4691. * @returns {Message<{}>} Message instance
  4692. */
  4693. Type.prototype.fromObject = function fromObject(object) {
  4694. return this.setup().fromObject(object);
  4695. };
  4696. /**
  4697. * Conversion options as used by {@link Type#toObject} and {@link Message.toObject}.
  4698. * @interface IConversionOptions
  4699. * @property {Function} [longs] Long conversion type.
  4700. * Valid values are `String` and `Number` (the global types).
  4701. * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
  4702. * @property {Function} [enums] Enum value conversion type.
  4703. * Only valid value is `String` (the global type).
  4704. * Defaults to copy the present value, which is the numeric id.
  4705. * @property {Function} [bytes] Bytes value conversion type.
  4706. * Valid values are `Array` and (a base64 encoded) `String` (the global types).
  4707. * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser.
  4708. * @property {boolean} [defaults=false] Also sets default values on the resulting object
  4709. * @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false`
  4710. * @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false`
  4711. * @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any
  4712. * @property {boolean} [json=false] Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings
  4713. */
  4714. /**
  4715. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  4716. * @param {Message<{}>} message Message instance
  4717. * @param {IConversionOptions} [options] Conversion options
  4718. * @returns {Object.<string,*>} Plain object
  4719. */
  4720. Type.prototype.toObject = function toObject(message, options) {
  4721. return this.setup().toObject(message, options);
  4722. };
  4723. /**
  4724. * Decorator function as returned by {@link Type.d} (TypeScript).
  4725. * @typedef TypeDecorator
  4726. * @type {function}
  4727. * @param {Constructor<T>} target Target constructor
  4728. * @returns {undefined}
  4729. * @template T extends Message<T>
  4730. */
  4731. /**
  4732. * Type decorator (TypeScript).
  4733. * @param {string} [typeName] Type name, defaults to the constructor's name
  4734. * @returns {TypeDecorator<T>} Decorator function
  4735. * @template T extends Message<T>
  4736. */
  4737. Type.d = function decorateType(typeName) {
  4738. return function typeDecorator(target) {
  4739. util.decorateType(target, typeName);
  4740. };
  4741. };
  4742. },{"11":11,"12":12,"13":13,"14":14,"15":15,"18":18,"19":19,"21":21,"23":23,"24":24,"30":30,"33":33,"36":36,"37":37,"38":38}],32:[function(require,module,exports){
  4743. "use strict";
  4744. /**
  4745. * Common type constants.
  4746. * @namespace
  4747. */
  4748. var types = exports;
  4749. var util = require(33);
  4750. var s = [
  4751. "double", // 0
  4752. "float", // 1
  4753. "int32", // 2
  4754. "uint32", // 3
  4755. "sint32", // 4
  4756. "fixed32", // 5
  4757. "sfixed32", // 6
  4758. "int64", // 7
  4759. "uint64", // 8
  4760. "sint64", // 9
  4761. "fixed64", // 10
  4762. "sfixed64", // 11
  4763. "bool", // 12
  4764. "string", // 13
  4765. "bytes" // 14
  4766. ];
  4767. function bake(values, offset) {
  4768. var i = 0, o = {};
  4769. offset |= 0;
  4770. while (i < values.length) o[s[i + offset]] = values[i++];
  4771. return o;
  4772. }
  4773. /**
  4774. * Basic type wire types.
  4775. * @type {Object.<string,number>}
  4776. * @const
  4777. * @property {number} double=1 Fixed64 wire type
  4778. * @property {number} float=5 Fixed32 wire type
  4779. * @property {number} int32=0 Varint wire type
  4780. * @property {number} uint32=0 Varint wire type
  4781. * @property {number} sint32=0 Varint wire type
  4782. * @property {number} fixed32=5 Fixed32 wire type
  4783. * @property {number} sfixed32=5 Fixed32 wire type
  4784. * @property {number} int64=0 Varint wire type
  4785. * @property {number} uint64=0 Varint wire type
  4786. * @property {number} sint64=0 Varint wire type
  4787. * @property {number} fixed64=1 Fixed64 wire type
  4788. * @property {number} sfixed64=1 Fixed64 wire type
  4789. * @property {number} bool=0 Varint wire type
  4790. * @property {number} string=2 Ldelim wire type
  4791. * @property {number} bytes=2 Ldelim wire type
  4792. */
  4793. types.basic = bake([
  4794. /* double */ 1,
  4795. /* float */ 5,
  4796. /* int32 */ 0,
  4797. /* uint32 */ 0,
  4798. /* sint32 */ 0,
  4799. /* fixed32 */ 5,
  4800. /* sfixed32 */ 5,
  4801. /* int64 */ 0,
  4802. /* uint64 */ 0,
  4803. /* sint64 */ 0,
  4804. /* fixed64 */ 1,
  4805. /* sfixed64 */ 1,
  4806. /* bool */ 0,
  4807. /* string */ 2,
  4808. /* bytes */ 2
  4809. ]);
  4810. /**
  4811. * Basic type defaults.
  4812. * @type {Object.<string,*>}
  4813. * @const
  4814. * @property {number} double=0 Double default
  4815. * @property {number} float=0 Float default
  4816. * @property {number} int32=0 Int32 default
  4817. * @property {number} uint32=0 Uint32 default
  4818. * @property {number} sint32=0 Sint32 default
  4819. * @property {number} fixed32=0 Fixed32 default
  4820. * @property {number} sfixed32=0 Sfixed32 default
  4821. * @property {number} int64=0 Int64 default
  4822. * @property {number} uint64=0 Uint64 default
  4823. * @property {number} sint64=0 Sint32 default
  4824. * @property {number} fixed64=0 Fixed64 default
  4825. * @property {number} sfixed64=0 Sfixed64 default
  4826. * @property {boolean} bool=false Bool default
  4827. * @property {string} string="" String default
  4828. * @property {Array.<number>} bytes=Array(0) Bytes default
  4829. * @property {null} message=null Message default
  4830. */
  4831. types.defaults = bake([
  4832. /* double */ 0,
  4833. /* float */ 0,
  4834. /* int32 */ 0,
  4835. /* uint32 */ 0,
  4836. /* sint32 */ 0,
  4837. /* fixed32 */ 0,
  4838. /* sfixed32 */ 0,
  4839. /* int64 */ 0,
  4840. /* uint64 */ 0,
  4841. /* sint64 */ 0,
  4842. /* fixed64 */ 0,
  4843. /* sfixed64 */ 0,
  4844. /* bool */ false,
  4845. /* string */ "",
  4846. /* bytes */ util.emptyArray,
  4847. /* message */ null
  4848. ]);
  4849. /**
  4850. * Basic long type wire types.
  4851. * @type {Object.<string,number>}
  4852. * @const
  4853. * @property {number} int64=0 Varint wire type
  4854. * @property {number} uint64=0 Varint wire type
  4855. * @property {number} sint64=0 Varint wire type
  4856. * @property {number} fixed64=1 Fixed64 wire type
  4857. * @property {number} sfixed64=1 Fixed64 wire type
  4858. */
  4859. types.long = bake([
  4860. /* int64 */ 0,
  4861. /* uint64 */ 0,
  4862. /* sint64 */ 0,
  4863. /* fixed64 */ 1,
  4864. /* sfixed64 */ 1
  4865. ], 7);
  4866. /**
  4867. * Allowed types for map keys with their associated wire type.
  4868. * @type {Object.<string,number>}
  4869. * @const
  4870. * @property {number} int32=0 Varint wire type
  4871. * @property {number} uint32=0 Varint wire type
  4872. * @property {number} sint32=0 Varint wire type
  4873. * @property {number} fixed32=5 Fixed32 wire type
  4874. * @property {number} sfixed32=5 Fixed32 wire type
  4875. * @property {number} int64=0 Varint wire type
  4876. * @property {number} uint64=0 Varint wire type
  4877. * @property {number} sint64=0 Varint wire type
  4878. * @property {number} fixed64=1 Fixed64 wire type
  4879. * @property {number} sfixed64=1 Fixed64 wire type
  4880. * @property {number} bool=0 Varint wire type
  4881. * @property {number} string=2 Ldelim wire type
  4882. */
  4883. types.mapKey = bake([
  4884. /* int32 */ 0,
  4885. /* uint32 */ 0,
  4886. /* sint32 */ 0,
  4887. /* fixed32 */ 5,
  4888. /* sfixed32 */ 5,
  4889. /* int64 */ 0,
  4890. /* uint64 */ 0,
  4891. /* sint64 */ 0,
  4892. /* fixed64 */ 1,
  4893. /* sfixed64 */ 1,
  4894. /* bool */ 0,
  4895. /* string */ 2
  4896. ], 2);
  4897. /**
  4898. * Allowed types for packed repeated fields with their associated wire type.
  4899. * @type {Object.<string,number>}
  4900. * @const
  4901. * @property {number} double=1 Fixed64 wire type
  4902. * @property {number} float=5 Fixed32 wire type
  4903. * @property {number} int32=0 Varint wire type
  4904. * @property {number} uint32=0 Varint wire type
  4905. * @property {number} sint32=0 Varint wire type
  4906. * @property {number} fixed32=5 Fixed32 wire type
  4907. * @property {number} sfixed32=5 Fixed32 wire type
  4908. * @property {number} int64=0 Varint wire type
  4909. * @property {number} uint64=0 Varint wire type
  4910. * @property {number} sint64=0 Varint wire type
  4911. * @property {number} fixed64=1 Fixed64 wire type
  4912. * @property {number} sfixed64=1 Fixed64 wire type
  4913. * @property {number} bool=0 Varint wire type
  4914. */
  4915. types.packed = bake([
  4916. /* double */ 1,
  4917. /* float */ 5,
  4918. /* int32 */ 0,
  4919. /* uint32 */ 0,
  4920. /* sint32 */ 0,
  4921. /* fixed32 */ 5,
  4922. /* sfixed32 */ 5,
  4923. /* int64 */ 0,
  4924. /* uint64 */ 0,
  4925. /* sint64 */ 0,
  4926. /* fixed64 */ 1,
  4927. /* sfixed64 */ 1,
  4928. /* bool */ 0
  4929. ]);
  4930. },{"33":33}],33:[function(require,module,exports){
  4931. "use strict";
  4932. /**
  4933. * Various utility functions.
  4934. * @namespace
  4935. */
  4936. var util = module.exports = require(35);
  4937. var roots = require(27);
  4938. var Type, // cyclic
  4939. Enum;
  4940. util.codegen = require(3);
  4941. util.fetch = require(5);
  4942. util.path = require(8);
  4943. /**
  4944. * Node's fs module if available.
  4945. * @type {Object.<string,*>}
  4946. */
  4947. util.fs = util.inquire("fs");
  4948. /**
  4949. * Converts an object's values to an array.
  4950. * @param {Object.<string,*>} object Object to convert
  4951. * @returns {Array.<*>} Converted array
  4952. */
  4953. util.toArray = function toArray(object) {
  4954. if (object) {
  4955. var keys = Object.keys(object),
  4956. array = new Array(keys.length),
  4957. index = 0;
  4958. while (index < keys.length)
  4959. array[index] = object[keys[index++]];
  4960. return array;
  4961. }
  4962. return [];
  4963. };
  4964. /**
  4965. * Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
  4966. * @param {Array.<*>} array Array to convert
  4967. * @returns {Object.<string,*>} Converted object
  4968. */
  4969. util.toObject = function toObject(array) {
  4970. var object = {},
  4971. index = 0;
  4972. while (index < array.length) {
  4973. var key = array[index++],
  4974. val = array[index++];
  4975. if (val !== undefined)
  4976. object[key] = val;
  4977. }
  4978. return object;
  4979. };
  4980. var safePropBackslashRe = /\\/g,
  4981. safePropQuoteRe = /"/g;
  4982. /**
  4983. * Tests whether the specified name is a reserved word in JS.
  4984. * @param {string} name Name to test
  4985. * @returns {boolean} `true` if reserved, otherwise `false`
  4986. */
  4987. util.isReserved = function isReserved(name) {
  4988. return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
  4989. };
  4990. /**
  4991. * Returns a safe property accessor for the specified property name.
  4992. * @param {string} prop Property name
  4993. * @returns {string} Safe accessor
  4994. */
  4995. util.safeProp = function safeProp(prop) {
  4996. if (!/^[$\w_]+$/.test(prop) || util.isReserved(prop))
  4997. return "[\"" + prop.replace(safePropBackslashRe, "\\\\").replace(safePropQuoteRe, "\\\"") + "\"]";
  4998. return "." + prop;
  4999. };
  5000. /**
  5001. * Converts the first character of a string to upper case.
  5002. * @param {string} str String to convert
  5003. * @returns {string} Converted string
  5004. */
  5005. util.ucFirst = function ucFirst(str) {
  5006. return str.charAt(0).toUpperCase() + str.substring(1);
  5007. };
  5008. var camelCaseRe = /_([a-z])/g;
  5009. /**
  5010. * Converts a string to camel case.
  5011. * @param {string} str String to convert
  5012. * @returns {string} Converted string
  5013. */
  5014. util.camelCase = function camelCase(str) {
  5015. return str.substring(0, 1)
  5016. + str.substring(1)
  5017. .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
  5018. };
  5019. /**
  5020. * Compares reflected fields by id.
  5021. * @param {Field} a First field
  5022. * @param {Field} b Second field
  5023. * @returns {number} Comparison value
  5024. */
  5025. util.compareFieldsById = function compareFieldsById(a, b) {
  5026. return a.id - b.id;
  5027. };
  5028. /**
  5029. * Decorator helper for types (TypeScript).
  5030. * @param {Constructor<T>} ctor Constructor function
  5031. * @param {string} [typeName] Type name, defaults to the constructor's name
  5032. * @returns {Type} Reflected type
  5033. * @template T extends Message<T>
  5034. * @property {Root} root Decorators root
  5035. */
  5036. util.decorateType = function decorateType(ctor, typeName) {
  5037. /* istanbul ignore if */
  5038. if (ctor.$type) {
  5039. if (typeName && ctor.$type.name !== typeName) {
  5040. util.decorateRoot.remove(ctor.$type);
  5041. ctor.$type.name = typeName;
  5042. util.decorateRoot.add(ctor.$type);
  5043. }
  5044. return ctor.$type;
  5045. }
  5046. /* istanbul ignore next */
  5047. if (!Type)
  5048. Type = require(31);
  5049. var type = new Type(typeName || ctor.name);
  5050. util.decorateRoot.add(type);
  5051. type.ctor = ctor; // sets up .encode, .decode etc.
  5052. Object.defineProperty(ctor, "$type", { value: type, enumerable: false });
  5053. Object.defineProperty(ctor.prototype, "$type", { value: type, enumerable: false });
  5054. return type;
  5055. };
  5056. var decorateEnumIndex = 0;
  5057. /**
  5058. * Decorator helper for enums (TypeScript).
  5059. * @param {Object} object Enum object
  5060. * @returns {Enum} Reflected enum
  5061. */
  5062. util.decorateEnum = function decorateEnum(object) {
  5063. /* istanbul ignore if */
  5064. if (object.$type)
  5065. return object.$type;
  5066. /* istanbul ignore next */
  5067. if (!Enum)
  5068. Enum = require(14);
  5069. var enm = new Enum("Enum" + decorateEnumIndex++, object);
  5070. util.decorateRoot.add(enm);
  5071. Object.defineProperty(object, "$type", { value: enm, enumerable: false });
  5072. return enm;
  5073. };
  5074. /**
  5075. * Decorator root (TypeScript).
  5076. * @name util.decorateRoot
  5077. * @type {Root}
  5078. * @readonly
  5079. */
  5080. Object.defineProperty(util, "decorateRoot", {
  5081. get: function() {
  5082. return roots["decorated"] || (roots["decorated"] = new (require(26))());
  5083. }
  5084. });
  5085. },{"14":14,"26":26,"27":27,"3":3,"31":31,"35":35,"5":5,"8":8}],34:[function(require,module,exports){
  5086. "use strict";
  5087. module.exports = LongBits;
  5088. var util = require(35);
  5089. /**
  5090. * Constructs new long bits.
  5091. * @classdesc Helper class for working with the low and high bits of a 64 bit value.
  5092. * @memberof util
  5093. * @constructor
  5094. * @param {number} lo Low 32 bits, unsigned
  5095. * @param {number} hi High 32 bits, unsigned
  5096. */
  5097. function LongBits(lo, hi) {
  5098. // note that the casts below are theoretically unnecessary as of today, but older statically
  5099. // generated converter code might still call the ctor with signed 32bits. kept for compat.
  5100. /**
  5101. * Low bits.
  5102. * @type {number}
  5103. */
  5104. this.lo = lo >>> 0;
  5105. /**
  5106. * High bits.
  5107. * @type {number}
  5108. */
  5109. this.hi = hi >>> 0;
  5110. }
  5111. /**
  5112. * Zero bits.
  5113. * @memberof util.LongBits
  5114. * @type {util.LongBits}
  5115. */
  5116. var zero = LongBits.zero = new LongBits(0, 0);
  5117. zero.toNumber = function() { return 0; };
  5118. zero.zzEncode = zero.zzDecode = function() { return this; };
  5119. zero.length = function() { return 1; };
  5120. /**
  5121. * Zero hash.
  5122. * @memberof util.LongBits
  5123. * @type {string}
  5124. */
  5125. var zeroHash = LongBits.zeroHash = "\0\0\0\0\0\0\0\0";
  5126. /**
  5127. * Constructs new long bits from the specified number.
  5128. * @param {number} value Value
  5129. * @returns {util.LongBits} Instance
  5130. */
  5131. LongBits.fromNumber = function fromNumber(value) {
  5132. if (value === 0)
  5133. return zero;
  5134. var sign = value < 0;
  5135. if (sign)
  5136. value = -value;
  5137. var lo = value >>> 0,
  5138. hi = (value - lo) / 4294967296 >>> 0;
  5139. if (sign) {
  5140. hi = ~hi >>> 0;
  5141. lo = ~lo >>> 0;
  5142. if (++lo > 4294967295) {
  5143. lo = 0;
  5144. if (++hi > 4294967295)
  5145. hi = 0;
  5146. }
  5147. }
  5148. return new LongBits(lo, hi);
  5149. };
  5150. /**
  5151. * Constructs new long bits from a number, long or string.
  5152. * @param {number|string} value Value
  5153. * @returns {util.LongBits} Instance
  5154. */
  5155. LongBits.from = function from(value) {
  5156. if (typeof value === "number")
  5157. return LongBits.fromNumber(value);
  5158. if (util.isString(value)) {
  5159. /* istanbul ignore else */
  5160. if (util.Long)
  5161. value = util.Long.fromString(value);
  5162. else
  5163. return LongBits.fromNumber(parseInt(value, 10));
  5164. }
  5165. return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
  5166. };
  5167. /**
  5168. * Converts this long bits to a possibly unsafe JavaScript number.
  5169. * @param {boolean} [unsigned=false] Whether unsigned or not
  5170. * @returns {number} Possibly unsafe number
  5171. */
  5172. LongBits.prototype.toNumber = function toNumber(unsigned) {
  5173. if (!unsigned && this.hi >>> 31) {
  5174. var lo = ~this.lo + 1 >>> 0,
  5175. hi = ~this.hi >>> 0;
  5176. if (!lo)
  5177. hi = hi + 1 >>> 0;
  5178. return -(lo + hi * 4294967296);
  5179. }
  5180. return this.lo + this.hi * 4294967296;
  5181. };
  5182. /*
  5183. * Converts this long bits to a long.
  5184. * @param {boolean} [unsigned=false] Whether unsigned or not
  5185. * @returns {Long} Long
  5186. */
  5187. LongBits.prototype.toLong = function toLong(unsigned) {
  5188. return util.Long
  5189. ? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
  5190. /* istanbul ignore next */
  5191. : { low: this.lo | 0, high: this.hi | 0, unsigned: Boolean(unsigned) };
  5192. };
  5193. var charCodeAt = String.prototype.charCodeAt;
  5194. /**
  5195. * Constructs new long bits from the specified 8 characters long hash.
  5196. * @param {string} hash Hash
  5197. * @returns {util.LongBits} Bits
  5198. */
  5199. LongBits.fromHash = function fromHash(hash) {
  5200. if (hash === zeroHash)
  5201. return zero;
  5202. return new LongBits(
  5203. ( charCodeAt.call(hash, 0)
  5204. | charCodeAt.call(hash, 1) << 8
  5205. | charCodeAt.call(hash, 2) << 16
  5206. | charCodeAt.call(hash, 3) << 24) >>> 0
  5207. ,
  5208. ( charCodeAt.call(hash, 4)
  5209. | charCodeAt.call(hash, 5) << 8
  5210. | charCodeAt.call(hash, 6) << 16
  5211. | charCodeAt.call(hash, 7) << 24) >>> 0
  5212. );
  5213. };
  5214. /**
  5215. * Converts this long bits to a 8 characters long hash.
  5216. * @returns {string} Hash
  5217. */
  5218. LongBits.prototype.toHash = function toHash() {
  5219. return String.fromCharCode(
  5220. this.lo & 255,
  5221. this.lo >>> 8 & 255,
  5222. this.lo >>> 16 & 255,
  5223. this.lo >>> 24 ,
  5224. this.hi & 255,
  5225. this.hi >>> 8 & 255,
  5226. this.hi >>> 16 & 255,
  5227. this.hi >>> 24
  5228. );
  5229. };
  5230. /**
  5231. * Zig-zag encodes this long bits.
  5232. * @returns {util.LongBits} `this`
  5233. */
  5234. LongBits.prototype.zzEncode = function zzEncode() {
  5235. var mask = this.hi >> 31;
  5236. this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
  5237. this.lo = ( this.lo << 1 ^ mask) >>> 0;
  5238. return this;
  5239. };
  5240. /**
  5241. * Zig-zag decodes this long bits.
  5242. * @returns {util.LongBits} `this`
  5243. */
  5244. LongBits.prototype.zzDecode = function zzDecode() {
  5245. var mask = -(this.lo & 1);
  5246. this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
  5247. this.hi = ( this.hi >>> 1 ^ mask) >>> 0;
  5248. return this;
  5249. };
  5250. /**
  5251. * Calculates the length of this longbits when encoded as a varint.
  5252. * @returns {number} Length
  5253. */
  5254. LongBits.prototype.length = function length() {
  5255. var part0 = this.lo,
  5256. part1 = (this.lo >>> 28 | this.hi << 4) >>> 0,
  5257. part2 = this.hi >>> 24;
  5258. return part2 === 0
  5259. ? part1 === 0
  5260. ? part0 < 16384
  5261. ? part0 < 128 ? 1 : 2
  5262. : part0 < 2097152 ? 3 : 4
  5263. : part1 < 16384
  5264. ? part1 < 128 ? 5 : 6
  5265. : part1 < 2097152 ? 7 : 8
  5266. : part2 < 128 ? 9 : 10;
  5267. };
  5268. },{"35":35}],35:[function(require,module,exports){
  5269. "use strict";
  5270. var util = exports;
  5271. // used to return a Promise where callback is omitted
  5272. util.asPromise = require(1);
  5273. // converts to / from base64 encoded strings
  5274. util.base64 = require(2);
  5275. // base class of rpc.Service
  5276. util.EventEmitter = require(4);
  5277. // float handling accross browsers
  5278. util.float = require(6);
  5279. // requires modules optionally and hides the call from bundlers
  5280. util.inquire = require(7);
  5281. // converts to / from utf8 encoded strings
  5282. util.utf8 = require(10);
  5283. // provides a node-like buffer pool in the browser
  5284. util.pool = require(9);
  5285. // utility to work with the low and high bits of a 64 bit value
  5286. util.LongBits = require(34);
  5287. // global object reference
  5288. util.global = typeof window !== "undefined" && window
  5289. || typeof global !== "undefined" && global
  5290. || typeof self !== "undefined" && self
  5291. || this; // eslint-disable-line no-invalid-this
  5292. /**
  5293. * An immuable empty array.
  5294. * @memberof util
  5295. * @type {Array.<*>}
  5296. * @const
  5297. */
  5298. util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
  5299. /**
  5300. * An immutable empty object.
  5301. * @type {Object}
  5302. * @const
  5303. */
  5304. util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
  5305. /**
  5306. * Whether running within node or not.
  5307. * @memberof util
  5308. * @type {boolean}
  5309. * @const
  5310. */
  5311. util.isNode = Boolean(util.global.process && util.global.process.versions && util.global.process.versions.node);
  5312. /**
  5313. * Tests if the specified value is an integer.
  5314. * @function
  5315. * @param {*} value Value to test
  5316. * @returns {boolean} `true` if the value is an integer
  5317. */
  5318. util.isInteger = Number.isInteger || /* istanbul ignore next */ function isInteger(value) {
  5319. return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
  5320. };
  5321. /**
  5322. * Tests if the specified value is a string.
  5323. * @param {*} value Value to test
  5324. * @returns {boolean} `true` if the value is a string
  5325. */
  5326. util.isString = function isString(value) {
  5327. return typeof value === "string" || value instanceof String;
  5328. };
  5329. /**
  5330. * Tests if the specified value is a non-null object.
  5331. * @param {*} value Value to test
  5332. * @returns {boolean} `true` if the value is a non-null object
  5333. */
  5334. util.isObject = function isObject(value) {
  5335. return value && typeof value === "object";
  5336. };
  5337. /**
  5338. * Checks if a property on a message is considered to be present.
  5339. * This is an alias of {@link util.isSet}.
  5340. * @function
  5341. * @param {Object} obj Plain object or message instance
  5342. * @param {string} prop Property name
  5343. * @returns {boolean} `true` if considered to be present, otherwise `false`
  5344. */
  5345. util.isset =
  5346. /**
  5347. * Checks if a property on a message is considered to be present.
  5348. * @param {Object} obj Plain object or message instance
  5349. * @param {string} prop Property name
  5350. * @returns {boolean} `true` if considered to be present, otherwise `false`
  5351. */
  5352. util.isSet = function isSet(obj, prop) {
  5353. var value = obj[prop];
  5354. if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
  5355. return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
  5356. return false;
  5357. };
  5358. /**
  5359. * Any compatible Buffer instance.
  5360. * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
  5361. * @interface Buffer
  5362. * @extends Uint8Array
  5363. */
  5364. /**
  5365. * Node's Buffer class if available.
  5366. * @type {Constructor<Buffer>}
  5367. */
  5368. util.Buffer = (function() {
  5369. try {
  5370. var Buffer = util.inquire("buffer").Buffer;
  5371. // refuse to use non-node buffers if not explicitly assigned (perf reasons):
  5372. return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
  5373. } catch (e) {
  5374. /* istanbul ignore next */
  5375. return null;
  5376. }
  5377. })();
  5378. // Internal alias of or polyfull for Buffer.from.
  5379. util._Buffer_from = null;
  5380. // Internal alias of or polyfill for Buffer.allocUnsafe.
  5381. util._Buffer_allocUnsafe = null;
  5382. /**
  5383. * Creates a new buffer of whatever type supported by the environment.
  5384. * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
  5385. * @returns {Uint8Array|Buffer} Buffer
  5386. */
  5387. util.newBuffer = function newBuffer(sizeOrArray) {
  5388. /* istanbul ignore next */
  5389. return typeof sizeOrArray === "number"
  5390. ? util.Buffer
  5391. ? util._Buffer_allocUnsafe(sizeOrArray)
  5392. : new util.Array(sizeOrArray)
  5393. : util.Buffer
  5394. ? util._Buffer_from(sizeOrArray)
  5395. : typeof Uint8Array === "undefined"
  5396. ? sizeOrArray
  5397. : new Uint8Array(sizeOrArray);
  5398. };
  5399. /**
  5400. * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
  5401. * @type {Constructor<Uint8Array>}
  5402. */
  5403. util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
  5404. /*
  5405. * Long.js's Long class if available.
  5406. */
  5407. util.Long = /* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore next */ util.global.dcodeIO.Long
  5408. || /* istanbul ignore next */ util.global.Long
  5409. || util.inquire("long");
  5410. /**
  5411. * Regular expression used to verify 2 bit (`bool`) map keys.
  5412. * @type {RegExp}
  5413. * @const
  5414. */
  5415. util.key2Re = /^true|false|0|1$/;
  5416. /**
  5417. * Regular expression used to verify 32 bit (`int32` etc.) map keys.
  5418. * @type {RegExp}
  5419. * @const
  5420. */
  5421. util.key32Re = /^-?(?:0|[1-9][0-9]*)$/;
  5422. /**
  5423. * Regular expression used to verify 64 bit (`int64` etc.) map keys.
  5424. * @type {RegExp}
  5425. * @const
  5426. */
  5427. util.key64Re = /^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/;
  5428. /*
  5429. * Converts a number or long to an 8 characters long hash string.
  5430. * @param {Long|number} value Value to convert
  5431. * @returns {string} Hash
  5432. */
  5433. util.longToHash = function longToHash(value) {
  5434. return value
  5435. ? util.LongBits.from(value).toHash()
  5436. : util.LongBits.zeroHash;
  5437. };
  5438. /*
  5439. * Converts an 8 characters long hash string to a long or number.
  5440. * @param {string} hash Hash
  5441. * @param {boolean} [unsigned=false] Whether unsigned or not
  5442. * @returns {Long|number} Original value
  5443. */
  5444. util.longFromHash = function longFromHash(hash, unsigned) {
  5445. var bits = util.LongBits.fromHash(hash);
  5446. if (util.Long)
  5447. return util.Long.fromBits(bits.lo, bits.hi, unsigned);
  5448. return bits.toNumber(Boolean(unsigned));
  5449. };
  5450. /**
  5451. * Merges the properties of the source object into the destination object.
  5452. * @memberof util
  5453. * @param {Object.<string,*>} dst Destination object
  5454. * @param {Object.<string,*>} src Source object
  5455. * @param {boolean} [ifNotSet=false] Merges only if the key is not already set
  5456. * @returns {Object.<string,*>} Destination object
  5457. */
  5458. function merge(dst, src, ifNotSet) { // used by converters
  5459. for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
  5460. if (dst[keys[i]] === undefined || !ifNotSet)
  5461. dst[keys[i]] = src[keys[i]];
  5462. return dst;
  5463. }
  5464. util.merge = merge;
  5465. /**
  5466. * Converts the first character of a string to lower case.
  5467. * @param {string} str String to convert
  5468. * @returns {string} Converted string
  5469. */
  5470. util.lcFirst = function lcFirst(str) {
  5471. return str.charAt(0).toLowerCase() + str.substring(1);
  5472. };
  5473. /**
  5474. * Creates a custom error constructor.
  5475. * @memberof util
  5476. * @param {string} name Error name
  5477. * @returns {Constructor<Error>} Custom error constructor
  5478. */
  5479. function newError(name) {
  5480. function CustomError(message, properties) {
  5481. if (!(this instanceof CustomError))
  5482. return new CustomError(message, properties);
  5483. // Error.call(this, message);
  5484. // ^ just returns a new error instance because the ctor can be called as a function
  5485. Object.defineProperty(this, "message", { get: function() { return message; } });
  5486. /* istanbul ignore next */
  5487. if (Error.captureStackTrace) // node
  5488. Error.captureStackTrace(this, CustomError);
  5489. else
  5490. Object.defineProperty(this, "stack", { value: (new Error()).stack || "" });
  5491. if (properties)
  5492. merge(this, properties);
  5493. }
  5494. (CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;
  5495. Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
  5496. CustomError.prototype.toString = function toString() {
  5497. return this.name + ": " + this.message;
  5498. };
  5499. return CustomError;
  5500. }
  5501. util.newError = newError;
  5502. /**
  5503. * Constructs a new protocol error.
  5504. * @classdesc Error subclass indicating a protocol specifc error.
  5505. * @memberof util
  5506. * @extends Error
  5507. * @template T extends Message<T>
  5508. * @constructor
  5509. * @param {string} message Error message
  5510. * @param {Object.<string,*>} [properties] Additional properties
  5511. * @example
  5512. * try {
  5513. * MyMessage.decode(someBuffer); // throws if required fields are missing
  5514. * } catch (e) {
  5515. * if (e instanceof ProtocolError && e.instance)
  5516. * console.log("decoded so far: " + JSON.stringify(e.instance));
  5517. * }
  5518. */
  5519. util.ProtocolError = newError("ProtocolError");
  5520. /**
  5521. * So far decoded message instance.
  5522. * @name util.ProtocolError#instance
  5523. * @type {Message<T>}
  5524. */
  5525. /**
  5526. * A OneOf getter as returned by {@link util.oneOfGetter}.
  5527. * @typedef OneOfGetter
  5528. * @type {function}
  5529. * @returns {string|undefined} Set field name, if any
  5530. */
  5531. /**
  5532. * Builds a getter for a oneof's present field name.
  5533. * @param {string[]} fieldNames Field names
  5534. * @returns {OneOfGetter} Unbound getter
  5535. */
  5536. util.oneOfGetter = function getOneOf(fieldNames) {
  5537. var fieldMap = {};
  5538. for (var i = 0; i < fieldNames.length; ++i)
  5539. fieldMap[fieldNames[i]] = 1;
  5540. /**
  5541. * @returns {string|undefined} Set field name, if any
  5542. * @this Object
  5543. * @ignore
  5544. */
  5545. return function() { // eslint-disable-line consistent-return
  5546. for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
  5547. if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
  5548. return keys[i];
  5549. };
  5550. };
  5551. /**
  5552. * A OneOf setter as returned by {@link util.oneOfSetter}.
  5553. * @typedef OneOfSetter
  5554. * @type {function}
  5555. * @param {string|undefined} value Field name
  5556. * @returns {undefined}
  5557. */
  5558. /**
  5559. * Builds a setter for a oneof's present field name.
  5560. * @param {string[]} fieldNames Field names
  5561. * @returns {OneOfSetter} Unbound setter
  5562. */
  5563. util.oneOfSetter = function setOneOf(fieldNames) {
  5564. /**
  5565. * @param {string} name Field name
  5566. * @returns {undefined}
  5567. * @this Object
  5568. * @ignore
  5569. */
  5570. return function(name) {
  5571. for (var i = 0; i < fieldNames.length; ++i)
  5572. if (fieldNames[i] !== name)
  5573. delete this[fieldNames[i]];
  5574. };
  5575. };
  5576. /**
  5577. * Default conversion options used for {@link Message#toJSON} implementations.
  5578. *
  5579. * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
  5580. *
  5581. * - Longs become strings
  5582. * - Enums become string keys
  5583. * - Bytes become base64 encoded strings
  5584. * - (Sub-)Messages become plain objects
  5585. * - Maps become plain objects with all string keys
  5586. * - Repeated fields become arrays
  5587. * - NaN and Infinity for float and double fields become strings
  5588. *
  5589. * @type {IConversionOptions}
  5590. * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
  5591. */
  5592. util.toJSONOptions = {
  5593. longs: String,
  5594. enums: String,
  5595. bytes: String,
  5596. json: true
  5597. };
  5598. // Sets up buffer utility according to the environment (called in index-minimal)
  5599. util._configure = function() {
  5600. var Buffer = util.Buffer;
  5601. /* istanbul ignore if */
  5602. if (!Buffer) {
  5603. util._Buffer_from = util._Buffer_allocUnsafe = null;
  5604. return;
  5605. }
  5606. // because node 4.x buffers are incompatible & immutable
  5607. // see: https://github.com/dcodeIO/protobuf.js/pull/665
  5608. util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
  5609. /* istanbul ignore next */
  5610. function Buffer_from(value, encoding) {
  5611. return new Buffer(value, encoding);
  5612. };
  5613. util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
  5614. /* istanbul ignore next */
  5615. function Buffer_allocUnsafe(size) {
  5616. return new Buffer(size);
  5617. };
  5618. };
  5619. },{"1":1,"10":10,"2":2,"34":34,"4":4,"6":6,"7":7,"9":9}],36:[function(require,module,exports){
  5620. "use strict";
  5621. module.exports = verifier;
  5622. var Enum = require(14),
  5623. util = require(33);
  5624. function invalid(field, expected) {
  5625. return field.name + ": " + expected + (field.repeated && expected !== "array" ? "[]" : field.map && expected !== "object" ? "{k:"+field.keyType+"}" : "") + " expected";
  5626. }
  5627. /**
  5628. * Generates a partial value verifier.
  5629. * @param {Codegen} gen Codegen instance
  5630. * @param {Field} field Reflected field
  5631. * @param {number} fieldIndex Field index
  5632. * @param {string} ref Variable reference
  5633. * @returns {Codegen} Codegen instance
  5634. * @ignore
  5635. */
  5636. function genVerifyValue(gen, field, fieldIndex, ref) {
  5637. /* eslint-disable no-unexpected-multiline */
  5638. if (field.resolvedType) {
  5639. if (field.resolvedType instanceof Enum) { gen
  5640. ("switch(%s){", ref)
  5641. ("default:")
  5642. ("return%j", invalid(field, "enum value"));
  5643. for (var keys = Object.keys(field.resolvedType.values), j = 0; j < keys.length; ++j) gen
  5644. ("case %i:", field.resolvedType.values[keys[j]]);
  5645. gen
  5646. ("break")
  5647. ("}");
  5648. } else {
  5649. gen
  5650. ("{")
  5651. ("var e=types[%i].verify(%s);", fieldIndex, ref)
  5652. ("if(e)")
  5653. ("return%j+e", field.name + ".")
  5654. ("}");
  5655. }
  5656. } else {
  5657. switch (field.type) {
  5658. case "int32":
  5659. case "uint32":
  5660. case "sint32":
  5661. case "fixed32":
  5662. case "sfixed32": gen
  5663. ("if(!util.isInteger(%s))", ref)
  5664. ("return%j", invalid(field, "integer"));
  5665. break;
  5666. case "int64":
  5667. case "uint64":
  5668. case "sint64":
  5669. case "fixed64":
  5670. case "sfixed64": gen
  5671. ("if(!util.isInteger(%s)&&!(%s&&util.isInteger(%s.low)&&util.isInteger(%s.high)))", ref, ref, ref, ref)
  5672. ("return%j", invalid(field, "integer|Long"));
  5673. break;
  5674. case "float":
  5675. case "double": gen
  5676. ("if(typeof %s!==\"number\")", ref)
  5677. ("return%j", invalid(field, "number"));
  5678. break;
  5679. case "bool": gen
  5680. ("if(typeof %s!==\"boolean\")", ref)
  5681. ("return%j", invalid(field, "boolean"));
  5682. break;
  5683. case "string": gen
  5684. ("if(!util.isString(%s))", ref)
  5685. ("return%j", invalid(field, "string"));
  5686. break;
  5687. case "bytes": gen
  5688. ("if(!(%s&&typeof %s.length===\"number\"||util.isString(%s)))", ref, ref, ref)
  5689. ("return%j", invalid(field, "buffer"));
  5690. break;
  5691. }
  5692. }
  5693. return gen;
  5694. /* eslint-enable no-unexpected-multiline */
  5695. }
  5696. /**
  5697. * Generates a partial key verifier.
  5698. * @param {Codegen} gen Codegen instance
  5699. * @param {Field} field Reflected field
  5700. * @param {string} ref Variable reference
  5701. * @returns {Codegen} Codegen instance
  5702. * @ignore
  5703. */
  5704. function genVerifyKey(gen, field, ref) {
  5705. /* eslint-disable no-unexpected-multiline */
  5706. switch (field.keyType) {
  5707. case "int32":
  5708. case "uint32":
  5709. case "sint32":
  5710. case "fixed32":
  5711. case "sfixed32": gen
  5712. ("if(!util.key32Re.test(%s))", ref)
  5713. ("return%j", invalid(field, "integer key"));
  5714. break;
  5715. case "int64":
  5716. case "uint64":
  5717. case "sint64":
  5718. case "fixed64":
  5719. case "sfixed64": gen
  5720. ("if(!util.key64Re.test(%s))", ref) // see comment above: x is ok, d is not
  5721. ("return%j", invalid(field, "integer|Long key"));
  5722. break;
  5723. case "bool": gen
  5724. ("if(!util.key2Re.test(%s))", ref)
  5725. ("return%j", invalid(field, "boolean key"));
  5726. break;
  5727. }
  5728. return gen;
  5729. /* eslint-enable no-unexpected-multiline */
  5730. }
  5731. /**
  5732. * Generates a verifier specific to the specified message type.
  5733. * @param {Type} mtype Message type
  5734. * @returns {Codegen} Codegen instance
  5735. */
  5736. function verifier(mtype) {
  5737. /* eslint-disable no-unexpected-multiline */
  5738. var gen = util.codegen(["m"], mtype.name + "$verify")
  5739. ("if(typeof m!==\"object\"||m===null)")
  5740. ("return%j", "object expected");
  5741. var oneofs = mtype.oneofsArray,
  5742. seenFirstField = {};
  5743. if (oneofs.length) gen
  5744. ("var p={}");
  5745. for (var i = 0; i < /* initializes */ mtype.fieldsArray.length; ++i) {
  5746. var field = mtype._fieldsArray[i].resolve(),
  5747. ref = "m" + util.safeProp(field.name);
  5748. if (field.optional) gen
  5749. ("if(%s!=null&&m.hasOwnProperty(%j)){", ref, field.name); // !== undefined && !== null
  5750. // map fields
  5751. if (field.map) { gen
  5752. ("if(!util.isObject(%s))", ref)
  5753. ("return%j", invalid(field, "object"))
  5754. ("var k=Object.keys(%s)", ref)
  5755. ("for(var i=0;i<k.length;++i){");
  5756. genVerifyKey(gen, field, "k[i]");
  5757. genVerifyValue(gen, field, i, ref + "[k[i]]")
  5758. ("}");
  5759. // repeated fields
  5760. } else if (field.repeated) {
  5761. var arrayRef = ref;
  5762. if (field.useToArray()) {
  5763. arrayRef = "array" + field.id;
  5764. gen("var %s", arrayRef);
  5765. gen("if (%s!=null&&%s.toArray) { %s = %s.toArray() } else { %s = %s }",
  5766. ref, ref, arrayRef, ref, arrayRef, ref);
  5767. }
  5768. gen
  5769. ("if(!Array.isArray(%s))", arrayRef)
  5770. ("return%j", invalid(field, "array"))
  5771. ("for(var i=0;i<%s.length;++i){", arrayRef);
  5772. if (field.preEncoded()) {
  5773. gen("if (!(%s instanceof Uint8Array)) {", arrayRef + "[i]")
  5774. }
  5775. genVerifyValue(gen, field, i, arrayRef + "[i]")
  5776. if (field.preEncoded()) {
  5777. gen("}");
  5778. }
  5779. gen("}");
  5780. // required or present fields
  5781. } else {
  5782. if (field.partOf) {
  5783. var oneofProp = util.safeProp(field.partOf.name);
  5784. if (seenFirstField[field.partOf.name] === 1) gen
  5785. ("if(p%s===1)", oneofProp)
  5786. ("return%j", field.partOf.name + ": multiple values");
  5787. seenFirstField[field.partOf.name] = 1;
  5788. gen
  5789. ("p%s=1", oneofProp);
  5790. }
  5791. genVerifyValue(gen, field, i, ref);
  5792. }
  5793. if (field.optional) gen
  5794. ("}");
  5795. }
  5796. return gen
  5797. ("return null");
  5798. /* eslint-enable no-unexpected-multiline */
  5799. }
  5800. },{"14":14,"33":33}],37:[function(require,module,exports){
  5801. "use strict";
  5802. /**
  5803. * Wrappers for common types.
  5804. * @type {Object.<string,IWrapper>}
  5805. * @const
  5806. */
  5807. var wrappers = exports;
  5808. var Message = require(19);
  5809. /**
  5810. * From object converter part of an {@link IWrapper}.
  5811. * @typedef WrapperFromObjectConverter
  5812. * @type {function}
  5813. * @param {Object.<string,*>} object Plain object
  5814. * @returns {Message<{}>} Message instance
  5815. * @this Type
  5816. */
  5817. /**
  5818. * To object converter part of an {@link IWrapper}.
  5819. * @typedef WrapperToObjectConverter
  5820. * @type {function}
  5821. * @param {Message<{}>} message Message instance
  5822. * @param {IConversionOptions} [options] Conversion options
  5823. * @returns {Object.<string,*>} Plain object
  5824. * @this Type
  5825. */
  5826. /**
  5827. * Common type wrapper part of {@link wrappers}.
  5828. * @interface IWrapper
  5829. * @property {WrapperFromObjectConverter} [fromObject] From object converter
  5830. * @property {WrapperToObjectConverter} [toObject] To object converter
  5831. */
  5832. // Custom wrapper for Any
  5833. wrappers[".google.protobuf.Any"] = {
  5834. fromObject: function(object) {
  5835. // unwrap value type if mapped
  5836. if (object && object["@type"]) {
  5837. var type = this.lookup(object["@type"]);
  5838. /* istanbul ignore else */
  5839. if (type) {
  5840. // type_url does not accept leading "."
  5841. var type_url = object["@type"].charAt(0) === "." ?
  5842. object["@type"].substr(1) : object["@type"];
  5843. // type_url prefix is optional, but path seperator is required
  5844. return this.create({
  5845. type_url: "/" + type_url,
  5846. value: type.encode(type.fromObject(object)).finish()
  5847. });
  5848. }
  5849. }
  5850. return this.fromObject(object);
  5851. },
  5852. toObject: function(message, options) {
  5853. // decode value if requested and unmapped
  5854. if (options && options.json && message.type_url && message.value) {
  5855. // Only use fully qualified type name after the last '/'
  5856. var name = message.type_url.substring(message.type_url.lastIndexOf("/") + 1);
  5857. var type = this.lookup(name);
  5858. /* istanbul ignore else */
  5859. if (type)
  5860. message = type.decode(message.value);
  5861. }
  5862. // wrap value if unmapped
  5863. if (!(message instanceof this.ctor) && message instanceof Message) {
  5864. var object = message.$type.toObject(message, options);
  5865. object["@type"] = message.$type.fullName;
  5866. return object;
  5867. }
  5868. return this.toObject(message, options);
  5869. }
  5870. };
  5871. },{"19":19}],38:[function(require,module,exports){
  5872. "use strict";
  5873. module.exports = Writer;
  5874. var util = require(35);
  5875. var BufferWriter; // cyclic
  5876. var LongBits = util.LongBits,
  5877. base64 = util.base64,
  5878. utf8 = util.utf8;
  5879. /**
  5880. * Constructs a new writer operation instance.
  5881. * @classdesc Scheduled writer operation.
  5882. * @constructor
  5883. * @param {function(*, Uint8Array, number)} fn Function to call
  5884. * @param {number} len Value byte length
  5885. * @param {*} val Value to write
  5886. * @ignore
  5887. */
  5888. function Op(fn, len, val) {
  5889. /**
  5890. * Function to call.
  5891. * @type {function(Uint8Array, number, *)}
  5892. */
  5893. this.fn = fn;
  5894. /**
  5895. * Value byte length.
  5896. * @type {number}
  5897. */
  5898. this.len = len;
  5899. /**
  5900. * Next operation.
  5901. * @type {Writer.Op|undefined}
  5902. */
  5903. this.next = undefined;
  5904. /**
  5905. * Value to write.
  5906. * @type {*}
  5907. */
  5908. this.val = val; // type varies
  5909. }
  5910. /* istanbul ignore next */
  5911. function noop() {} // eslint-disable-line no-empty-function
  5912. /**
  5913. * Constructs a new writer state instance.
  5914. * @classdesc Copied writer state.
  5915. * @memberof Writer
  5916. * @constructor
  5917. * @param {Writer} writer Writer to copy state from
  5918. * @ignore
  5919. */
  5920. function State(writer) {
  5921. /**
  5922. * Current head.
  5923. * @type {Writer.Op}
  5924. */
  5925. this.head = writer.head;
  5926. /**
  5927. * Current tail.
  5928. * @type {Writer.Op}
  5929. */
  5930. this.tail = writer.tail;
  5931. /**
  5932. * Current buffer length.
  5933. * @type {number}
  5934. */
  5935. this.len = writer.len;
  5936. /**
  5937. * Next state.
  5938. * @type {State|null}
  5939. */
  5940. this.next = writer.states;
  5941. }
  5942. /**
  5943. * Constructs a new writer instance.
  5944. * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
  5945. * @constructor
  5946. */
  5947. function Writer() {
  5948. /**
  5949. * Current length.
  5950. * @type {number}
  5951. */
  5952. this.len = 0;
  5953. /**
  5954. * Operations head.
  5955. * @type {Object}
  5956. */
  5957. this.head = new Op(noop, 0, 0);
  5958. /**
  5959. * Operations tail
  5960. * @type {Object}
  5961. */
  5962. this.tail = this.head;
  5963. /**
  5964. * Linked forked states.
  5965. * @type {Object|null}
  5966. */
  5967. this.states = null;
  5968. // When a value is written, the writer calculates its byte length and puts it into a linked
  5969. // list of operations to perform when finish() is called. This both allows us to allocate
  5970. // buffers of the exact required size and reduces the amount of work we have to do compared
  5971. // to first calculating over objects and then encoding over objects. In our case, the encoding
  5972. // part is just a linked list walk calling operations with already prepared values.
  5973. }
  5974. /**
  5975. * Creates a new writer.
  5976. * @function
  5977. * @returns {BufferWriter|Writer} A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
  5978. */
  5979. Writer.create = util.Buffer
  5980. ? function create_buffer_setup() {
  5981. return (Writer.create = function create_buffer() {
  5982. return new BufferWriter();
  5983. })();
  5984. }
  5985. /* istanbul ignore next */
  5986. : function create_array() {
  5987. return new Writer();
  5988. };
  5989. /**
  5990. * Allocates a buffer of the specified size.
  5991. * @param {number} size Buffer size
  5992. * @returns {Uint8Array} Buffer
  5993. */
  5994. Writer.alloc = function alloc(size) {
  5995. return new util.Array(size);
  5996. };
  5997. // Use Uint8Array buffer pool in the browser, just like node does with buffers
  5998. /* istanbul ignore else */
  5999. if (util.Array !== Array)
  6000. Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
  6001. /**
  6002. * Pushes a new operation to the queue.
  6003. * @param {function(Uint8Array, number, *)} fn Function to call
  6004. * @param {number} len Value byte length
  6005. * @param {number} val Value to write
  6006. * @returns {Writer} `this`
  6007. * @private
  6008. */
  6009. Writer.prototype._push = function push(fn, len, val) {
  6010. this.tail = this.tail.next = new Op(fn, len, val);
  6011. this.len += len;
  6012. return this;
  6013. };
  6014. function writeByte(val, buf, pos) {
  6015. buf[pos] = val & 255;
  6016. }
  6017. function writeVarint32(val, buf, pos) {
  6018. while (val > 127) {
  6019. buf[pos++] = val & 127 | 128;
  6020. val >>>= 7;
  6021. }
  6022. buf[pos] = val;
  6023. }
  6024. /**
  6025. * Constructs a new varint writer operation instance.
  6026. * @classdesc Scheduled varint writer operation.
  6027. * @extends Op
  6028. * @constructor
  6029. * @param {number} len Value byte length
  6030. * @param {number} val Value to write
  6031. * @ignore
  6032. */
  6033. function VarintOp(len, val) {
  6034. this.len = len;
  6035. this.next = undefined;
  6036. this.val = val;
  6037. }
  6038. VarintOp.prototype = Object.create(Op.prototype);
  6039. VarintOp.prototype.fn = writeVarint32;
  6040. /**
  6041. * Writes an unsigned 32 bit value as a varint.
  6042. * @param {number} value Value to write
  6043. * @returns {Writer} `this`
  6044. */
  6045. Writer.prototype.uint32 = function write_uint32(value) {
  6046. // here, the call to this.push has been inlined and a varint specific Op subclass is used.
  6047. // uint32 is by far the most frequently used operation and benefits significantly from this.
  6048. this.len += (this.tail = this.tail.next = new VarintOp(
  6049. (value = value >>> 0)
  6050. < 128 ? 1
  6051. : value < 16384 ? 2
  6052. : value < 2097152 ? 3
  6053. : value < 268435456 ? 4
  6054. : 5,
  6055. value)).len;
  6056. return this;
  6057. };
  6058. /**
  6059. * Writes a signed 32 bit value as a varint.
  6060. * @function
  6061. * @param {number} value Value to write
  6062. * @returns {Writer} `this`
  6063. */
  6064. Writer.prototype.int32 = function write_int32(value) {
  6065. return value < 0
  6066. ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
  6067. : this.uint32(value);
  6068. };
  6069. /**
  6070. * Writes a 32 bit value as a varint, zig-zag encoded.
  6071. * @param {number} value Value to write
  6072. * @returns {Writer} `this`
  6073. */
  6074. Writer.prototype.sint32 = function write_sint32(value) {
  6075. return this.uint32((value << 1 ^ value >> 31) >>> 0);
  6076. };
  6077. function writeVarint64(val, buf, pos) {
  6078. while (val.hi) {
  6079. buf[pos++] = val.lo & 127 | 128;
  6080. val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
  6081. val.hi >>>= 7;
  6082. }
  6083. while (val.lo > 127) {
  6084. buf[pos++] = val.lo & 127 | 128;
  6085. val.lo = val.lo >>> 7;
  6086. }
  6087. buf[pos++] = val.lo;
  6088. }
  6089. /**
  6090. * Writes an unsigned 64 bit value as a varint.
  6091. * @param {number|string} value Value to write
  6092. * @returns {Writer} `this`
  6093. * @throws {TypeError} If `value` is a string and no long library is present.
  6094. */
  6095. Writer.prototype.uint64 = function write_uint64(value) {
  6096. var bits = LongBits.from(value);
  6097. return this._push(writeVarint64, bits.length(), bits);
  6098. };
  6099. /**
  6100. * Writes a signed 64 bit value as a varint.
  6101. * @function
  6102. * @param {number|string} value Value to write
  6103. * @returns {Writer} `this`
  6104. * @throws {TypeError} If `value` is a string and no long library is present.
  6105. */
  6106. Writer.prototype.int64 = Writer.prototype.uint64;
  6107. /**
  6108. * Writes a signed 64 bit value as a varint, zig-zag encoded.
  6109. * @param {number|string} value Value to write
  6110. * @returns {Writer} `this`
  6111. * @throws {TypeError} If `value` is a string and no long library is present.
  6112. */
  6113. Writer.prototype.sint64 = function write_sint64(value) {
  6114. var bits = LongBits.from(value).zzEncode();
  6115. return this._push(writeVarint64, bits.length(), bits);
  6116. };
  6117. /**
  6118. * Writes a boolish value as a varint.
  6119. * @param {boolean} value Value to write
  6120. * @returns {Writer} `this`
  6121. */
  6122. Writer.prototype.bool = function write_bool(value) {
  6123. return this._push(writeByte, 1, value ? 1 : 0);
  6124. };
  6125. function writeFixed32(val, buf, pos) {
  6126. buf[pos ] = val & 255;
  6127. buf[pos + 1] = val >>> 8 & 255;
  6128. buf[pos + 2] = val >>> 16 & 255;
  6129. buf[pos + 3] = val >>> 24;
  6130. }
  6131. /**
  6132. * Writes an unsigned 32 bit value as fixed 32 bits.
  6133. * @param {number} value Value to write
  6134. * @returns {Writer} `this`
  6135. */
  6136. Writer.prototype.fixed32 = function write_fixed32(value) {
  6137. return this._push(writeFixed32, 4, value >>> 0);
  6138. };
  6139. /**
  6140. * Writes a signed 32 bit value as fixed 32 bits.
  6141. * @function
  6142. * @param {number} value Value to write
  6143. * @returns {Writer} `this`
  6144. */
  6145. Writer.prototype.sfixed32 = Writer.prototype.fixed32;
  6146. /**
  6147. * Writes an unsigned 64 bit value as fixed 64 bits.
  6148. * @param {number|string} value Value to write
  6149. * @returns {Writer} `this`
  6150. * @throws {TypeError} If `value` is a string and no long library is present.
  6151. */
  6152. Writer.prototype.fixed64 = function write_fixed64(value) {
  6153. var bits = LongBits.from(value);
  6154. return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
  6155. };
  6156. /**
  6157. * Writes a signed 64 bit value as fixed 64 bits.
  6158. * @function
  6159. * @param {number|string} value Value to write
  6160. * @returns {Writer} `this`
  6161. * @throws {TypeError} If `value` is a string and no long library is present.
  6162. */
  6163. Writer.prototype.sfixed64 = Writer.prototype.fixed64;
  6164. /**
  6165. * Writes a float (32 bit).
  6166. * @function
  6167. * @param {number} value Value to write
  6168. * @returns {Writer} `this`
  6169. */
  6170. Writer.prototype.float = function write_float(value) {
  6171. return this._push(util.float.writeFloatLE, 4, value);
  6172. };
  6173. /**
  6174. * Writes a double (64 bit float).
  6175. * @function
  6176. * @param {number} value Value to write
  6177. * @returns {Writer} `this`
  6178. */
  6179. Writer.prototype.double = function write_double(value) {
  6180. return this._push(util.float.writeDoubleLE, 8, value);
  6181. };
  6182. var writeBytes = util.Array.prototype.set
  6183. ? function writeBytes_set(val, buf, pos) {
  6184. buf.set(val, pos); // also works for plain array values
  6185. }
  6186. /* istanbul ignore next */
  6187. : function writeBytes_for(val, buf, pos) {
  6188. for (var i = 0; i < val.length; ++i)
  6189. buf[pos + i] = val[i];
  6190. };
  6191. /**
  6192. * Writes a sequence of bytes.
  6193. * @param {Uint8Array|string} value Buffer or base64 encoded string to write
  6194. * @returns {Writer} `this`
  6195. */
  6196. Writer.prototype.bytes = function write_bytes(value) {
  6197. var len = value.length >>> 0;
  6198. if (!len)
  6199. return this._push(writeByte, 1, 0);
  6200. if (util.isString(value)) {
  6201. var buf = Writer.alloc(len = base64.length(value));
  6202. base64.decode(value, buf, 0);
  6203. value = buf;
  6204. }
  6205. return this.uint32(len)._push(writeBytes, len, value);
  6206. };
  6207. /**
  6208. * Writes a string.
  6209. * @param {string} value Value to write
  6210. * @returns {Writer} `this`
  6211. */
  6212. Writer.prototype.string = function write_string(value) {
  6213. var len = utf8.length(value);
  6214. return len
  6215. ? this.uint32(len)._push(utf8.write, len, value)
  6216. : this._push(writeByte, 1, 0);
  6217. };
  6218. /**
  6219. * Forks this writer's state by pushing it to a stack.
  6220. * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
  6221. * @returns {Writer} `this`
  6222. */
  6223. Writer.prototype.fork = function fork() {
  6224. this.states = new State(this);
  6225. this.head = this.tail = new Op(noop, 0, 0);
  6226. this.len = 0;
  6227. return this;
  6228. };
  6229. /**
  6230. * Resets this instance to the last state.
  6231. * @returns {Writer} `this`
  6232. */
  6233. Writer.prototype.reset = function reset() {
  6234. if (this.states) {
  6235. this.head = this.states.head;
  6236. this.tail = this.states.tail;
  6237. this.len = this.states.len;
  6238. this.states = this.states.next;
  6239. } else {
  6240. this.head = this.tail = new Op(noop, 0, 0);
  6241. this.len = 0;
  6242. }
  6243. return this;
  6244. };
  6245. /**
  6246. * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
  6247. * @returns {Writer} `this`
  6248. */
  6249. Writer.prototype.ldelim = function ldelim() {
  6250. var head = this.head,
  6251. tail = this.tail,
  6252. len = this.len;
  6253. this.reset().uint32(len);
  6254. if (len) {
  6255. this.tail.next = head.next; // skip noop
  6256. this.tail = tail;
  6257. this.len += len;
  6258. }
  6259. return this;
  6260. };
  6261. /**
  6262. * Finishes the write operation.
  6263. * @returns {Uint8Array} Finished buffer
  6264. */
  6265. Writer.prototype.finish = function finish() {
  6266. var head = this.head.next, // skip noop
  6267. buf = this.constructor.alloc(this.len),
  6268. pos = 0;
  6269. while (head) {
  6270. head.fn(head.val, buf, pos);
  6271. pos += head.len;
  6272. head = head.next;
  6273. }
  6274. // this.head = this.tail = null;
  6275. return buf;
  6276. };
  6277. Writer._configure = function(BufferWriter_) {
  6278. BufferWriter = BufferWriter_;
  6279. };
  6280. },{"35":35}],39:[function(require,module,exports){
  6281. "use strict";
  6282. module.exports = BufferWriter;
  6283. // extends Writer
  6284. var Writer = require(38);
  6285. (BufferWriter.prototype = Object.create(Writer.prototype)).constructor = BufferWriter;
  6286. var util = require(35);
  6287. var Buffer = util.Buffer;
  6288. /**
  6289. * Constructs a new buffer writer instance.
  6290. * @classdesc Wire format writer using node buffers.
  6291. * @extends Writer
  6292. * @constructor
  6293. */
  6294. function BufferWriter() {
  6295. Writer.call(this);
  6296. }
  6297. /**
  6298. * Allocates a buffer of the specified size.
  6299. * @param {number} size Buffer size
  6300. * @returns {Buffer} Buffer
  6301. */
  6302. BufferWriter.alloc = function alloc_buffer(size) {
  6303. return (BufferWriter.alloc = util._Buffer_allocUnsafe)(size);
  6304. };
  6305. var writeBytesBuffer = Buffer && Buffer.prototype instanceof Uint8Array && Buffer.prototype.set.name === "set"
  6306. ? function writeBytesBuffer_set(val, buf, pos) {
  6307. buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
  6308. // also works for plain array values
  6309. }
  6310. /* istanbul ignore next */
  6311. : function writeBytesBuffer_copy(val, buf, pos) {
  6312. if (val.copy) // Buffer values
  6313. val.copy(buf, pos, 0, val.length);
  6314. else for (var i = 0; i < val.length;) // plain array values
  6315. buf[pos++] = val[i++];
  6316. };
  6317. /**
  6318. * @override
  6319. */
  6320. BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
  6321. if (util.isString(value))
  6322. value = util._Buffer_from(value, "base64");
  6323. var len = value.length >>> 0;
  6324. this.uint32(len);
  6325. if (len)
  6326. this._push(writeBytesBuffer, len, value);
  6327. return this;
  6328. };
  6329. function writeStringBuffer(val, buf, pos) {
  6330. if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
  6331. util.utf8.write(val, buf, pos);
  6332. else
  6333. buf.utf8Write(val, pos);
  6334. }
  6335. /**
  6336. * @override
  6337. */
  6338. BufferWriter.prototype.string = function write_string_buffer(value) {
  6339. var len = Buffer.byteLength(value);
  6340. this.uint32(len);
  6341. if (len)
  6342. this._push(writeStringBuffer, len, value);
  6343. return this;
  6344. };
  6345. /**
  6346. * Finishes the write operation.
  6347. * @name BufferWriter#finish
  6348. * @function
  6349. * @returns {Buffer} Finished buffer
  6350. */
  6351. },{"35":35,"38":38}]},{},[16])
  6352. })();
  6353. //# sourceMappingURL=protobuf.js.map