protobuf.js 224 KB

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