protobuf.js 226 KB

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