protobuf.js 224 KB

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