index.js 245 KB

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