protobuf.js 217 KB

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