protobuf.js 225 KB

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