1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336 |
- /*
- Copyright 2013 Daniel Wirtz <dcode@dcode.io>
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @license ProtoBuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
- * Released under the Apache License, Version 2.0
- * see: https://github.com/dcodeIO/ProtoBuf.js for details
- */
- (function(global) {
- "use strict";
- function init(ByteBuffer) {
- /**
- * The ProtoBuf namespace.
- * @exports ProtoBuf
- * @namespace
- * @expose
- */
- var ProtoBuf = {};
- /**
- * ProtoBuf.js version.
- * @type {string}
- * @const
- * @expose
- */
- ProtoBuf.VERSION = "3.8.0";
- /**
- * Wire types.
- * @type {Object.<string,number>}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES = {};
- /**
- * Varint wire type.
- * @type {number}
- * @expose
- */
- ProtoBuf.WIRE_TYPES.VARINT = 0;
- /**
- * Fixed 64 bits wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.BITS64 = 1;
- /**
- * Length delimited wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.LDELIM = 2;
- /**
- * Start group wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
- /**
- * End group wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
- /**
- * Fixed 32 bits wire type.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.WIRE_TYPES.BITS32 = 5;
- /**
- * Packable wire types.
- * @type {!Array.<number>}
- * @const
- * @expose
- */
- ProtoBuf.PACKABLE_WIRE_TYPES = [
- ProtoBuf.WIRE_TYPES.VARINT,
- ProtoBuf.WIRE_TYPES.BITS64,
- ProtoBuf.WIRE_TYPES.BITS32
- ];
- /**
- * Types.
- * @dict
- * @type {Object.<string,{name: string, wireType: number}>}
- * @const
- * @expose
- */
- ProtoBuf.TYPES = {
- // According to the protobuf spec.
- "int32": {
- name: "int32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "uint32": {
- name: "uint32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "sint32": {
- name: "sint32",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "int64": {
- name: "int64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "uint64": {
- name: "uint64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "sint64": {
- name: "sint64",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "bool": {
- name: "bool",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "double": {
- name: "double",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "string": {
- name: "string",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "bytes": {
- name: "bytes",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "fixed32": {
- name: "fixed32",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "sfixed32": {
- name: "sfixed32",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "fixed64": {
- name: "fixed64",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "sfixed64": {
- name: "sfixed64",
- wireType: ProtoBuf.WIRE_TYPES.BITS64
- },
- "float": {
- name: "float",
- wireType: ProtoBuf.WIRE_TYPES.BITS32
- },
- "enum": {
- name: "enum",
- wireType: ProtoBuf.WIRE_TYPES.VARINT
- },
- "message": {
- name: "message",
- wireType: ProtoBuf.WIRE_TYPES.LDELIM
- },
- "group": {
- name: "group",
- wireType: ProtoBuf.WIRE_TYPES.STARTGROUP
- }
- };
- /**
- * Minimum field id.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.ID_MIN = 1;
- /**
- * Maximum field id.
- * @type {number}
- * @const
- * @expose
- */
- ProtoBuf.ID_MAX = 0x1FFFFFFF;
- /**
- * @type {!function(new: ByteBuffer, ...[*])}
- * @expose
- */
- ProtoBuf.ByteBuffer = ByteBuffer;
- /**
- * @type {?function(new: Long, ...[*])}
- * @expose
- */
- ProtoBuf.Long = ByteBuffer.Long || null;
- /**
- * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
- * Must be set prior to parsing.
- * @type {boolean}
- * @expose
- */
- ProtoBuf.convertFieldsToCamelCase = false;
- /**
- * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
- * setting this to `false` prior to building messages.
- * @type {boolean}
- * @expose
- */
- ProtoBuf.populateAccessors = true;
- /**
- * @alias ProtoBuf.Util
- * @expose
- */
- ProtoBuf.Util = (function() {
- "use strict";
- // Object.create polyfill
- // ref: https://developer.mozilla.org/de/docs/JavaScript/Reference/Global_Objects/Object/create
- if (!Object.create)
- /** @expose */
- Object.create = function (o) {
- if (arguments.length > 1)
- throw Error('Object.create polyfill only accepts the first parameter.');
- function F() {}
- F.prototype = o;
- return new F();
- };
- /**
- * ProtoBuf utilities.
- * @exports ProtoBuf.Util
- * @namespace
- */
- var Util = {};
- /**
- * Flag if running in node (fs is available) or not.
- * @type {boolean}
- * @const
- * @expose
- */
- Util.IS_NODE = false;
- try {
- // There is no reliable way to detect node.js as an environment, so our
- // best bet is to feature-detect what we actually need.
- Util.IS_NODE =
- typeof require === 'function' &&
- typeof require("fs").readFileSync === 'function' &&
- typeof require("path").resolve === 'function';
- } catch (e) {}
- /**
- * Constructs a XMLHttpRequest object.
- * @return {XMLHttpRequest}
- * @throws {Error} If XMLHttpRequest is not supported
- * @expose
- */
- Util.XHR = function() {
- // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html
- var XMLHttpFactories = [
- function () {return new XMLHttpRequest()},
- function () {return new ActiveXObject("Msxml2.XMLHTTP")},
- function () {return new ActiveXObject("Msxml3.XMLHTTP")},
- function () {return new ActiveXObject("Microsoft.XMLHTTP")}
- ];
- /** @type {?XMLHttpRequest} */
- var xhr = null;
- for (var i=0;i<XMLHttpFactories.length;i++) {
- try { xhr = XMLHttpFactories[i](); }
- catch (e) { continue; }
- break;
- }
- if (!xhr)
- throw Error("XMLHttpRequest is not supported");
- return xhr;
- };
- /**
- * Fetches a resource.
- * @param {string} path Resource path
- * @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will
- * be fetched synchronously. If the request failed, contents will be null.
- * @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
- * @expose
- */
- Util.fetch = function(path, callback) {
- if (callback && typeof callback != 'function')
- callback = null;
- if (Util.IS_NODE) {
- if (callback) {
- require("fs").readFile(path, function(err, data) {
- if (err)
- callback(null);
- else
- callback(""+data);
- });
- } else
- try {
- return require("fs").readFileSync(path);
- } catch (e) {
- return null;
- }
- } else {
- var xhr = Util.XHR();
- xhr.open('GET', path, callback ? true : false);
- // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
- xhr.setRequestHeader('Accept', 'text/plain');
- if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
- if (callback) {
- xhr.onreadystatechange = function() {
- if (xhr.readyState != 4) return;
- if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
- callback(xhr.responseText);
- else
- callback(null);
- };
- if (xhr.readyState == 4)
- return;
- xhr.send(null);
- } else {
- xhr.send(null);
- if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
- return xhr.responseText;
- return null;
- }
- }
- };
- /**
- * Tests if an object is an array.
- * @function
- * @param {*} obj Object to test
- * @returns {boolean} true if it is an array, else false
- * @expose
- */
- Util.isArray = Array.isArray || function(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
- };
- return Util;
- })();
- /**
- * Language expressions.
- * @type {!Object.<string,string|!RegExp>}
- * @expose
- */
- ProtoBuf.Lang = {
- OPEN: "{",
- CLOSE: "}",
- OPTOPEN: "[",
- OPTCLOSE: "]",
- OPTEND: ",",
- EQUAL: "=",
- END: ";",
- STRINGOPEN: '"',
- STRINGCLOSE: '"',
- STRINGOPEN_SQ: "'",
- STRINGCLOSE_SQ: "'",
- COPTOPEN: '(',
- COPTCLOSE: ')',
- DELIM: /[\s\{\}=;\[\],'"\(\)]/g,
- // KEYWORD: /^(?:package|option|import|message|enum|extend|service|syntax|extensions|group)$/,
- RULE: /^(?:required|optional|repeated)$/,
- TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/,
- NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
- TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
- TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/,
- FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,
- NUMBER: /^-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+|([0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?))$/,
- NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/,
- NUMBER_HEX: /^0x[0-9a-fA-F]+$/,
- NUMBER_OCT: /^0[0-7]+$/,
- NUMBER_FLT: /^[0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?$/,
- ID: /^(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,
- NEGID: /^\-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,
- WHITESPACE: /\s/,
- STRING: /['"]([^'"\\]*(\\.[^"\\]*)*)['"]/g,
- BOOL: /^(?:true|false)$/i
- };
- /**
- * @alias ProtoBuf.DotProto
- * @expose
- */
- ProtoBuf.DotProto = (function(ProtoBuf, Lang) {
- "use strict";
- /**
- * Utilities to parse .proto files.
- * @exports ProtoBuf.DotProto
- * @namespace
- */
- var DotProto = {};
- /**
- * Constructs a new Tokenizer.
- * @exports ProtoBuf.DotProto.Tokenizer
- * @class prototype tokenizer
- * @param {string} proto Proto to tokenize
- * @constructor
- */
- var Tokenizer = function(proto) {
- /**
- * Source to parse.
- * @type {string}
- * @expose
- */
- this.source = ""+proto; // In case it's a buffer
- /**
- * Current index.
- * @type {number}
- * @expose
- */
- this.index = 0;
- /**
- * Current line.
- * @type {number}
- * @expose
- */
- this.line = 1;
- /**
- * Stacked values.
- * @type {Array}
- * @expose
- */
- this.stack = [];
- /**
- * Whether currently reading a string or not.
- * @type {boolean}
- * @expose
- */
- this.readingString = false;
- /**
- * Whatever character ends the string. Either a single or double quote character.
- * @type {string}
- * @expose
- */
- this.stringEndsWith = Lang.STRINGCLOSE;
- };
- /**
- * @alias ProtoBuf.DotProto.Tokenizer.prototype
- * @inner
- */
- var TokenizerPrototype = Tokenizer.prototype;
- /**
- * Reads a string beginning at the current index.
- * @return {string} The string
- * @throws {Error} If it's not a valid string
- * @private
- */
- TokenizerPrototype._readString = function() {
- Lang.STRING.lastIndex = this.index-1; // Include the open quote
- var match;
- if ((match = Lang.STRING.exec(this.source)) !== null) {
- var s = match[1];
- this.index = Lang.STRING.lastIndex;
- this.stack.push(this.stringEndsWith);
- return s;
- }
- throw Error("Unterminated string at line "+this.line+", index "+this.index);
- };
- /**
- * Gets the next token and advances by one.
- * @return {?string} Token or `null` on EOF
- * @throws {Error} If it's not a valid proto file
- * @expose
- */
- TokenizerPrototype.next = function() {
- if (this.stack.length > 0)
- return this.stack.shift();
- if (this.index >= this.source.length)
- return null; // No more tokens
- if (this.readingString) {
- this.readingString = false;
- return this._readString();
- }
- var repeat, last;
- do {
- repeat = false;
- // Strip white spaces
- while (Lang.WHITESPACE.test(last = this.source.charAt(this.index))) {
- this.index++;
- if (last === "\n")
- this.line++;
- if (this.index === this.source.length)
- return null;
- }
- // Strip comments
- if (this.source.charAt(this.index) === '/') {
- if (this.source.charAt(++this.index) === '/') { // Single line
- while (this.source.charAt(this.index) !== "\n") {
- this.index++;
- if (this.index == this.source.length)
- return null;
- }
- this.index++;
- this.line++;
- repeat = true;
- } else if (this.source.charAt(this.index) === '*') { /* Block */
- last = '';
- while (last+(last=this.source.charAt(this.index)) !== '*/') {
- this.index++;
- if (last === "\n")
- this.line++;
- if (this.index === this.source.length)
- return null;
- }
- this.index++;
- repeat = true;
- } else
- throw Error("Unterminated comment at line "+this.line+": /"+this.source.charAt(this.index));
- }
- } while (repeat);
- if (this.index === this.source.length) return null;
- // Read the next token
- var end = this.index;
- Lang.DELIM.lastIndex = 0;
- var delim = Lang.DELIM.test(this.source.charAt(end));
- if (!delim) {
- ++end;
- while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end)))
- end++;
- } else
- ++end;
- var token = this.source.substring(this.index, this.index = end);
- if (token === Lang.STRINGOPEN)
- this.readingString = true,
- this.stringEndsWith = Lang.STRINGCLOSE;
- else if (token === Lang.STRINGOPEN_SQ)
- this.readingString = true,
- this.stringEndsWith = Lang.STRINGCLOSE_SQ;
- return token;
- };
- /**
- * Peeks for the next token.
- * @return {?string} Token or `null` on EOF
- * @throws {Error} If it's not a valid proto file
- * @expose
- */
- TokenizerPrototype.peek = function() {
- if (this.stack.length === 0) {
- var token = this.next();
- if (token === null)
- return null;
- this.stack.push(token);
- }
- return this.stack[0];
- };
- /**
- * Returns a string representation of this object.
- * @return {string} String representation as of "Tokenizer(index/length)"
- * @expose
- */
- TokenizerPrototype.toString = function() {
- return "Tokenizer("+this.index+"/"+this.source.length+" at line "+this.line+")";
- };
- /**
- * @alias ProtoBuf.DotProto.Tokenizer
- * @expose
- */
- DotProto.Tokenizer = Tokenizer;
- /**
- * Constructs a new Parser.
- * @exports ProtoBuf.DotProto.Parser
- * @class prototype parser
- * @param {string} proto Protocol source
- * @constructor
- */
- var Parser = function(proto) {
- /**
- * Tokenizer.
- * @type {ProtoBuf.DotProto.Tokenizer}
- * @expose
- */
- this.tn = new Tokenizer(proto);
- };
- /**
- * @alias ProtoBuf.DotProto.Parser.prototype
- * @inner
- */
- var ParserPrototype = Parser.prototype;
- /**
- * Runs the parser.
- * @return {{package: string|null, messages: Array.<object>, enums: Array.<object>, imports: Array.<string>, options: object<string,*>}}
- * @throws {Error} If the source cannot be parsed
- * @expose
- */
- ParserPrototype.parse = function() {
- var topLevel = {
- "name": "[ROOT]", // temporary
- "package": null,
- "messages": [],
- "enums": [],
- "imports": [],
- "options": {},
- "services": []
- };
- var token, head = true;
- while(token = this.tn.next()) {
- switch (token) {
- case 'package':
- if (!head || topLevel["package"] !== null)
- throw Error("Unexpected package at line "+this.tn.line);
- topLevel["package"] = this._parsePackage(token);
- break;
- case 'import':
- if (!head)
- throw Error("Unexpected import at line "+this.tn.line);
- topLevel.imports.push(this._parseImport(token));
- break;
- case 'message':
- this._parseMessage(topLevel, null, token);
- head = false;
- break;
- case 'enum':
- this._parseEnum(topLevel, token);
- head = false;
- break;
- case 'option':
- if (!head)
- throw Error("Unexpected option at line "+this.tn.line);
- this._parseOption(topLevel, token);
- break;
- case 'service':
- this._parseService(topLevel, token);
- break;
- case 'extend':
- this._parseExtend(topLevel, token);
- break;
- case 'syntax':
- this._parseIgnoredStatement(topLevel, token);
- break;
- default:
- throw Error("Unexpected token at line "+this.tn.line+": "+token);
- }
- }
- delete topLevel["name"];
- return topLevel;
- };
- /**
- * Parses a number value.
- * @param {string} val Number value to parse
- * @return {number} Number
- * @throws {Error} If the number value is invalid
- * @private
- */
- ParserPrototype._parseNumber = function(val) {
- var sign = 1;
- if (val.charAt(0) == '-')
- sign = -1,
- val = val.substring(1);
- if (Lang.NUMBER_DEC.test(val))
- return sign*parseInt(val, 10);
- else if (Lang.NUMBER_HEX.test(val))
- return sign*parseInt(val.substring(2), 16);
- else if (Lang.NUMBER_OCT.test(val))
- return sign*parseInt(val.substring(1), 8);
- else if (Lang.NUMBER_FLT.test(val))
- return sign*parseFloat(val);
- throw Error("Illegal number at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- };
- /**
- * Parses a (possibly multiline) string.
- * @returns {string}
- * @private
- */
- ParserPrototype._parseString = function() {
- var value = "", token;
- do {
- token = this.tn.next(); // Known to be = this.tn.stringEndsWith
- value += this.tn.next();
- token = this.tn.next();
- if (token !== this.tn.stringEndsWith)
- throw Error("Illegal end of string at line "+this.tn.line+": "+token);
- token = this.tn.peek();
- } while (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ);
- return value;
- };
- /**
- * Parses an ID value.
- * @param {string} val ID value to parse
- * @param {boolean=} neg Whether the ID may be negative, defaults to `false`
- * @returns {number} ID
- * @throws {Error} If the ID value is invalid
- * @private
- */
- ParserPrototype._parseId = function(val, neg) {
- var id = -1;
- var sign = 1;
- if (val.charAt(0) == '-')
- sign = -1,
- val = val.substring(1);
- if (Lang.NUMBER_DEC.test(val))
- id = parseInt(val);
- else if (Lang.NUMBER_HEX.test(val))
- id = parseInt(val.substring(2), 16);
- else if (Lang.NUMBER_OCT.test(val))
- id = parseInt(val.substring(1), 8);
- else
- throw Error("Illegal id at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- id = (sign*id)|0; // Force to 32bit
- if (!neg && id < 0)
- throw Error("Illegal id at line "+this.tn.line+": "+(sign < 0 ? '-' : '')+val);
- return id;
- };
- /**
- * Parses the package definition.
- * @param {string} token Initial token
- * @return {string} Package name
- * @throws {Error} If the package definition cannot be parsed
- * @private
- */
- ParserPrototype._parsePackage = function(token) {
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal package name at line "+this.tn.line+": "+token);
- var pkg = token;
- token = this.tn.next();
- if (token != Lang.END)
- throw Error("Illegal end of package at line "+this.tn.line+": "+token);
- return pkg;
- };
- /**
- * Parses an import definition.
- * @param {string} token Initial token
- * @return {string} Import file name
- * @throws {Error} If the import definition cannot be parsed
- * @private
- */
- ParserPrototype._parseImport = function(token) {
- token = this.tn.peek();
- if (token === "public")
- this.tn.next(),
- token = this.tn.peek();
- if (token !== Lang.STRINGOPEN && token !== Lang.STRINGOPEN_SQ)
- throw Error("Illegal start of import at line "+this.tn.line+": "+token);
- var imported = this._parseString();
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal end of import at line "+this.tn.line+": "+token);
- return imported;
- };
- /**
- * Parses a namespace option.
- * @param {Object} parent Parent definition
- * @param {string} token Initial token
- * @throws {Error} If the option cannot be parsed
- * @private
- */
- ParserPrototype._parseOption = function(parent, token) {
- token = this.tn.next();
- var custom = false;
- if (token == Lang.COPTOPEN)
- custom = true,
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- // we can allow options of the form google.protobuf.* since they will just get ignored anyways
- if (!/google\.protobuf\./.test(token))
- throw Error("Illegal option name in message "+parent.name+" at line "+this.tn.line+": "+token);
- var name = token;
- token = this.tn.next();
- if (custom) { // (my_method_option).foo, (my_method_option), some_method_option, (foo.my_option).bar
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal end in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- name = '('+name+')';
- token = this.tn.next();
- if (Lang.FQTYPEREF.test(token))
- name += token,
- token = this.tn.next();
- }
- if (token !== Lang.EQUAL)
- throw Error("Illegal operator in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- var value;
- token = this.tn.peek();
- if (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ)
- value = this._parseString();
- else {
- this.tn.next();
- if (Lang.NUMBER.test(token))
- value = this._parseNumber(token, true);
- else if (Lang.BOOL.test(token))
- value = token === 'true';
- else if (Lang.TYPEREF.test(token))
- value = token;
- else
- throw Error("Illegal option value in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- }
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal end of option in message "+parent.name+", option "+name+" at line "+this.tn.line+": "+token);
- parent["options"][name] = value;
- };
- /**
- * Parses an ignored statement of the form ['keyword', ..., ';'].
- * @param {Object} parent Parent definition
- * @param {string} keyword Initial token
- * @throws {Error} If the directive cannot be parsed
- * @private
- */
- ParserPrototype._parseIgnoredStatement = function(parent, keyword) {
- var token;
- do {
- token = this.tn.next();
- if (token === null)
- throw Error("Unexpected EOF in "+parent.name+", "+keyword+" at line "+this.tn.line);
- if (token === Lang.END)
- break;
- } while (true);
- };
- /**
- * Parses a service definition.
- * @param {Object} parent Parent definition
- * @param {string} token Initial token
- * @throws {Error} If the service cannot be parsed
- * @private
- */
- ParserPrototype._parseService = function(parent, token) {
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal service name at line "+this.tn.line+": "+token);
- var name = token;
- var svc = {
- "name": name,
- "rpc": {},
- "options": {}
- };
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of service "+name+" at line "+this.tn.line+": "+token);
- do {
- token = this.tn.next();
- if (token === "option")
- this._parseOption(svc, token);
- else if (token === 'rpc')
- this._parseServiceRPC(svc, token);
- else if (token !== Lang.CLOSE)
- throw Error("Illegal type of service "+name+" at line "+this.tn.line+": "+token);
- } while (token !== Lang.CLOSE);
- parent["services"].push(svc);
- };
- /**
- * Parses a RPC service definition of the form ['rpc', name, (request), 'returns', (response)].
- * @param {Object} svc Parent definition
- * @param {string} token Initial token
- * @private
- */
- ParserPrototype._parseServiceRPC = function(svc, token) {
- var type = token;
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal method name in service "+svc["name"]+" at line "+this.tn.line+": "+token);
- var name = token;
- var method = {
- "request": null,
- "response": null,
- "options": {}
- };
- token = this.tn.next();
- if (token !== Lang.COPTOPEN)
- throw Error("Illegal start of request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- method["request"] = token;
- token = this.tn.next();
- if (token != Lang.COPTCLOSE)
- throw Error("Illegal end of request type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token.toLowerCase() !== "returns")
- throw Error("Illegal delimiter in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token != Lang.COPTOPEN)
- throw Error("Illegal start of response type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- method["response"] = token;
- token = this.tn.next();
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal end of response type in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token === Lang.OPEN) {
- do {
- token = this.tn.next();
- if (token === 'option')
- this._parseOption(method, token); // <- will fail for the custom-options example
- else if (token !== Lang.CLOSE)
- throw Error("Illegal start of option inservice "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- } while (token !== Lang.CLOSE);
- if (this.tn.peek() === Lang.END)
- this.tn.next();
- } else if (token !== Lang.END)
- throw Error("Illegal delimiter in service "+svc["name"]+"#"+name+" at line "+this.tn.line+": "+token);
- if (typeof svc[type] === 'undefined')
- svc[type] = {};
- svc[type][name] = method;
- };
- /**
- * Parses a message definition.
- * @param {Object} parent Parent definition
- * @param {Object} fld Field definition if this is a group, otherwise `null`
- * @param {string} token First token
- * @return {Object}
- * @throws {Error} If the message cannot be parsed
- * @private
- */
- ParserPrototype._parseMessage = function(parent, fld, token) {
- /** @dict */
- var msg = {}; // Note: At some point we might want to exclude the parser, so we need a dict.
- var isGroup = token === "group";
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal "+(isGroup ? "group" : "message")+" name"+(parent ? " in message "+parent["name"] : "")+" at line "+this.tn.line+": "+token);
- msg["name"] = token;
- if (isGroup) {
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal id assignment after group "+msg.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- fld["id"] = this._parseId(token);
- } catch (e) {
- throw Error("Illegal field id value for group "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- msg["isGroup"] = true;
- }
- msg["fields"] = []; // Note: Using arrays to support also browser that cannot preserve order of object keys.
- msg["enums"] = [];
- msg["messages"] = [];
- msg["options"] = {};
- msg["oneofs"] = {};
- token = this.tn.next();
- if (token === Lang.OPTOPEN && fld)
- this._parseFieldOptions(msg, fld, token),
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of "+(isGroup ? "group" : "message")+" "+msg.name+" at line "+this.tn.line+": "+token);
- // msg["extensions"] = undefined
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- break;
- } else if (Lang.RULE.test(token))
- this._parseMessageField(msg, token);
- else if (token === "oneof")
- this._parseMessageOneOf(msg, token);
- else if (token === "enum")
- this._parseEnum(msg, token);
- else if (token === "message")
- this._parseMessage(msg, null, token);
- else if (token === "option")
- this._parseOption(msg, token);
- else if (token === "extensions")
- msg["extensions"] = this._parseExtensions(msg, token);
- else if (token === "extend")
- this._parseExtend(msg, token);
- else
- throw Error("Illegal token in message "+msg.name+" at line "+this.tn.line+": "+token);
- } while (true);
- parent["messages"].push(msg);
- return msg;
- };
- /**
- * Parses a message field.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @returns {!Object} Field descriptor
- * @throws {Error} If the message field cannot be parsed
- * @private
- */
- ParserPrototype._parseMessageField = function(msg, token) {
- /** @dict */
- var fld = {}, grp = null;
- fld["rule"] = token;
- /** @dict */
- fld["options"] = {};
- token = this.tn.next();
- if (token === "group") {
- // "A [legacy] group simply combines a nested message type and a field into a single declaration. In your
- // code, you can treat this message just as if it had a Result type field called result (the latter name is
- // converted to lower-case so that it does not conflict with the former)."
- grp = this._parseMessage(msg, fld, token);
- if (!/^[A-Z]/.test(grp["name"]))
- throw Error('Group names must start with a capital letter');
- fld["type"] = grp["name"];
- fld["name"] = grp["name"].toLowerCase();
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- } else {
- if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
- throw Error("Illegal field type in message "+msg.name+" at line "+this.tn.line+": "+token);
- fld["type"] = token;
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal field name in message "+msg.name+" at line "+this.tn.line+": "+token);
- fld["name"] = token;
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in field "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- fld["id"] = this._parseId(token);
- } catch (e) {
- throw Error("Illegal field id in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- token = this.tn.next();
- if (token === Lang.OPTOPEN)
- this._parseFieldOptions(msg, fld, token),
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal delimiter in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- }
- msg["fields"].push(fld);
- return fld;
- };
- /**
- * Parses a message oneof.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @throws {Error} If the message oneof cannot be parsed
- * @private
- */
- ParserPrototype._parseMessageOneOf = function(msg, token) {
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal oneof name in message "+msg.name+" at line "+this.tn.line+": "+token);
- var name = token,
- fld;
- var fields = [];
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of oneof "+name+" at line "+this.tn.line+": "+token);
- while (this.tn.peek() !== Lang.CLOSE) {
- fld = this._parseMessageField(msg, "optional");
- fld["oneof"] = name;
- fields.push(fld["id"]);
- }
- this.tn.next();
- msg["oneofs"][name] = fields;
- };
- /**
- * Parses a set of field option definitions.
- * @param {Object} msg Message definition
- * @param {Object} fld Field definition
- * @param {string} token Initial token
- * @throws {Error} If the message field options cannot be parsed
- * @private
- */
- ParserPrototype._parseFieldOptions = function(msg, fld, token) {
- var first = true;
- do {
- token = this.tn.next();
- if (token === Lang.OPTCLOSE)
- break;
- else if (token === Lang.OPTEND) {
- if (first)
- throw Error("Illegal start of options in message "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- }
- this._parseFieldOption(msg, fld, token);
- first = false;
- } while (true);
- };
- /**
- * Parses a single field option.
- * @param {Object} msg Message definition
- * @param {Object} fld Field definition
- * @param {string} token Initial token
- * @throws {Error} If the mesage field option cannot be parsed
- * @private
- */
- ParserPrototype._parseFieldOption = function(msg, fld, token) {
- var custom = false;
- if (token === Lang.COPTOPEN)
- token = this.tn.next(),
- custom = true;
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal field option in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- var name = token;
- token = this.tn.next();
- if (custom) {
- if (token !== Lang.COPTCLOSE)
- throw Error("Illegal delimiter in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- name = '('+name+')';
- token = this.tn.next();
- if (Lang.FQTYPEREF.test(token))
- name += token,
- token = this.tn.next();
- }
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in "+msg.name+"#"+fld.name+" at line "+this.tn.line+": "+token);
- var value;
- token = this.tn.peek();
- if (token === Lang.STRINGOPEN || token === Lang.STRINGOPEN_SQ) {
- value = this._parseString();
- } else if (Lang.NUMBER.test(token, true))
- value = this._parseNumber(this.tn.next(), true);
- else if (Lang.BOOL.test(token))
- value = this.tn.next().toLowerCase() === 'true';
- else if (Lang.TYPEREF.test(token))
- value = this.tn.next(); // TODO: Resolve?
- else
- throw Error("Illegal value in message "+msg.name+"#"+fld.name+", option "+name+" at line "+this.tn.line+": "+token);
- fld["options"][name] = value;
- };
- /**
- * Parses an enum.
- * @param {Object} msg Message definition
- * @param {string} token Initial token
- * @throws {Error} If the enum cannot be parsed
- * @private
- */
- ParserPrototype._parseEnum = function(msg, token) {
- /** @dict */
- var enm = {};
- token = this.tn.next();
- if (!Lang.NAME.test(token))
- throw Error("Illegal enum name in message "+msg.name+" at line "+this.tn.line+": "+token);
- enm["name"] = token;
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of enum "+enm.name+" at line "+this.tn.line+": "+token);
- enm["values"] = [];
- enm["options"] = {};
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token === Lang.END)
- this.tn.next();
- break;
- }
- if (token == 'option')
- this._parseOption(enm, token);
- else {
- if (!Lang.NAME.test(token))
- throw Error("Illegal name in enum "+enm.name+" at line "+this.tn.line+": "+token);
- this._parseEnumValue(enm, token);
- }
- } while (true);
- msg["enums"].push(enm);
- };
- /**
- * Parses an enum value.
- * @param {Object} enm Enum definition
- * @param {string} token Initial token
- * @throws {Error} If the enum value cannot be parsed
- * @private
- */
- ParserPrototype._parseEnumValue = function(enm, token) {
- /** @dict */
- var val = {};
- val["name"] = token;
- token = this.tn.next();
- if (token !== Lang.EQUAL)
- throw Error("Illegal token in enum "+enm.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- try {
- val["id"] = this._parseId(token, true);
- } catch (e) {
- throw Error("Illegal id in enum "+enm.name+" at line "+this.tn.line+": "+token);
- }
- enm["values"].push(val);
- token = this.tn.next();
- if (token === Lang.OPTOPEN) {
- var opt = { 'options' : {} }; // TODO: Actually expose them somehow.
- this._parseFieldOptions(enm, opt, token);
- token = this.tn.next();
- }
- if (token !== Lang.END)
- throw Error("Illegal delimiter in enum "+enm.name+" at line "+this.tn.line+": "+token);
- };
- /**
- * Parses an extensions statement.
- * @param {Object} msg Message object
- * @param {string} token Initial token
- * @throws {Error} If the extensions statement cannot be parsed
- * @private
- */
- ParserPrototype._parseExtensions = function(msg, token) {
- /** @type {Array.<number>} */
- var range = [];
- token = this.tn.next();
- if (token === "min") // FIXME: Does the official implementation support this?
- range.push(ProtoBuf.ID_MIN);
- else if (token === "max")
- range.push(ProtoBuf.ID_MAX);
- else
- range.push(this._parseNumber(token));
- token = this.tn.next();
- if (token !== 'to')
- throw Error("Illegal extensions delimiter in message "+msg.name+" at line "+this.tn.line+": "+token);
- token = this.tn.next();
- if (token === "min")
- range.push(ProtoBuf.ID_MIN);
- else if (token === "max")
- range.push(ProtoBuf.ID_MAX);
- else
- range.push(this._parseNumber(token));
- token = this.tn.next();
- if (token !== Lang.END)
- throw Error("Illegal extensions delimiter in message "+msg.name+" at line "+this.tn.line+": "+token);
- return range;
- };
- /**
- * Parses an extend block.
- * @param {Object} parent Parent object
- * @param {string} token Initial token
- * @throws {Error} If the extend block cannot be parsed
- * @private
- */
- ParserPrototype._parseExtend = function(parent, token) {
- token = this.tn.next();
- if (!Lang.TYPEREF.test(token))
- throw Error("Illegal message name at line "+this.tn.line+": "+token);
- /** @dict */
- var ext = {};
- ext["ref"] = token;
- ext["fields"] = [];
- token = this.tn.next();
- if (token !== Lang.OPEN)
- throw Error("Illegal start of extend "+ext.name+" at line "+this.tn.line+": "+token);
- do {
- token = this.tn.next();
- if (token === Lang.CLOSE) {
- token = this.tn.peek();
- if (token == Lang.END)
- this.tn.next();
- break;
- } else if (Lang.RULE.test(token))
- this._parseMessageField(ext, token);
- else
- throw Error("Illegal token in extend "+ext.name+" at line "+this.tn.line+": "+token);
- } while (true);
- parent["messages"].push(ext);
- return ext;
- };
- /**
- * Returns a string representation of this object.
- * @returns {string} String representation as of "Parser"
- */
- ParserPrototype.toString = function() {
- return "Parser";
- };
- /**
- * @alias ProtoBuf.DotProto.Parser
- * @expose
- */
- DotProto.Parser = Parser;
- return DotProto;
- })(ProtoBuf, ProtoBuf.Lang);
- /**
- * @alias ProtoBuf.Reflect
- * @expose
- */
- ProtoBuf.Reflect = (function(ProtoBuf) {
- "use strict";
- /**
- * Reflection types.
- * @exports ProtoBuf.Reflect
- * @namespace
- */
- var Reflect = {};
- /**
- * Constructs a Reflect base class.
- * @exports ProtoBuf.Reflect.T
- * @constructor
- * @abstract
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {?ProtoBuf.Reflect.T} parent Parent object
- * @param {string} name Object name
- */
- var T = function(builder, parent, name) {
- /**
- * Builder reference.
- * @type {!ProtoBuf.Builder}
- * @expose
- */
- this.builder = builder;
- /**
- * Parent object.
- * @type {?ProtoBuf.Reflect.T}
- * @expose
- */
- this.parent = parent;
- /**
- * Object name in namespace.
- * @type {string}
- * @expose
- */
- this.name = name;
- /**
- * Fully qualified class name
- * @type {string}
- * @expose
- */
- this.className;
- };
- /**
- * @alias ProtoBuf.Reflect.T.prototype
- * @inner
- */
- var TPrototype = T.prototype;
- /**
- * Returns the fully qualified name of this object.
- * @returns {string} Fully qualified name as of ".PATH.TO.THIS"
- * @expose
- */
- TPrototype.fqn = function() {
- var name = this.name,
- ptr = this;
- do {
- ptr = ptr.parent;
- if (ptr == null)
- break;
- name = ptr.name+"."+name;
- } while (true);
- return name;
- };
- /**
- * Returns a string representation of this Reflect object (its fully qualified name).
- * @param {boolean=} includeClass Set to true to include the class name. Defaults to false.
- * @return String representation
- * @expose
- */
- TPrototype.toString = function(includeClass) {
- return (includeClass ? this.className + " " : "") + this.fqn();
- };
- /**
- * Builds this type.
- * @throws {Error} If this type cannot be built directly
- * @expose
- */
- TPrototype.build = function() {
- throw Error(this.toString(true)+" cannot be built directly");
- };
- /**
- * @alias ProtoBuf.Reflect.T
- * @expose
- */
- Reflect.T = T;
- /**
- * Constructs a new Namespace.
- * @exports ProtoBuf.Reflect.Namespace
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent
- * @param {string} name Namespace name
- * @param {Object.<string,*>=} options Namespace options
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Namespace = function(builder, parent, name, options) {
- T.call(this, builder, parent, name);
- /**
- * @override
- */
- this.className = "Namespace";
- /**
- * Children inside the namespace.
- * @type {!Array.<ProtoBuf.Reflect.T>}
- */
- this.children = [];
- /**
- * Options.
- * @type {!Object.<string, *>}
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Reflect.Namespace.prototype
- * @inner
- */
- var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);
- /**
- * Returns an array of the namespace's children.
- * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).
- * @return {Array.<ProtoBuf.Reflect.T>}
- * @expose
- */
- NamespacePrototype.getChildren = function(type) {
- type = type || null;
- if (type == null)
- return this.children.slice();
- var children = [];
- for (var i=0, k=this.children.length; i<k; ++i)
- if (this.children[i] instanceof type)
- children.push(this.children[i]);
- return children;
- };
- /**
- * Adds a child to the namespace.
- * @param {ProtoBuf.Reflect.T} child Child
- * @throws {Error} If the child cannot be added (duplicate)
- * @expose
- */
- NamespacePrototype.addChild = function(child) {
- var other;
- if (other = this.getChild(child.name)) {
- // Try to revert camelcase transformation on collision
- if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)
- other.name = other.originalName; // Revert previous first (effectively keeps both originals)
- else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)
- child.name = child.originalName;
- else
- throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);
- }
- this.children.push(child);
- };
- /**
- * Gets a child by its name or id.
- * @param {string|number} nameOrId Child name or id
- * @return {?ProtoBuf.Reflect.T} The child or null if not found
- * @expose
- */
- NamespacePrototype.getChild = function(nameOrId) {
- var key = typeof nameOrId === 'number' ? 'id' : 'name';
- for (var i=0, k=this.children.length; i<k; ++i)
- if (this.children[i][key] === nameOrId)
- return this.children[i];
- return null;
- };
- /**
- * Resolves a reflect object inside of this namespace.
- * @param {string} qn Qualified name to resolve
- * @param {boolean=} excludeFields Excludes fields, defaults to `false`
- * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found
- * @expose
- */
- NamespacePrototype.resolve = function(qn, excludeFields) {
- var part = qn.split("."),
- ptr = this,
- i = 0;
- if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'
- while (ptr.parent !== null)
- ptr = ptr.parent;
- i++;
- }
- var child;
- do {
- do {
- child = ptr.getChild(part[i]);
- if (!child || !(child instanceof Reflect.T) || (excludeFields && child instanceof Reflect.Message.Field)) {
- ptr = null;
- break;
- }
- ptr = child; i++;
- } while (i < part.length);
- if (ptr != null)
- break; // Found
- // Else search the parent
- if (this.parent !== null) {
- return this.parent.resolve(qn, excludeFields);
- }
- } while (ptr != null);
- return ptr;
- };
- /**
- * Builds the namespace and returns the runtime counterpart.
- * @return {Object.<string,Function|Object>} Runtime namespace
- * @expose
- */
- NamespacePrototype.build = function() {
- /** @dict */
- var ns = {};
- var children = this.children;
- for (var i=0, k=children.length, child; i<k; ++i) {
- child = children[i];
- if (child instanceof Namespace)
- ns[child.name] = child.build();
- }
- if (Object.defineProperty)
- Object.defineProperty(ns, "$options", { "value": this.buildOpt() });
- return ns;
- };
- /**
- * Builds the namespace's '$options' property.
- * @return {Object.<string,*>}
- */
- NamespacePrototype.buildOpt = function() {
- var opt = {},
- keys = Object.keys(this.options);
- for (var i=0, k=keys.length; i<k; ++i) {
- var key = keys[i],
- val = this.options[keys[i]];
- // TODO: Options are not resolved, yet.
- // if (val instanceof Namespace) {
- // opt[key] = val.build();
- // } else {
- opt[key] = val;
- // }
- }
- return opt;
- };
- /**
- * Gets the value assigned to the option with the specified name.
- * @param {string=} name Returns the option value if specified, otherwise all options are returned.
- * @return {*|Object.<string,*>}null} Option value or NULL if there is no such option
- */
- NamespacePrototype.getOption = function(name) {
- if (typeof name === 'undefined')
- return this.options;
- return typeof this.options[name] !== 'undefined' ? this.options[name] : null;
- };
- /**
- * @alias ProtoBuf.Reflect.Namespace
- * @expose
- */
- Reflect.Namespace = Namespace;
- /**
- * Constructs a new Message.
- * @exports ProtoBuf.Reflect.Message
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Namespace} parent Parent message or namespace
- * @param {string} name Message name
- * @param {Object.<string,*>=} options Message options
- * @param {boolean=} isGroup `true` if this is a legacy group
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Message = function(builder, parent, name, options, isGroup) {
- Namespace.call(this, builder, parent, name, options);
- /**
- * @override
- */
- this.className = "Message";
- /**
- * Extensions range.
- * @type {!Array.<number>}
- * @expose
- */
- this.extensions = [ProtoBuf.ID_MIN, ProtoBuf.ID_MAX];
- /**
- * Runtime message class.
- * @type {?function(new:ProtoBuf.Builder.Message)}
- * @expose
- */
- this.clazz = null;
- /**
- * Whether this is a legacy group or not.
- * @type {boolean}
- * @expose
- */
- this.isGroup = !!isGroup;
- // The following cached collections are used to efficiently iterate over or look up fields when decoding.
- /**
- * Cached fields.
- * @type {?Array.<!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fields = null;
- /**
- * Cached fields by id.
- * @type {?Object.<number,!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fieldsById = null;
- /**
- * Cached fields by name.
- * @type {?Object.<string,!ProtoBuf.Reflect.Message.Field>}
- * @private
- */
- this._fieldsByName = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Message.prototype
- * @inner
- */
- var MessagePrototype = Message.prototype = Object.create(Namespace.prototype);
- /**
- * Builds the message and returns the runtime counterpart, which is a fully functional class.
- * @see ProtoBuf.Builder.Message
- * @param {boolean=} rebuild Whether to rebuild or not, defaults to false
- * @return {ProtoBuf.Reflect.Message} Message class
- * @throws {Error} If the message cannot be built
- * @expose
- */
- MessagePrototype.build = function(rebuild) {
- if (this.clazz && !rebuild)
- return this.clazz;
- // Create the runtime Message class in its own scope
- var clazz = (function(ProtoBuf, T) {
- var fields = T.getChildren(ProtoBuf.Reflect.Message.Field),
- oneofs = T.getChildren(ProtoBuf.Reflect.Message.OneOf);
- /**
- * Constructs a new runtime Message.
- * @name ProtoBuf.Builder.Message
- * @class Barebone of all runtime messages.
- * @param {!Object.<string,*>|string} values Preset values
- * @param {...string} var_args
- * @constructor
- * @throws {Error} If the message cannot be created
- */
- var Message = function(values, var_args) {
- ProtoBuf.Builder.Message.call(this);
- // Create virtual oneof properties
- for (var i=0, k=oneofs.length; i<k; ++i)
- this[oneofs[i].name] = null;
- // Create fields and set default values
- for (i=0, k=fields.length; i<k; ++i) {
- var field = fields[i];
- this[field.name] = field.repeated ? [] : null;
- if (field.required && field.defaultValue !== null)
- this[field.name] = field.defaultValue;
- }
- if (arguments.length > 0) {
- // Set field values from a values object
- if (arguments.length === 1 && typeof values === 'object' &&
- /* not another Message */ typeof values.encode !== 'function' &&
- /* not a repeated field */ !ProtoBuf.Util.isArray(values) &&
- /* not a ByteBuffer */ !(values instanceof ByteBuffer) &&
- /* not an ArrayBuffer */ !(values instanceof ArrayBuffer) &&
- /* not a Long */ !(ProtoBuf.Long && values instanceof ProtoBuf.Long)) {
- var keys = Object.keys(values);
- for (i=0, k=keys.length; i<k; ++i)
- this.$set(keys[i], values[keys[i]]); // May throw
- } else // Set field values from arguments, in declaration order
- for (i=0, k=arguments.length; i<k; ++i)
- this.$set(fields[i].name, arguments[i]); // May throw
- }
- };
- /**
- * @alias ProtoBuf.Builder.Message.prototype
- * @inner
- */
- var MessagePrototype = Message.prototype = Object.create(ProtoBuf.Builder.Message.prototype);
- /**
- * Adds a value to a repeated field.
- * @name ProtoBuf.Builder.Message#add
- * @function
- * @param {string} key Field name
- * @param {*} value Value to add
- * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
- * @throws {Error} If the value cannot be added
- * @expose
- */
- MessagePrototype.add = function(key, value, noAssert) {
- var field = T._fieldsByName[key];
- if (!noAssert) {
- if (!field)
- throw Error(this+"#"+key+" is undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true)); // May throw if it's an enum or embedded message
- if (!field.repeated)
- throw Error(this+"#"+key+" is not a repeated field");
- }
- if (this[field.name] === null)
- this[field.name] = [];
- this[field.name].push(noAssert ? value : field.verifyValue(value, true));
- };
- /**
- * Adds a value to a repeated field. This is an alias for {@link ProtoBuf.Builder.Message#add}.
- * @name ProtoBuf.Builder.Message#$add
- * @function
- * @param {string} key Field name
- * @param {*} value Value to add
- * @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
- * @throws {Error} If the value cannot be added
- * @expose
- */
- MessagePrototype.$add = MessagePrototype.add;
- /**
- * Sets a field's value.
- * @name ProtoBuf.Builder.Message#set
- * @function
- * @param {string} key Key
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert for an actual field / proper value type, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @throws {Error} If the value cannot be set
- * @expose
- */
- MessagePrototype.set = function(key, value, noAssert) {
- if (key && typeof key === 'object') {
- for (var i in key)
- if (key.hasOwnProperty(i))
- this.$set(i, key[i], noAssert);
- return this;
- }
- var field = T._fieldsByName[key];
- if (!noAssert) {
- if (!field)
- throw Error(this+"#"+key+" is not a field: undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true));
- this[field.name] = (value = field.verifyValue(value)); // May throw
- } else {
- this[field.name] = value;
- }
- if (field.oneof) {
- if (value !== null) {
- if (this[field.oneof.name] !== null)
- this[this[field.oneof.name]] = null; // Unset the previous (field name is the oneof field's value)
- this[field.oneof.name] = field.name;
- } else if (field.oneof.name === key)
- this[field.oneof.name] = null;
- }
- return this;
- };
- /**
- * Sets a field's value. This is an alias for [@link ProtoBuf.Builder.Message#set}.
- * @name ProtoBuf.Builder.Message#$set
- * @function
- * @param {string} key Key
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @throws {Error} If the value cannot be set
- * @expose
- */
- MessagePrototype.$set = MessagePrototype.set;
- /**
- * Gets a field's value.
- * @name ProtoBuf.Builder.Message#get
- * @function
- * @param {string} key Key
- * @param {boolean=} noAssert Whether to not assert for an actual field, defaults to `false`
- * @return {*} Value
- * @throws {Error} If there is no such field
- * @expose
- */
- MessagePrototype.get = function(key, noAssert) {
- if (noAssert)
- return this[key];
- var field = T._fieldsByName[key];
- if (!field || !(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: undefined");
- if (!(field instanceof ProtoBuf.Reflect.Message.Field))
- throw Error(this+"#"+key+" is not a field: "+field.toString(true));
- return this[field.name];
- };
- /**
- * Gets a field's value. This is an alias for {@link ProtoBuf.Builder.Message#$get}.
- * @name ProtoBuf.Builder.Message#$get
- * @function
- * @param {string} key Key
- * @return {*} Value
- * @throws {Error} If there is no such field
- * @expose
- */
- MessagePrototype.$get = MessagePrototype.get;
- // Getters and setters
- for (var i=0; i<fields.length; i++) {
- var field = fields[i];
- // no setters for extension fields as these are named by their fqn
- if (field instanceof ProtoBuf.Reflect.Message.ExtensionField)
- continue;
- if (T.builder.options['populateAccessors'])
- (function(field) {
- // set/get[SomeValue]
- var Name = field.originalName.replace(/(_[a-zA-Z])/g, function(match) {
- return match.toUpperCase().replace('_','');
- });
- Name = Name.substring(0,1).toUpperCase() + Name.substring(1);
- // set/get_[some_value] FIXME: Do we really need these?
- var name = field.originalName.replace(/([A-Z])/g, function(match) {
- return "_"+match;
- });
- /**
- * The current field's unbound setter function.
- * @function
- * @param {*} value
- * @param {boolean=} noAssert
- * @returns {!ProtoBuf.Builder.Message}
- * @inner
- */
- var setter = function(value, noAssert) {
- this[field.name] = noAssert ? value : field.verifyValue(value);
- return this;
- };
- /**
- * The current field's unbound getter function.
- * @function
- * @returns {*}
- * @inner
- */
- var getter = function() {
- return this[field.name];
- };
- /**
- * Sets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#set[SomeField]
- * @function
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @abstract
- * @throws {Error} If the value cannot be set
- */
- if (T.getChild("set"+Name) === null)
- MessagePrototype["set"+Name] = setter;
- /**
- * Sets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#set_[some_field]
- * @function
- * @param {*} value Value to set
- * @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
- * @returns {!ProtoBuf.Builder.Message} this
- * @abstract
- * @throws {Error} If the value cannot be set
- */
- if (T.getChild("set_"+name) === null)
- MessagePrototype["set_"+name] = setter;
- /**
- * Gets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#get[SomeField]
- * @function
- * @abstract
- * @return {*} The value
- */
- if (T.getChild("get"+Name) === null)
- MessagePrototype["get"+Name] = getter;
- /**
- * Gets a value. This method is present for each field, but only if there is no name conflict with
- * another field.
- * @name ProtoBuf.Builder.Message#get_[some_field]
- * @function
- * @return {*} The value
- * @abstract
- */
- if (T.getChild("get_"+name) === null)
- MessagePrototype["get_"+name] = getter;
- })(field);
- }
- // En-/decoding
- /**
- * Encodes the message.
- * @name ProtoBuf.Builder.Message#$encode
- * @function
- * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
- * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
- * @return {!ByteBuffer} Encoded message as a ByteBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ByteBuffer in the `encoded` property on the error.
- * @expose
- * @see ProtoBuf.Builder.Message#encode64
- * @see ProtoBuf.Builder.Message#encodeHex
- * @see ProtoBuf.Builder.Message#encodeAB
- */
- MessagePrototype.encode = function(buffer, noVerify) {
- if (typeof buffer === 'boolean')
- noVerify = buffer,
- buffer = undefined;
- var isNew = false;
- if (!buffer)
- buffer = new ByteBuffer(),
- isNew = true;
- var le = buffer.littleEndian;
- try {
- T.encode(this, buffer.LE(), noVerify);
- return (isNew ? buffer.flip() : buffer).LE(le);
- } catch (e) {
- buffer.LE(le);
- throw(e);
- }
- };
- /**
- * Calculates the byte length of the message.
- * @name ProtoBuf.Builder.Message#calculate
- * @function
- * @returns {number} Byte length
- * @throws {Error} If the message cannot be calculated or if required fields are missing.
- * @expose
- */
- MessagePrototype.calculate = function() {
- return T.calculate(this);
- };
- /**
- * Encodes the varint32 length-delimited message.
- * @name ProtoBuf.Builder.Message#encodeDelimited
- * @function
- * @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
- * @return {!ByteBuffer} Encoded message as a ByteBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ByteBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeDelimited = function(buffer) {
- var isNew = false;
- if (!buffer)
- buffer = new ByteBuffer(),
- isNew = true;
- var enc = new ByteBuffer().LE();
- T.encode(this, enc).flip();
- buffer.writeVarint32(enc.remaining());
- buffer.append(enc);
- return isNew ? buffer.flip() : buffer;
- };
- /**
- * Directly encodes the message to an ArrayBuffer.
- * @name ProtoBuf.Builder.Message#encodeAB
- * @function
- * @return {ArrayBuffer} Encoded message as ArrayBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ArrayBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeAB = function() {
- try {
- return this.encode().toArrayBuffer();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toArrayBuffer();
- throw(e);
- }
- };
- /**
- * Returns the message as an ArrayBuffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeAB}.
- * @name ProtoBuf.Builder.Message#toArrayBuffer
- * @function
- * @return {ArrayBuffer} Encoded message as ArrayBuffer
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded ArrayBuffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toArrayBuffer = MessagePrototype.encodeAB;
- /**
- * Directly encodes the message to a node Buffer.
- * @name ProtoBuf.Builder.Message#encodeNB
- * @function
- * @return {!Buffer}
- * @throws {Error} If the message cannot be encoded, not running under node.js or if required fields are
- * missing. The later still returns the encoded node Buffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeNB = function() {
- try {
- return this.encode().toBuffer();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toBuffer();
- throw(e);
- }
- };
- /**
- * Returns the message as a node Buffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeNB}.
- * @name ProtoBuf.Builder.Message#toBuffer
- * @function
- * @return {!Buffer}
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded node Buffer in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toBuffer = MessagePrototype.encodeNB;
- /**
- * Directly encodes the message to a base64 encoded string.
- * @name ProtoBuf.Builder.Message#encode64
- * @function
- * @return {string} Base64 encoded string
- * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
- * still returns the encoded base64 string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encode64 = function() {
- try {
- return this.encode().toBase64();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toBase64();
- throw(e);
- }
- };
- /**
- * Returns the message as a base64 encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encode64}.
- * @name ProtoBuf.Builder.Message#toBase64
- * @function
- * @return {string} Base64 encoded string
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded base64 string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toBase64 = MessagePrototype.encode64;
- /**
- * Directly encodes the message to a hex encoded string.
- * @name ProtoBuf.Builder.Message#encodeHex
- * @function
- * @return {string} Hex encoded string
- * @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
- * still returns the encoded hex string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.encodeHex = function() {
- try {
- return this.encode().toHex();
- } catch (e) {
- if (e["encoded"]) e["encoded"] = e["encoded"].toHex();
- throw(e);
- }
- };
- /**
- * Returns the message as a hex encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encodeHex}.
- * @name ProtoBuf.Builder.Message#toHex
- * @function
- * @return {string} Hex encoded string
- * @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
- * returns the encoded hex string in the `encoded` property on the error.
- * @expose
- */
- MessagePrototype.toHex = MessagePrototype.encodeHex;
- /**
- * Clones a message object to a raw object.
- * @param {*} obj Object to clone
- * @param {boolean} includeBinaryAsBase64 Whether to include binary data as base64 strings or not
- * @returns {*} Cloned object
- * @inner
- */
- function cloneRaw(obj, includeBinaryAsBase64) {
- var clone = {};
- for (var i in obj)
- if (obj.hasOwnProperty(i)) {
- if (obj[i] === null || typeof obj[i] !== 'object')
- clone[i] = obj[i];
- else if (obj[i] instanceof ByteBuffer) {
- if (includeBinaryAsBase64)
- clone[i] = obj[i].toBase64();
- } else // is a non-null object
- clone[i] = cloneRaw(obj[i], includeBinaryAsBase64);
- }
- return clone;
- }
- /**
- * Returns the message's raw payload.
- * @param {boolean=} includeBinaryAsBase64 Whether to include binary data as base64 strings or not, defaults to `false`
- * @returns {Object.<string,*>} Raw payload
- * @expose
- */
- MessagePrototype.toRaw = function(includeBinaryAsBase64) {
- return cloneRaw(this, !!includeBinaryAsBase64);
- };
- /**
- * Decodes a message from the specified buffer or string.
- * @name ProtoBuf.Builder.Message.decode
- * @function
- * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
- * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- * @see ProtoBuf.Builder.Message.decode64
- * @see ProtoBuf.Builder.Message.decodeHex
- */
- Message.decode = function(buffer, enc) {
- if (typeof buffer === 'string')
- buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
- buffer = buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer); // May throw
- var le = buffer.littleEndian;
- try {
- var msg = T.decode(buffer.LE());
- buffer.LE(le);
- return msg;
- } catch (e) {
- buffer.LE(le);
- throw(e);
- }
- };
- /**
- * Decodes a varint32 length-delimited message from the specified buffer or string.
- * @name ProtoBuf.Builder.Message.decodeDelimited
- * @function
- * @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
- * @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
- * @return {ProtoBuf.Builder.Message} Decoded message or `null` if not enough bytes are available yet
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decodeDelimited = function(buffer, enc) {
- if (typeof buffer === 'string')
- buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
- buffer = buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer); // May throw
- if (buffer.remaining() < 1)
- return null;
- var off = buffer.offset,
- len = buffer.readVarint32();
- if (buffer.remaining() < len) {
- buffer.offset = off;
- return null;
- }
- try {
- var msg = T.decode(buffer.slice(buffer.offset, buffer.offset + len).LE());
- buffer.offset += len;
- return msg;
- } catch (err) {
- buffer.offset += len;
- throw err;
- }
- };
- /**
- * Decodes the message from the specified base64 encoded string.
- * @name ProtoBuf.Builder.Message.decode64
- * @function
- * @param {string} str String to decode from
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decode64 = function(str) {
- return Message.decode(str, "base64");
- };
- /**
- * Decodes the message from the specified hex encoded string.
- * @name ProtoBuf.Builder.Message.decodeHex
- * @function
- * @param {string} str String to decode from
- * @return {!ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
- * returns the decoded message with missing fields in the `decoded` property on the error.
- * @expose
- */
- Message.decodeHex = function(str) {
- return Message.decode(str, "hex");
- };
- // Utility
- /**
- * Returns a string representation of this Message.
- * @name ProtoBuf.Builder.Message#toString
- * @function
- * @return {string} String representation as of ".Fully.Qualified.MessageName"
- * @expose
- */
- MessagePrototype.toString = function() {
- return T.toString();
- };
- // Properties
- /**
- * Options.
- * @name ProtoBuf.Builder.Message.$options
- * @type {Object.<string,*>}
- * @expose
- */
- var $options; // cc
- /**
- * Reflection type.
- * @name ProtoBuf.Builder.Message#$type
- * @type {!ProtoBuf.Reflect.Message}
- * @expose
- */
- var $type; // cc
- if (Object.defineProperty)
- Object.defineProperty(Message, '$options', { "value": T.buildOpt() }),
- Object.defineProperty(MessagePrototype, "$type", {
- get: function() { return T; }
- });
- return Message;
- })(ProtoBuf, this);
- // Static enums and prototyped sub-messages / cached collections
- this._fields = [];
- this._fieldsById = {};
- this._fieldsByName = {};
- for (var i=0, k=this.children.length, child; i<k; i++) {
- child = this.children[i];
- if (child instanceof Enum)
- clazz[child.name] = child.build();
- else if (child instanceof Message)
- clazz[child.name] = child.build();
- else if (child instanceof Message.Field)
- child.build(),
- this._fields.push(child),
- this._fieldsById[child.id] = child,
- this._fieldsByName[child.name] = child;
- else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
- throw Error("Illegal reflect child of "+this.toString(true)+": "+children[i].toString(true));
- }
- return this.clazz = clazz;
- };
- /**
- * Encodes a runtime message's contents to the specified buffer.
- * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
- * @param {ByteBuffer} buffer ByteBuffer to write to
- * @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If required fields are missing or the message cannot be encoded for another reason
- * @expose
- */
- MessagePrototype.encode = function(message, buffer, noVerify) {
- var fieldMissing = null,
- field;
- for (var i=0, k=this._fields.length, val; i<k; ++i) {
- field = this._fields[i];
- val = message[field.name];
- if (field.required && val === null) {
- if (fieldMissing === null)
- fieldMissing = field;
- } else
- field.encode(noVerify ? val : field.verifyValue(val), buffer);
- }
- if (fieldMissing !== null) {
- var err = Error("Missing at least one required field for "+this.toString(true)+": "+fieldMissing);
- err["encoded"] = buffer; // Still expose what we got
- throw(err);
- }
- return buffer;
- };
- /**
- * Calculates a runtime message's byte length.
- * @param {!ProtoBuf.Builder.Message} message Runtime message to encode
- * @returns {number} Byte length
- * @throws {Error} If required fields are missing or the message cannot be calculated for another reason
- * @expose
- */
- MessagePrototype.calculate = function(message) {
- for (var n=0, i=0, k=this._fields.length, field, val; i<k; ++i) {
- field = this._fields[i];
- val = message[field.name];
- if (field.required && val === null)
- throw Error("Missing at least one required field for "+this.toString(true)+": "+field);
- else
- n += field.calculate(val);
- }
- return n;
- };
- /**
- * Skips all data until the end of the specified group has been reached.
- * @param {number} expectedId Expected GROUPEND id
- * @param {!ByteBuffer} buf ByteBuffer
- * @returns {boolean} `true` if a value as been skipped, `false` if the end has been reached
- * @throws {Error} If it wasn't possible to find the end of the group (buffer overrun or end tag mismatch)
- * @inner
- */
- function skipTillGroupEnd(expectedId, buf) {
- var tag = buf.readVarint32(), // Throws on OOB
- wireType = tag & 0x07,
- id = tag >> 3;
- switch (wireType) {
- case ProtoBuf.WIRE_TYPES.VARINT:
- do tag = buf.readUint8();
- while ((tag & 0x80) === 0x80);
- break;
- case ProtoBuf.WIRE_TYPES.BITS64:
- buf.offset += 8;
- break;
- case ProtoBuf.WIRE_TYPES.LDELIM:
- tag = buf.readVarint32(); // reads the varint
- buf.offset += tag; // skips n bytes
- break;
- case ProtoBuf.WIRE_TYPES.STARTGROUP:
- skipTillGroupEnd(id, buf);
- break;
- case ProtoBuf.WIRE_TYPES.ENDGROUP:
- if (id === expectedId)
- return false;
- else
- throw Error("Illegal GROUPEND after unknown group: "+id+" ("+expectedId+" expected)");
- case ProtoBuf.WIRE_TYPES.BITS32:
- buf.offset += 4;
- break;
- default:
- throw Error("Illegal wire type in unknown group "+expectedId+": "+wireType);
- }
- return true;
- }
- /**
- * Decodes an encoded message and returns the decoded message.
- * @param {ByteBuffer} buffer ByteBuffer to decode from
- * @param {number=} length Message length. Defaults to decode all the available data.
- * @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
- * @return {ProtoBuf.Builder.Message} Decoded message
- * @throws {Error} If the message cannot be decoded
- * @expose
- */
- MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
- length = typeof length === 'number' ? length : -1;
- var start = buffer.offset,
- msg = new (this.clazz)(),
- tag, wireType, id, field;
- while (buffer.offset < start+length || (length === -1 && buffer.remaining() > 0)) {
- tag = buffer.readVarint32();
- wireType = tag & 0x07;
- id = tag >> 3;
- if (wireType === ProtoBuf.WIRE_TYPES.ENDGROUP) {
- if (id !== expectedGroupEndId)
- throw Error("Illegal group end indicator for "+this.toString(true)+": "+id+" ("+(expectedGroupEndId ? expectedGroupEndId+" expected" : "not a group")+")");
- break;
- }
- if (!(field = this._fieldsById[id])) {
- // "messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing."
- switch (wireType) {
- case ProtoBuf.WIRE_TYPES.VARINT:
- buffer.readVarint32();
- break;
- case ProtoBuf.WIRE_TYPES.BITS32:
- buffer.offset += 4;
- break;
- case ProtoBuf.WIRE_TYPES.BITS64:
- buffer.offset += 8;
- break;
- case ProtoBuf.WIRE_TYPES.LDELIM:
- var len = buffer.readVarint32();
- buffer.offset += len;
- break;
- case ProtoBuf.WIRE_TYPES.STARTGROUP:
- while (skipTillGroupEnd(id, buffer)) {}
- break;
- default:
- throw Error("Illegal wire type for unknown field "+id+" in "+this.toString(true)+"#decode: "+wireType);
- }
- continue;
- }
- if (field.repeated && !field.options["packed"])
- msg[field.name].push(field.decode(wireType, buffer));
- else {
- msg[field.name] = field.decode(wireType, buffer);
- if (field.oneof) {
- if (this[field.oneof.name] !== null)
- this[this[field.oneof.name]] = null;
- msg[field.oneof.name] = field.name;
- }
- }
- }
- // Check if all required fields are present and set default values for optional fields that are not
- for (var i=0, k=this._fields.length; i<k; ++i) {
- field = this._fields[i];
- if (msg[field.name] === null)
- if (field.required) {
- var err = Error("Missing at least one required field for "+this.toString(true)+": "+field.name);
- err["decoded"] = msg; // Still expose what we got
- throw(err);
- } else if (field.defaultValue !== null)
- msg[field.name] = field.defaultValue;
- }
- return msg;
- };
- /**
- * @alias ProtoBuf.Reflect.Message
- * @expose
- */
- Reflect.Message = Message;
- /**
- * Constructs a new Message Field.
- * @exports ProtoBuf.Reflect.Message.Field
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} rule Rule, one of requried, optional, repeated
- * @param {string} type Data type, e.g. int32
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @param {Object.<string,*>=} options Options
- * @param {!ProtoBuf.Reflect.Message.OneOf=} oneof Enclosing OneOf
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Field = function(builder, message, rule, type, name, id, options, oneof) {
- T.call(this, builder, message, name);
- /**
- * @override
- */
- this.className = "Message.Field";
- /**
- * Message field required flag.
- * @type {boolean}
- * @expose
- */
- this.required = rule === "required";
- /**
- * Message field repeated flag.
- * @type {boolean}
- * @expose
- */
- this.repeated = rule === "repeated";
- /**
- * Message field type. Type reference string if unresolved, protobuf type if resolved.
- * @type {string|{name: string, wireType: number}}
- * @expose
- */
- this.type = type;
- /**
- * Resolved type reference inside the global namespace.
- * @type {ProtoBuf.Reflect.T|null}
- * @expose
- */
- this.resolvedType = null;
- /**
- * Unique message field id.
- * @type {number}
- * @expose
- */
- this.id = id;
- /**
- * Message field options.
- * @type {!Object.<string,*>}
- * @dict
- * @expose
- */
- this.options = options || {};
- /**
- * Default value.
- * @type {*}
- * @expose
- */
- this.defaultValue = null;
- /**
- * Enclosing OneOf.
- * @type {?ProtoBuf.Reflect.Message.OneOf}
- * @expose
- */
- this.oneof = oneof || null;
- /**
- * Original field name.
- * @type {string}
- * @expose
- */
- this.originalName = this.name; // Used to revert camelcase transformation on naming collisions
- // Convert field names to camel case notation if the override is set
- if (this.builder.options['convertFieldsToCamelCase'] && !(this instanceof Message.ExtensionField))
- this.name = Field._toCamelCase(this.name);
- };
- /**
- * Converts a field name to camel case.
- * @param {string} name Likely underscore notated name
- * @returns {string} Camel case notated name
- * @private
- */
- Field._toCamelCase = function(name) {
- return name.replace(/_([a-zA-Z])/g, function($0, $1) {
- return $1.toUpperCase();
- });
- };
- /**
- * @alias ProtoBuf.Reflect.Message.Field.prototype
- * @inner
- */
- var FieldPrototype = Field.prototype = Object.create(T.prototype);
- /**
- * Builds the field.
- * @override
- * @expose
- */
- FieldPrototype.build = function() {
- this.defaultValue = typeof this.options['default'] !== 'undefined'
- ? this.verifyValue(this.options['default']) : null;
- };
- /**
- * Makes a Long from a value.
- * @param {{low: number, high: number, unsigned: boolean}|string|number} value Value
- * @param {boolean=} unsigned Whether unsigned or not, defaults to reuse it from Long-like objects or to signed for
- * strings and numbers
- * @returns {!Long}
- * @throws {Error} If the value cannot be converted to a Long
- * @inner
- */
- function mkLong(value, unsigned) {
- if (value && typeof value.low === 'number' && typeof value.high === 'number' && typeof value.unsigned === 'boolean'
- && value.low === value.low && value.high === value.high)
- return new ProtoBuf.Long(value.low, value.high, typeof unsigned === 'undefined' ? value.unsigned : unsigned);
- if (typeof value === 'string')
- return ProtoBuf.Long.fromString(value, unsigned || false, 10);
- if (typeof value === 'number')
- return ProtoBuf.Long.fromNumber(value, unsigned || false);
- throw Error("not convertible to Long");
- }
- /**
- * Checks if the given value can be set for this field.
- * @param {*} value Value to check
- * @param {boolean=} skipRepeated Whether to skip the repeated value check or not. Defaults to false.
- * @return {*} Verified, maybe adjusted, value
- * @throws {Error} If the value cannot be set for this field
- * @expose
- */
- FieldPrototype.verifyValue = function(value, skipRepeated) {
- skipRepeated = skipRepeated || false;
- var fail = function(val, msg) {
- throw Error("Illegal value for "+this.toString(true)+" of type "+this.type.name+": "+val+" ("+msg+")");
- }.bind(this);
- if (value === null) { // NULL values for optional fields
- if (this.required)
- fail(typeof value, "required");
- return null;
- }
- var i;
- if (this.repeated && !skipRepeated) { // Repeated values as arrays
- if (!ProtoBuf.Util.isArray(value))
- value = [value];
- var res = [];
- for (i=0; i<value.length; i++)
- res.push(this.verifyValue(value[i], true));
- return res;
- }
- // All non-repeated fields expect no array
- if (!this.repeated && ProtoBuf.Util.isArray(value))
- fail(typeof value, "no array expected");
- switch (this.type) {
- // Signed 32bit
- case ProtoBuf.TYPES["int32"]:
- case ProtoBuf.TYPES["sint32"]:
- case ProtoBuf.TYPES["sfixed32"]:
- // Account for !NaN: value === value
- if (typeof value !== 'number' || (value === value && value % 1 !== 0))
- fail(typeof value, "not an integer");
- return value > 4294967295 ? value | 0 : value;
- // Unsigned 32bit
- case ProtoBuf.TYPES["uint32"]:
- case ProtoBuf.TYPES["fixed32"]:
- if (typeof value !== 'number' || (value === value && value % 1 !== 0))
- fail(typeof value, "not an integer");
- return value < 0 ? value >>> 0 : value;
- // Signed 64bit
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["sint64"]:
- case ProtoBuf.TYPES["sfixed64"]: {
- if (ProtoBuf.Long)
- try {
- return mkLong(value, false);
- } catch (e) {
- fail(typeof value, e.message);
- }
- else
- fail(typeof value, "requires Long.js");
- }
- // Unsigned 64bit
- case ProtoBuf.TYPES["uint64"]:
- case ProtoBuf.TYPES["fixed64"]: {
- if (ProtoBuf.Long)
- try {
- return mkLong(value, true);
- } catch (e) {
- fail(typeof value, e.message);
- }
- else
- fail(typeof value, "requires Long.js");
- }
- // Bool
- case ProtoBuf.TYPES["bool"]:
- if (typeof value !== 'boolean')
- fail(typeof value, "not a boolean");
- return value;
- // Float
- case ProtoBuf.TYPES["float"]:
- case ProtoBuf.TYPES["double"]:
- if (typeof value !== 'number')
- fail(typeof value, "not a number");
- return value;
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- if (typeof value !== 'string' && !(value && value instanceof String))
- fail(typeof value, "not a string");
- return ""+value; // Convert String object to string
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]:
- if (ByteBuffer.isByteBuffer(value))
- return value;
- return ByteBuffer.wrap(value, "base64");
- // Constant enum value
- case ProtoBuf.TYPES["enum"]: {
- var values = this.resolvedType.getChildren(Enum.Value);
- for (i=0; i<values.length; i++)
- if (values[i].name == value)
- return values[i].id;
- else if (values[i].id == value)
- return values[i].id;
- fail(value, "not a valid enum value");
- }
- // Embedded message
- case ProtoBuf.TYPES["group"]:
- case ProtoBuf.TYPES["message"]: {
- if (!value || typeof value !== 'object')
- fail(typeof value, "object expected");
- if (value instanceof this.resolvedType.clazz)
- return value;
- if (value instanceof ProtoBuf.Builder.Message) {
- // Mismatched type: Convert to object (see: https://github.com/dcodeIO/ProtoBuf.js/issues/180)
- var obj = {};
- for (var i in value)
- if (value.hasOwnProperty(i))
- obj[i] = value[i];
- value = obj;
- }
- // Else let's try to construct one from a key-value object
- return new (this.resolvedType.clazz)(value); // May throw for a hundred of reasons
- }
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal value for "+this.toString(true)+": "+value+" (undefined type "+this.type+")");
- };
- /**
- * Encodes the specified field value to the specified buffer.
- * @param {*} value Verified field value
- * @param {ByteBuffer} buffer ByteBuffer to encode to
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If the field cannot be encoded
- * @expose
- */
- FieldPrototype.encode = function(value, buffer) {
- if (this.type === null || typeof this.type !== 'object')
- throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
- if (value === null || (this.repeated && value.length == 0))
- return buffer; // Optional omitted
- try {
- if (this.repeated) {
- var i;
- // "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire
- // types) can be declared 'packed'."
- if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- // "All of the elements of the field are packed into a single key-value pair with wire type 2
- // (length-delimited). Each element is encoded the same way it would be normally, except without a
- // tag preceding it."
- buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
- buffer.ensureCapacity(buffer.offset += 1); // We do not know the length yet, so let's assume a varint of length 1
- var start = buffer.offset; // Remember where the contents begin
- for (i=0; i<value.length; i++)
- this.encodeValue(value[i], buffer);
- var len = buffer.offset-start,
- varintLen = ByteBuffer.calculateVarint32(len);
- if (varintLen > 1) { // We need to move the contents
- var contents = buffer.slice(start, buffer.offset);
- start += varintLen-1;
- buffer.offset = start;
- buffer.append(contents);
- }
- buffer.writeVarint32(len, start-varintLen);
- } else {
- // "If your message definition has repeated elements (without the [packed=true] option), the encoded
- // message has zero or more key-value pairs with the same tag number"
- for (i=0; i<value.length; i++)
- buffer.writeVarint32((this.id << 3) | this.type.wireType),
- this.encodeValue(value[i], buffer);
- }
- } else
- buffer.writeVarint32((this.id << 3) | this.type.wireType),
- this.encodeValue(value, buffer);
- } catch (e) {
- throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
- }
- return buffer;
- };
- /**
- * Encodes a value to the specified buffer. Does not encode the key.
- * @param {*} value Field value
- * @param {ByteBuffer} buffer ByteBuffer to encode to
- * @return {ByteBuffer} The ByteBuffer for chaining
- * @throws {Error} If the value cannot be encoded
- * @expose
- */
- FieldPrototype.encodeValue = function(value, buffer) {
- if (value === null) return buffer; // Nothing to encode
- // Tag has already been written
- switch (this.type) {
- // 32bit signed varint
- case ProtoBuf.TYPES["int32"]:
- // "If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes
- // long – it is, effectively, treated like a very large unsigned integer." (see #122)
- if (value < 0)
- buffer.writeVarint64(value);
- else
- buffer.writeVarint32(value);
- break;
- // 32bit unsigned varint
- case ProtoBuf.TYPES["uint32"]:
- buffer.writeVarint32(value);
- break;
- // 32bit varint zig-zag
- case ProtoBuf.TYPES["sint32"]:
- buffer.writeVarint32ZigZag(value);
- break;
- // Fixed unsigned 32bit
- case ProtoBuf.TYPES["fixed32"]:
- buffer.writeUint32(value);
- break;
- // Fixed signed 32bit
- case ProtoBuf.TYPES["sfixed32"]:
- buffer.writeInt32(value);
- break;
- // 64bit varint as-is
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["uint64"]:
- buffer.writeVarint64(value); // throws
- break;
- // 64bit varint zig-zag
- case ProtoBuf.TYPES["sint64"]:
- buffer.writeVarint64ZigZag(value); // throws
- break;
- // Fixed unsigned 64bit
- case ProtoBuf.TYPES["fixed64"]:
- buffer.writeUint64(value); // throws
- break;
- // Fixed signed 64bit
- case ProtoBuf.TYPES["sfixed64"]:
- buffer.writeInt64(value); // throws
- break;
- // Bool
- case ProtoBuf.TYPES["bool"]:
- if (typeof value === 'string')
- buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : !!value);
- else
- buffer.writeVarint32(value ? 1 : 0);
- break;
- // Constant enum value
- case ProtoBuf.TYPES["enum"]:
- buffer.writeVarint32(value);
- break;
- // 32bit float
- case ProtoBuf.TYPES["float"]:
- buffer.writeFloat32(value);
- break;
- // 64bit float
- case ProtoBuf.TYPES["double"]:
- buffer.writeFloat64(value);
- break;
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- buffer.writeVString(value);
- break;
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]:
- if (value.remaining() < 0)
- throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
- var prevOffset = value.offset;
- buffer.writeVarint32(value.remaining());
- buffer.append(value);
- value.offset = prevOffset;
- break;
- // Embedded message
- case ProtoBuf.TYPES["message"]:
- var bb = new ByteBuffer().LE();
- this.resolvedType.encode(value, bb);
- buffer.writeVarint32(bb.offset);
- buffer.append(bb.flip());
- break;
- // Legacy group
- case ProtoBuf.TYPES["group"]:
- this.resolvedType.encode(value, buffer);
- buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
- break;
- default:
- // We should never end here
- throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
- }
- return buffer;
- };
- /**
- * Calculates the length of this field's value on the network level.
- * @param {*} value Field value
- * @returns {number} Byte length
- * @expose
- */
- FieldPrototype.calculate = function(value) {
- value = this.verifyValue(value); // May throw
- if (this.type === null || typeof this.type !== 'object')
- throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
- if (value === null || (this.repeated && value.length == 0))
- return 0; // Optional omitted
- var n = 0;
- try {
- if (this.repeated) {
- var i, ni;
- if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
- ni = 0;
- for (i=0; i<value.length; i++)
- ni += this.calculateValue(value[i]);
- n += ByteBuffer.calculateVarint32(ni);
- n += ni;
- } else {
- for (i=0; i<value.length; i++)
- n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType),
- n += this.calculateValue(value[i]);
- }
- } else {
- n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType);
- n += this.calculateValue(value);
- }
- } catch (e) {
- throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
- }
- return n;
- };
- /**
- * Calculates the byte length of a value.
- * @param {*} value Field value
- * @returns {number} Byte length
- * @throws {Error} If the value cannot be calculated
- * @expose
- */
- FieldPrototype.calculateValue = function(value) {
- if (value === null) return 0; // Nothing to encode
- // Tag has already been written
- var n;
- switch (this.type) {
- case ProtoBuf.TYPES["int32"]:
- return value < 0 ? ByteBuffer.calculateVarint64(value) : ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["uint32"]:
- return ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["sint32"]:
- return ByteBuffer.calculateVarint32(ByteBuffer.zigZagEncode32(value));
- case ProtoBuf.TYPES["fixed32"]:
- case ProtoBuf.TYPES["sfixed32"]:
- case ProtoBuf.TYPES["float"]:
- return 4;
- case ProtoBuf.TYPES["int64"]:
- case ProtoBuf.TYPES["uint64"]:
- return ByteBuffer.calculateVarint64(value);
- case ProtoBuf.TYPES["sint64"]:
- return ByteBuffer.calculateVarint64(ByteBuffer.zigZagEncode64(value));
- case ProtoBuf.TYPES["fixed64"]:
- case ProtoBuf.TYPES["sfixed64"]:
- return 8;
- case ProtoBuf.TYPES["bool"]:
- return 1;
- case ProtoBuf.TYPES["enum"]:
- return ByteBuffer.calculateVarint32(value);
- case ProtoBuf.TYPES["double"]:
- return 8;
- case ProtoBuf.TYPES["string"]:
- n = ByteBuffer.calculateUTF8Bytes(value);
- return ByteBuffer.calculateVarint32(n) + n;
- case ProtoBuf.TYPES["bytes"]:
- if (value.remaining() < 0)
- throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
- return ByteBuffer.calculateVarint32(value.remaining()) + value.remaining();
- case ProtoBuf.TYPES["message"]:
- n = this.resolvedType.calculate(value);
- return ByteBuffer.calculateVarint32(n) + n;
- case ProtoBuf.TYPES["group"]:
- n = this.resolvedType.calculate(value);
- return n + ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
- };
- /**
- * Decode the field value from the specified buffer.
- * @param {number} wireType Leading wire type
- * @param {ByteBuffer} buffer ByteBuffer to decode from
- * @param {boolean=} skipRepeated Whether to skip the repeated check or not. Defaults to false.
- * @return {*} Decoded value
- * @throws {Error} If the field cannot be decoded
- * @expose
- */
- FieldPrototype.decode = function(wireType, buffer, skipRepeated) {
- var value, nBytes;
- if (wireType != this.type.wireType && (skipRepeated || (wireType != ProtoBuf.WIRE_TYPES.LDELIM || !this.repeated)))
- throw Error("Illegal wire type for field "+this.toString(true)+": "+wireType+" ("+this.type.wireType+" expected)");
- if (wireType == ProtoBuf.WIRE_TYPES.LDELIM && this.repeated && this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
- if (!skipRepeated) {
- nBytes = buffer.readVarint32();
- nBytes = buffer.offset + nBytes; // Limit
- var values = [];
- while (buffer.offset < nBytes)
- values.push(this.decode(this.type.wireType, buffer, true));
- return values;
- }
- // Read the next value otherwise...
- }
- switch (this.type) {
- // 32bit signed varint
- case ProtoBuf.TYPES["int32"]:
- return buffer.readVarint32() | 0;
- // 32bit unsigned varint
- case ProtoBuf.TYPES["uint32"]:
- return buffer.readVarint32() >>> 0;
- // 32bit signed varint zig-zag
- case ProtoBuf.TYPES["sint32"]:
- return buffer.readVarint32ZigZag() | 0;
- // Fixed 32bit unsigned
- case ProtoBuf.TYPES["fixed32"]:
- return buffer.readUint32() >>> 0;
- case ProtoBuf.TYPES["sfixed32"]:
- return buffer.readInt32() | 0;
- // 64bit signed varint
- case ProtoBuf.TYPES["int64"]:
- return buffer.readVarint64();
- // 64bit unsigned varint
- case ProtoBuf.TYPES["uint64"]:
- return buffer.readVarint64().toUnsigned();
- // 64bit signed varint zig-zag
- case ProtoBuf.TYPES["sint64"]:
- return buffer.readVarint64ZigZag();
- // Fixed 64bit unsigned
- case ProtoBuf.TYPES["fixed64"]:
- return buffer.readUint64();
- // Fixed 64bit signed
- case ProtoBuf.TYPES["sfixed64"]:
- return buffer.readInt64();
- // Bool varint
- case ProtoBuf.TYPES["bool"]:
- return !!buffer.readVarint32();
- // Constant enum value (varint)
- case ProtoBuf.TYPES["enum"]:
- // The following Builder.Message#set will already throw
- return buffer.readVarint32();
- // 32bit float
- case ProtoBuf.TYPES["float"]:
- return buffer.readFloat();
- // 64bit float
- case ProtoBuf.TYPES["double"]:
- return buffer.readDouble();
- // Length-delimited string
- case ProtoBuf.TYPES["string"]:
- return buffer.readVString();
- // Length-delimited bytes
- case ProtoBuf.TYPES["bytes"]: {
- nBytes = buffer.readVarint32();
- if (buffer.remaining() < nBytes)
- throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
- value = buffer.clone(); // Offset already set
- value.limit = value.offset+nBytes;
- buffer.offset += nBytes;
- return value;
- }
- // Length-delimited embedded message
- case ProtoBuf.TYPES["message"]: {
- nBytes = buffer.readVarint32();
- return this.resolvedType.decode(buffer, nBytes);
- }
- // Legacy group
- case ProtoBuf.TYPES["group"]:
- return this.resolvedType.decode(buffer, -1, this.id);
- }
- // We should never end here
- throw Error("[INTERNAL] Illegal wire type for "+this.toString(true)+": "+wireType);
- };
- /**
- * @alias ProtoBuf.Reflect.Message.Field
- * @expose
- */
- Reflect.Message.Field = Field;
- /**
- * Constructs a new Message ExtensionField.
- * @exports ProtoBuf.Reflect.Message.ExtensionField
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} rule Rule, one of requried, optional, repeated
- * @param {string} type Data type, e.g. int32
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Message.Field
- */
- var ExtensionField = function(builder, message, rule, type, name, id, options) {
- Field.call(this, builder, message, rule, type, name, id, options);
- /**
- * Extension reference.
- * @type {!ProtoBuf.Reflect.Extension}
- * @expose
- */
- this.extension;
- };
- // Extends Field
- ExtensionField.prototype = Object.create(Field.prototype);
- /**
- * @alias ProtoBuf.Reflect.Message.ExtensionField
- * @expose
- */
- Reflect.Message.ExtensionField = ExtensionField;
- /**
- * Constructs a new Message OneOf.
- * @exports ProtoBuf.Reflect.Message.OneOf
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Message} message Message reference
- * @param {string} name OneOf name
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var OneOf = function(builder, message, name) {
- T.call(this, builder, message, name);
- /**
- * Enclosed fields.
- * @type {!Array.<!ProtoBuf.Reflect.Message.Field>}
- * @expose
- */
- this.fields = [];
- };
- /**
- * @alias ProtoBuf.Reflect.Message.OneOf
- * @expose
- */
- Reflect.Message.OneOf = OneOf;
- /**
- * Constructs a new Enum.
- * @exports ProtoBuf.Reflect.Enum
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
- * @param {string} name Enum name
- * @param {Object.<string,*>=} options Enum options
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Enum = function(builder, parent, name, options) {
- Namespace.call(this, builder, parent, name, options);
- /**
- * @override
- */
- this.className = "Enum";
- /**
- * Runtime enum object.
- * @type {Object.<string,number>|null}
- * @expose
- */
- this.object = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Enum.prototype
- * @inner
- */
- var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
- /**
- * Builds this enum and returns the runtime counterpart.
- * @return {Object<string,*>}
- * @expose
- */
- EnumPrototype.build = function() {
- var enm = {},
- values = this.getChildren(Enum.Value);
- for (var i=0, k=values.length; i<k; ++i)
- enm[values[i]['name']] = values[i]['id'];
- if (Object.defineProperty)
- Object.defineProperty(enm, '$options', { "value": this.buildOpt() });
- return this.object = enm;
- };
- /**
- * @alias ProtoBuf.Reflect.Enum
- * @expose
- */
- Reflect.Enum = Enum;
- /**
- * Constructs a new Enum Value.
- * @exports ProtoBuf.Reflect.Enum.Value
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Enum} enm Enum reference
- * @param {string} name Field name
- * @param {number} id Unique field id
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Value = function(builder, enm, name, id) {
- T.call(this, builder, enm, name);
- /**
- * @override
- */
- this.className = "Enum.Value";
- /**
- * Unique enum value id.
- * @type {number}
- * @expose
- */
- this.id = id;
- };
- // Extends T
- Value.prototype = Object.create(T.prototype);
- /**
- * @alias ProtoBuf.Reflect.Enum.Value
- * @expose
- */
- Reflect.Enum.Value = Value;
- /**
- * An extension (field).
- * @exports ProtoBuf.Reflect.Extension
- * @constructor
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.T} parent Parent object
- * @param {string} name Object name
- * @param {!ProtoBuf.Reflect.Message.Field} field Extension field
- */
- var Extension = function(builder, parent, name, field) {
- T.call(this, builder, parent, name);
- /**
- * Extended message field.
- * @type {!ProtoBuf.Reflect.Message.Field}
- * @expose
- */
- this.field = field;
- };
- // Extends T
- Extension.prototype = Object.create(T.prototype);
- /**
- * @alias ProtoBuf.Reflect.Extension
- * @expose
- */
- Reflect.Extension = Extension;
- /**
- * Constructs a new Service.
- * @exports ProtoBuf.Reflect.Service
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Namespace} root Root
- * @param {string} name Service name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Namespace
- */
- var Service = function(builder, root, name, options) {
- Namespace.call(this, builder, root, name, options);
- /**
- * @override
- */
- this.className = "Service";
- /**
- * Built runtime service class.
- * @type {?function(new:ProtoBuf.Builder.Service)}
- */
- this.clazz = null;
- };
- /**
- * @alias ProtoBuf.Reflect.Service.prototype
- * @inner
- */
- var ServicePrototype = Service.prototype = Object.create(Namespace.prototype);
- /**
- * Builds the service and returns the runtime counterpart, which is a fully functional class.
- * @see ProtoBuf.Builder.Service
- * @param {boolean=} rebuild Whether to rebuild or not
- * @return {Function} Service class
- * @throws {Error} If the message cannot be built
- * @expose
- */
- ServicePrototype.build = function(rebuild) {
- if (this.clazz && !rebuild)
- return this.clazz;
- // Create the runtime Service class in its own scope
- return this.clazz = (function(ProtoBuf, T) {
- /**
- * Constructs a new runtime Service.
- * @name ProtoBuf.Builder.Service
- * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
- * @class Barebone of all runtime services.
- * @constructor
- * @throws {Error} If the service cannot be created
- */
- var Service = function(rpcImpl) {
- ProtoBuf.Builder.Service.call(this);
- /**
- * Service implementation.
- * @name ProtoBuf.Builder.Service#rpcImpl
- * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
- * @expose
- */
- this.rpcImpl = rpcImpl || function(name, msg, callback) {
- // This is what a user has to implement: A function receiving the method name, the actual message to
- // send (type checked) and the callback that's either provided with the error as its first
- // argument or null and the actual response message.
- setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
- };
- };
- /**
- * @alias ProtoBuf.Builder.Service.prototype
- * @inner
- */
- var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
- if (Object.defineProperty)
- Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
- Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] });
- /**
- * Asynchronously performs an RPC call using the given RPC implementation.
- * @name ProtoBuf.Builder.Service.[Method]
- * @function
- * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
- * @param {ProtoBuf.Builder.Message} req Request
- * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
- * the error if any and the response either as a pre-parsed message or as its raw bytes
- * @abstract
- */
- /**
- * Asynchronously performs an RPC call using the instance's RPC implementation.
- * @name ProtoBuf.Builder.Service#[Method]
- * @function
- * @param {ProtoBuf.Builder.Message} req Request
- * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
- * the error if any and the response either as a pre-parsed message or as its raw bytes
- * @abstract
- */
- var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
- for (var i=0; i<rpc.length; i++) {
- (function(method) {
- // service#Method(message, callback)
- ServicePrototype[method.name] = function(req, callback) {
- try {
- if (!req || !(req instanceof method.resolvedRequestType.clazz)) {
- setTimeout(callback.bind(this, Error("Illegal request type provided to service method "+T.name+"#"+method.name)), 0);
- return;
- }
- this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
- if (err) {
- callback(err);
- return;
- }
- try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
- if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
- callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
- return;
- }
- callback(null, res);
- });
- } catch (err) {
- setTimeout(callback.bind(this, err), 0);
- }
- };
- // Service.Method(rpcImpl, message, callback)
- Service[method.name] = function(rpcImpl, req, callback) {
- new Service(rpcImpl)[method.name](req, callback);
- };
- if (Object.defineProperty)
- Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
- Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
- })(rpc[i]);
- }
- return Service;
- })(ProtoBuf, this);
- };
- /**
- * @alias ProtoBuf.Reflect.Service
- * @expose
- */
- Reflect.Service = Service;
- /**
- * Abstract service method.
- * @exports ProtoBuf.Reflect.Service.Method
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Service} svc Service
- * @param {string} name Method name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.T
- */
- var Method = function(builder, svc, name, options) {
- T.call(this, builder, svc, name);
- /**
- * @override
- */
- this.className = "Service.Method";
- /**
- * Options.
- * @type {Object.<string, *>}
- * @expose
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Reflect.Service.Method.prototype
- * @inner
- */
- var MethodPrototype = Method.prototype = Object.create(T.prototype);
- /**
- * Builds the method's '$options' property.
- * @name ProtoBuf.Reflect.Service.Method#buildOpt
- * @function
- * @return {Object.<string,*>}
- */
- MethodPrototype.buildOpt = NamespacePrototype.buildOpt;
- /**
- * @alias ProtoBuf.Reflect.Service.Method
- * @expose
- */
- Reflect.Service.Method = Method;
- /**
- * RPC service method.
- * @exports ProtoBuf.Reflect.Service.RPCMethod
- * @param {!ProtoBuf.Builder} builder Builder reference
- * @param {!ProtoBuf.Reflect.Service} svc Service
- * @param {string} name Method name
- * @param {string} request Request message name
- * @param {string} response Response message name
- * @param {Object.<string,*>=} options Options
- * @constructor
- * @extends ProtoBuf.Reflect.Service.Method
- */
- var RPCMethod = function(builder, svc, name, request, response, options) {
- Method.call(this, builder, svc, name, options);
- /**
- * @override
- */
- this.className = "Service.RPCMethod";
- /**
- * Request message name.
- * @type {string}
- * @expose
- */
- this.requestName = request;
- /**
- * Response message name.
- * @type {string}
- * @expose
- */
- this.responseName = response;
- /**
- * Resolved request message type.
- * @type {ProtoBuf.Reflect.Message}
- * @expose
- */
- this.resolvedRequestType = null;
- /**
- * Resolved response message type.
- * @type {ProtoBuf.Reflect.Message}
- * @expose
- */
- this.resolvedResponseType = null;
- };
- // Extends Method
- RPCMethod.prototype = Object.create(Method.prototype);
- /**
- * @alias ProtoBuf.Reflect.Service.RPCMethod
- * @expose
- */
- Reflect.Service.RPCMethod = RPCMethod;
- return Reflect;
- })(ProtoBuf);
- /**
- * @alias ProtoBuf.Builder
- * @expose
- */
- ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
- "use strict";
- /**
- * Constructs a new Builder.
- * @exports ProtoBuf.Builder
- * @class Provides the functionality to build protocol messages.
- * @param {Object.<string,*>=} options Options
- * @constructor
- */
- var Builder = function(options) {
- /**
- * Namespace.
- * @type {ProtoBuf.Reflect.Namespace}
- * @expose
- */
- this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
- /**
- * Namespace pointer.
- * @type {ProtoBuf.Reflect.T}
- * @expose
- */
- this.ptr = this.ns;
- /**
- * Resolved flag.
- * @type {boolean}
- * @expose
- */
- this.resolved = false;
- /**
- * The current building result.
- * @type {Object.<string,ProtoBuf.Builder.Message|Object>|null}
- * @expose
- */
- this.result = null;
- /**
- * Imported files.
- * @type {Array.<string>}
- * @expose
- */
- this.files = {};
- /**
- * Import root override.
- * @type {?string}
- * @expose
- */
- this.importRoot = null;
- /**
- * Options.
- * @type {!Object.<string, *>}
- * @expose
- */
- this.options = options || {};
- };
- /**
- * @alias ProtoBuf.Builder.prototype
- * @inner
- */
- var BuilderPrototype = Builder.prototype;
- /**
- * Resets the pointer to the root namespace.
- * @expose
- */
- BuilderPrototype.reset = function() {
- this.ptr = this.ns;
- };
- /**
- * Defines a package on top of the current pointer position and places the pointer on it.
- * @param {string} pkg
- * @param {Object.<string,*>=} options
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If the package name is invalid
- * @expose
- */
- BuilderPrototype.define = function(pkg, options) {
- if (typeof pkg !== 'string' || !Lang.TYPEREF.test(pkg))
- throw Error("Illegal package: "+pkg);
- var part = pkg.split("."), i;
- for (i=0; i<part.length; i++) // To be absolutely sure
- if (!Lang.NAME.test(part[i]))
- throw Error("Illegal package: "+part[i]);
- for (i=0; i<part.length; i++) {
- if (this.ptr.getChild(part[i]) === null) // Keep existing namespace
- this.ptr.addChild(new Reflect.Namespace(this, this.ptr, part[i], options));
- this.ptr = this.ptr.getChild(part[i]);
- }
- return this;
- };
- /**
- * Tests if a definition is a valid message definition.
- * @param {Object.<string,*>} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidMessage = function(def) {
- // Messages require a string name
- if (typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]))
- return false;
- // Messages must not contain values (that'd be an enum) or methods (that'd be a service)
- if (typeof def["values"] !== 'undefined' || typeof def["rpc"] !== 'undefined')
- return false;
- // Fields, enums and messages are arrays if provided
- var i;
- if (typeof def["fields"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["fields"]))
- return false;
- var ids = [], id; // IDs must be unique
- for (i=0; i<def["fields"].length; i++) {
- if (!Builder.isValidMessageField(def["fields"][i]))
- return false;
- id = parseInt(def["fields"][i]["id"], 10);
- if (ids.indexOf(id) >= 0)
- return false;
- ids.push(id);
- }
- ids = null;
- }
- if (typeof def["enums"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["enums"]))
- return false;
- for (i=0; i<def["enums"].length; i++)
- if (!Builder.isValidEnum(def["enums"][i]))
- return false;
- }
- if (typeof def["messages"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["messages"]))
- return false;
- for (i=0; i<def["messages"].length; i++)
- if (!Builder.isValidMessage(def["messages"][i]) && !Builder.isValidExtend(def["messages"][i]))
- return false;
- }
- if (typeof def["extensions"] !== 'undefined')
- if (!ProtoBuf.Util.isArray(def["extensions"]) || def["extensions"].length !== 2 || typeof def["extensions"][0] !== 'number' || typeof def["extensions"][1] !== 'number')
- return false;
- return true;
- };
- /**
- * Tests if a definition is a valid message field definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidMessageField = function(def) {
- // Message fields require a string rule, name and type and an id
- if (typeof def["rule"] !== 'string' || typeof def["name"] !== 'string' || typeof def["type"] !== 'string' || typeof def["id"] === 'undefined')
- return false;
- if (!Lang.RULE.test(def["rule"]) || !Lang.NAME.test(def["name"]) || !Lang.TYPEREF.test(def["type"]) || !Lang.ID.test(""+def["id"]))
- return false;
- if (typeof def["options"] !== 'undefined') {
- // Options are objects
- if (typeof def["options"] !== 'object')
- return false;
- // Options are <string,string|number|boolean>
- var keys = Object.keys(def["options"]);
- for (var i=0, key; i<keys.length; i++)
- if (typeof (key = keys[i]) !== 'string' || (typeof def["options"][key] !== 'string' && typeof def["options"][key] !== 'number' && typeof def["options"][key] !== 'boolean'))
- return false;
- }
- return true;
- };
- /**
- * Tests if a definition is a valid enum definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidEnum = function(def) {
- // Enums require a string name
- if (typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]))
- return false;
- // Enums require at least one value
- if (typeof def["values"] === 'undefined' || !ProtoBuf.Util.isArray(def["values"]) || def["values"].length == 0)
- return false;
- for (var i=0; i<def["values"].length; i++) {
- // Values are objects
- if (typeof def["values"][i] != "object")
- return false;
- // Values require a string name and an id
- if (typeof def["values"][i]["name"] !== 'string' || typeof def["values"][i]["id"] === 'undefined')
- return false;
- if (!Lang.NAME.test(def["values"][i]["name"]) || !Lang.NEGID.test(""+def["values"][i]["id"]))
- return false;
- }
- // It's not important if there are other fields because ["values"] is already unique
- return true;
- };
- /**
- * Creates ths specified protocol types at the current pointer position.
- * @param {Array.<Object.<string,*>>} defs Messages, enums or services to create
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If a message definition is invalid
- * @expose
- */
- BuilderPrototype.create = function(defs) {
- if (!defs)
- return this; // Nothing to create
- if (!ProtoBuf.Util.isArray(defs))
- defs = [defs];
- if (defs.length == 0)
- return this;
- // It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
- var stack = [];
- stack.push(defs); // One level [a, b, c]
- while (stack.length > 0) {
- defs = stack.pop();
- if (ProtoBuf.Util.isArray(defs)) { // Stack always contains entire namespaces
- while (defs.length > 0) {
- var def = defs.shift(); // Namespace always contains an array of messages, enums and services
- if (Builder.isValidMessage(def)) {
- var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"]);
- // Create OneOfs
- var oneofs = {};
- if (def["oneofs"]) {
- var keys = Object.keys(def["oneofs"]);
- for (var i=0, k=keys.length; i<k; ++i)
- obj.addChild(oneofs[keys[i]] = new Reflect.Message.OneOf(this, obj, keys[i]));
- }
- // Create fields
- if (def["fields"] && def["fields"].length > 0) {
- for (i=0, k=def["fields"].length; i<k; ++i) { // i:k=Fields
- var fld = def['fields'][i];
- if (obj.getChild(fld['id']) !== null)
- throw Error("Duplicate field id in message "+obj.name+": "+fld['id']);
- if (fld["options"]) {
- var opts = Object.keys(fld["options"]);
- for (var j= 0,l=opts.length; j<l; ++j) { // j:l=Option names
- if (typeof opts[j] !== 'string')
- throw Error("Illegal field option name in message "+obj.name+"#"+fld["name"]+": "+opts[j]);
- if (typeof fld["options"][opts[j]] !== 'string' && typeof fld["options"][opts[j]] !== 'number' && typeof fld["options"][opts[j]] !== 'boolean')
- throw Error("Illegal field option value in message "+obj.name+"#"+fld["name"]+"#"+opts[j]+": "+fld["options"][opts[j]]);
- }
- }
- var oneof = null;
- if (typeof fld["oneof"] === 'string') {
- oneof = oneofs[fld["oneof"]];
- if (typeof oneof === 'undefined')
- throw Error("Illegal oneof in message "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
- }
- fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["type"], fld["name"], fld["id"], fld["options"], oneof);
- if (oneof)
- oneof.fields.push(fld);
- obj.addChild(fld);
- }
- }
- // Push enums and messages to stack
- var subObj = [];
- if (typeof def["enums"] !== 'undefined' && def['enums'].length > 0)
- for (i=0; i<def["enums"].length; i++)
- subObj.push(def["enums"][i]);
- if (def["messages"] && def["messages"].length > 0)
- for (i=0; i<def["messages"].length; i++)
- subObj.push(def["messages"][i]);
- // Set extension range
- if (def["extensions"]) {
- obj.extensions = def["extensions"];
- if (obj.extensions[0] < ProtoBuf.ID_MIN)
- obj.extensions[0] = ProtoBuf.ID_MIN;
- if (obj.extensions[1] > ProtoBuf.ID_MAX)
- obj.extensions[1] = ProtoBuf.ID_MAX;
- }
- this.ptr.addChild(obj); // Add to current namespace
- if (subObj.length > 0) {
- stack.push(defs); // Push the current level back
- defs = subObj; // Continue processing sub level
- subObj = null;
- this.ptr = obj; // And move the pointer to this namespace
- obj = null;
- continue;
- }
- subObj = null;
- obj = null;
- } else if (Builder.isValidEnum(def)) {
- obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"]);
- for (i=0; i<def["values"].length; i++)
- obj.addChild(new Reflect.Enum.Value(this, obj, def["values"][i]["name"], def["values"][i]["id"]));
- this.ptr.addChild(obj);
- obj = null;
- } else if (Builder.isValidService(def)) {
- obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
- for (i in def["rpc"])
- if (def["rpc"].hasOwnProperty(i))
- obj.addChild(new Reflect.Service.RPCMethod(this, obj, i, def["rpc"][i]["request"], def["rpc"][i]["response"], def["rpc"][i]["options"]));
- this.ptr.addChild(obj);
- obj = null;
- } else if (Builder.isValidExtend(def)) {
- obj = this.ptr.resolve(def["ref"]);
- if (obj) {
- for (i=0; i<def["fields"].length; i++) { // i=Fields
- if (obj.getChild(def['fields'][i]['id']) !== null)
- throw Error("Duplicate extended field id in message "+obj.name+": "+def['fields'][i]['id']);
- if (def['fields'][i]['id'] < obj.extensions[0] || def['fields'][i]['id'] > obj.extensions[1])
- throw Error("Illegal extended field id in message "+obj.name+": "+def['fields'][i]['id']+" ("+obj.extensions.join(' to ')+" expected)");
- // Convert extension field names to camel case notation if the override is set
- var name = def["fields"][i]["name"];
- if (this.options['convertFieldsToCamelCase'])
- name = Reflect.Message.Field._toCamelCase(def["fields"][i]["name"]);
- // see #161: Extensions use their fully qualified name as their runtime key and...
- fld = new Reflect.Message.ExtensionField(this, obj, def["fields"][i]["rule"], def["fields"][i]["type"], this.ptr.fqn()+'.'+name, def["fields"][i]["id"], def["fields"][i]["options"]);
- // ...are added on top of the current namespace as an extension which is used for
- // resolving their type later on (the extension always keeps the original name to
- // prevent naming collisions)
- var ext = new Reflect.Extension(this, this.ptr, def["fields"][i]["name"], fld);
- fld.extension = ext;
- this.ptr.addChild(ext);
- obj.addChild(fld);
- }
- } else if (!/\.?google\.protobuf\./.test(def["ref"])) // Silently skip internal extensions
- throw Error("Extended message "+def["ref"]+" is not defined");
- } else
- throw Error("Not a valid definition: "+JSON.stringify(def));
- def = null;
- }
- // Break goes here
- } else
- throw Error("Not a valid namespace: "+JSON.stringify(defs));
- defs = null;
- this.ptr = this.ptr.parent; // This namespace is s done
- }
- this.resolved = false; // Require re-resolve
- this.result = null; // Require re-build
- return this;
- };
- /**
- * Imports another definition into this builder.
- * @param {Object.<string,*>} json Parsed import
- * @param {(string|{root: string, file: string})=} filename Imported file name
- * @return {ProtoBuf.Builder} this
- * @throws {Error} If the definition or file cannot be imported
- * @expose
- */
- BuilderPrototype["import"] = function(json, filename) {
- if (typeof filename === 'string') {
- if (ProtoBuf.Util.IS_NODE)
- filename = require("path")['resolve'](filename);
- if (this.files[filename] === true) {
- this.reset();
- return this; // Skip duplicate imports
- }
- this.files[filename] = true;
- }
- if (!!json['imports'] && json['imports'].length > 0) {
- var importRoot, delim = '/', resetRoot = false;
- if (typeof filename === 'object') { // If an import root is specified, override
- this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
- importRoot = this.importRoot;
- filename = filename["file"];
- if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0) delim = '\\';
- } else if (typeof filename === 'string') {
- if (this.importRoot) // If import root is overridden, use it
- importRoot = this.importRoot;
- else { // Otherwise compute from filename
- if (filename.indexOf("/") >= 0) { // Unix
- importRoot = filename.replace(/\/[^\/]*$/, "");
- if (/* /file.proto */ importRoot === "")
- importRoot = "/";
- } else if (filename.indexOf("\\") >= 0) { // Windows
- importRoot = filename.replace(/\\[^\\]*$/, "");
- delim = '\\';
- } else
- importRoot = ".";
- }
- } else
- importRoot = null;
- for (var i=0; i<json['imports'].length; i++) {
- if (typeof json['imports'][i] === 'string') { // Import file
- if (!importRoot)
- throw Error("Cannot determine import root: File name is unknown");
- var importFilename = json['imports'][i];
- if (/^google\/protobuf\//.test(importFilename))
- continue; // Not needed and therefore not used
- importFilename = importRoot+delim+importFilename;
- if (this.files[importFilename] === true)
- continue; // Already imported
- if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a NOPARSE build
- importFilename = importFilename.replace(/\.proto$/, ".json"); // always load the JSON file
- var contents = ProtoBuf.Util.fetch(importFilename);
- if (contents === null)
- throw Error("Failed to import '"+importFilename+"' in '"+filename+"': File not found");
- if (/\.json$/i.test(importFilename)) // Always possible
- this["import"](JSON.parse(contents+""), importFilename); // May throw
- else
- this["import"]((new ProtoBuf.DotProto.Parser(contents+"")).parse(), importFilename); // May throw
- } else // Import structure
- if (!filename)
- this["import"](json['imports'][i]);
- else if (/\.(\w+)$/.test(filename)) // With extension: Append _importN to the name portion to make it unique
- this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)$/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
- else // Without extension: Append _importN to make it unique
- this["import"](json['imports'][i], filename+"_import"+i);
- }
- if (resetRoot) // Reset import root override when all imports are done
- this.importRoot = null;
- }
- if (json['messages']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['messages']);
- this.reset();
- }
- if (json['enums']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['enums']);
- this.reset();
- }
- if (json['services']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['services']);
- this.reset();
- }
- if (json['extends']) {
- if (json['package'])
- this.define(json['package'], json["options"]);
- this.create(json['extends']);
- this.reset();
- }
- return this;
- };
- /**
- * Tests if a definition is a valid service definition.
- * @param {Object} def Definition
- * @return {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidService = function(def) {
- // Services require a string name and an rpc object
- return !(typeof def["name"] !== 'string' || !Lang.NAME.test(def["name"]) || typeof def["rpc"] !== 'object');
- };
- /**
- * Tests if a definition is a valid extension.
- * @param {Object} def Definition
- * @returns {boolean} true if valid, else false
- * @expose
- */
- Builder.isValidExtend = function(def) {
- if (typeof def["ref"] !== 'string' || !Lang.TYPEREF.test(def["ref"]))
- return false;
- var i;
- if (typeof def["fields"] !== 'undefined') {
- if (!ProtoBuf.Util.isArray(def["fields"]))
- return false;
- var ids = [], id; // IDs must be unique (does not yet test for the extended message's ids)
- for (i=0; i<def["fields"].length; i++) {
- if (!Builder.isValidMessageField(def["fields"][i]))
- return false;
- id = parseInt(def["id"], 10);
- if (ids.indexOf(id) >= 0)
- return false;
- ids.push(id);
- }
- ids = null;
- }
- return true;
- };
- /**
- * Resolves all namespace objects.
- * @throws {Error} If a type cannot be resolved
- * @expose
- */
- BuilderPrototype.resolveAll = function() {
- // Resolve all reflected objects
- var res;
- if (this.ptr == null || typeof this.ptr.type === 'object')
- return; // Done (already resolved)
- if (this.ptr instanceof Reflect.Namespace) {
- // Build all children
- var children = this.ptr.children;
- for (var i= 0, k=children.length; i<k; ++i)
- this.ptr = children[i],
- this.resolveAll();
- } else if (this.ptr instanceof Reflect.Message.Field) {
- if (!Lang.TYPE.test(this.ptr.type)) { // Resolve type...
- if (!Lang.TYPEREF.test(this.ptr.type))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- res = (this.ptr instanceof Reflect.Message.ExtensionField ? this.ptr.extension.parent : this.ptr.parent).resolve(this.ptr.type, true);
- if (!res)
- throw Error("Unresolvable type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- this.ptr.resolvedType = res;
- if (res instanceof Reflect.Enum)
- this.ptr.type = ProtoBuf.TYPES["enum"];
- else if (res instanceof Reflect.Message)
- this.ptr.type = res.isGroup ? ProtoBuf.TYPES["group"] : ProtoBuf.TYPES["message"];
- else
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
- } else
- this.ptr.type = ProtoBuf.TYPES[this.ptr.type];
- } else if (this.ptr instanceof ProtoBuf.Reflect.Enum.Value) {
- // No need to build enum values (built in enum)
- } else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
- if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
- res = this.ptr.parent.resolve(this.ptr.requestName);
- if (!res || !(res instanceof ProtoBuf.Reflect.Message))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.requestName);
- this.ptr.resolvedRequestType = res;
- res = this.ptr.parent.resolve(this.ptr.responseName);
- if (!res || !(res instanceof ProtoBuf.Reflect.Message))
- throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.responseName);
- this.ptr.resolvedResponseType = res;
- } else {
- // Should not happen as nothing else is implemented
- throw Error("Illegal service type in "+this.ptr.toString(true));
- }
- } else if (!(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && !(this.ptr instanceof ProtoBuf.Reflect.Extension))
- throw Error("Illegal object in namespace: "+typeof(this.ptr)+":"+this.ptr);
- this.reset();
- };
- /**
- * Builds the protocol. This will first try to resolve all definitions and, if this has been successful,
- * return the built package.
- * @param {string=} path Specifies what to return. If omitted, the entire namespace will be returned.
- * @return {ProtoBuf.Builder.Message|Object.<string,*>}
- * @throws {Error} If a type could not be resolved
- * @expose
- */
- BuilderPrototype.build = function(path) {
- this.reset();
- if (!this.resolved)
- this.resolveAll(),
- this.resolved = true,
- this.result = null; // Require re-build
- if (this.result == null) // (Re-)Build
- this.result = this.ns.build();
- if (!path)
- return this.result;
- else {
- var part = path.split(".");
- var ptr = this.result; // Build namespace pointer (no hasChild etc.)
- for (var i=0; i<part.length; i++)
- if (ptr[part[i]])
- ptr = ptr[part[i]];
- else {
- ptr = null;
- break;
- }
- return ptr;
- }
- };
- /**
- * Similar to {@link ProtoBuf.Builder#build}, but looks up the internal reflection descriptor.
- * @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
- * @return {ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
- */
- BuilderPrototype.lookup = function(path) {
- return path ? this.ns.resolve(path) : this.ns;
- };
- /**
- * Returns a string representation of this object.
- * @return {string} String representation as of "Builder"
- * @expose
- */
- BuilderPrototype.toString = function() {
- return "Builder";
- };
- // Pseudo types documented in Reflect.js.
- // Exist for the sole purpose of being able to "... instanceof ProtoBuf.Builder.Message" etc.
- Builder.Message = function() {};
- Builder.Service = function() {};
- return Builder;
- })(ProtoBuf, ProtoBuf.Lang, ProtoBuf.Reflect);
- /**
- * Loads a .proto string and returns the Builder.
- * @param {string} proto .proto file contents
- * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.loadProto = function(proto, builder, filename) {
- if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
- filename = builder,
- builder = undefined;
- return ProtoBuf.loadJson((new ProtoBuf.DotProto.Parser(proto)).parse(), builder, filename);
- };
- /**
- * Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.
- * @function
- * @param {string} proto .proto file contents
- * @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy
- /**
- * Loads a .proto file and returns the Builder.
- * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.loadProtoFile = function(filename, callback, builder) {
- if (callback && typeof callback === 'object')
- builder = callback,
- callback = null;
- else if (!callback || typeof callback !== 'function')
- callback = null;
- if (callback)
- return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
- if (contents === null) {
- callback(Error("Failed to fetch file"));
- return;
- }
- try {
- callback(null, ProtoBuf.loadProto(contents, builder, filename));
- } catch (e) {
- callback(e);
- }
- });
- var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
- return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
- };
- /**
- * Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.
- * @function
- * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy
- /**
- * Constructs a new empty Builder.
- * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
- * @return {!ProtoBuf.Builder} Builder
- * @expose
- */
- ProtoBuf.newBuilder = function(options) {
- options = options || {};
- if (typeof options['convertFieldsToCamelCase'] === 'undefined')
- options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
- if (typeof options['populateAccessors'] === 'undefined')
- options['populateAccessors'] = ProtoBuf.populateAccessors;
- return new ProtoBuf.Builder(options);
- };
- /**
- * Loads a .json definition and returns the Builder.
- * @param {!*|string} json JSON definition
- * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
- * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
- * @return {ProtoBuf.Builder} Builder to create new messages
- * @throws {Error} If the definition cannot be parsed or built
- * @expose
- */
- ProtoBuf.loadJson = function(json, builder, filename) {
- if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
- filename = builder,
- builder = null;
- if (!builder || typeof builder !== 'object')
- builder = ProtoBuf.newBuilder();
- if (typeof json === 'string')
- json = JSON.parse(json);
- builder["import"](json, filename);
- builder.resolveAll();
- builder.build();
- return builder;
- };
- /**
- * Loads a .json file and returns the Builder.
- * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
- * an overridden 'root' path for all imported files.
- * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
- * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
- * file will be read synchronously and this function will return the Builder.
- * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
- * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
- * request has failed), else undefined
- * @expose
- */
- ProtoBuf.loadJsonFile = function(filename, callback, builder) {
- if (callback && typeof callback === 'object')
- builder = callback,
- callback = null;
- else if (!callback || typeof callback !== 'function')
- callback = null;
- if (callback)
- return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
- if (contents === null) {
- callback(Error("Failed to fetch file"));
- return;
- }
- try {
- callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
- } catch (e) {
- callback(e);
- }
- });
- var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
- return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
- };
- return ProtoBuf;
- }
- /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports)
- module['exports'] = init(require("bytebuffer"));
- /* AMD */ else if (typeof define === 'function' && define["amd"])
- define(["ByteBuffer"], init);
- /* Global */ else
- (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = init(global["dcodeIO"]["ByteBuffer"]);
- })(this);
|