protobuf.js 219 KB

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