wkx.js 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019
  1. require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. (function (Buffer){
  3. module.exports = BinaryReader;
  4. function BinaryReader(buffer, isBigEndian) {
  5. this.buffer = buffer;
  6. this.position = 0;
  7. this.isBigEndian = isBigEndian || false;
  8. }
  9. function _read(readLE, readBE, size) {
  10. return function () {
  11. var value;
  12. if (this.isBigEndian)
  13. value = readBE.call(this.buffer, this.position);
  14. else
  15. value = readLE.call(this.buffer, this.position);
  16. this.position += size;
  17. return value;
  18. };
  19. }
  20. BinaryReader.prototype.readUInt8 = _read(Buffer.prototype.readUInt8, Buffer.prototype.readUInt8, 1);
  21. BinaryReader.prototype.readUInt16 = _read(Buffer.prototype.readUInt16LE, Buffer.prototype.readUInt16BE, 2);
  22. BinaryReader.prototype.readUInt32 = _read(Buffer.prototype.readUInt32LE, Buffer.prototype.readUInt32BE, 4);
  23. BinaryReader.prototype.readInt8 = _read(Buffer.prototype.readInt8, Buffer.prototype.readInt8, 1);
  24. BinaryReader.prototype.readInt16 = _read(Buffer.prototype.readInt16LE, Buffer.prototype.readInt16BE, 2);
  25. BinaryReader.prototype.readInt32 = _read(Buffer.prototype.readInt32LE, Buffer.prototype.readInt32BE, 4);
  26. BinaryReader.prototype.readFloat = _read(Buffer.prototype.readFloatLE, Buffer.prototype.readFloatBE, 4);
  27. BinaryReader.prototype.readDouble = _read(Buffer.prototype.readDoubleLE, Buffer.prototype.readDoubleBE, 8);
  28. BinaryReader.prototype.readVarInt = function () {
  29. var nextByte,
  30. result = 0,
  31. bytesRead = 0;
  32. do {
  33. nextByte = this.buffer[this.position + bytesRead];
  34. result += (nextByte & 0x7F) << (7 * bytesRead);
  35. bytesRead++;
  36. } while (nextByte >= 0x80);
  37. this.position += bytesRead;
  38. return result;
  39. };
  40. }).call(this,require("buffer").Buffer)
  41. },{"buffer":"buffer"}],2:[function(require,module,exports){
  42. (function (Buffer){
  43. module.exports = BinaryWriter;
  44. function BinaryWriter(size, allowResize) {
  45. this.buffer = new Buffer(size);
  46. this.position = 0;
  47. this.allowResize = allowResize;
  48. }
  49. function _write(write, size) {
  50. return function (value, noAssert) {
  51. this.ensureSize(size);
  52. write.call(this.buffer, value, this.position, noAssert);
  53. this.position += size;
  54. };
  55. }
  56. BinaryWriter.prototype.writeUInt8 = _write(Buffer.prototype.writeUInt8, 1);
  57. BinaryWriter.prototype.writeUInt16LE = _write(Buffer.prototype.writeUInt16LE, 2);
  58. BinaryWriter.prototype.writeUInt16BE = _write(Buffer.prototype.writeUInt16BE, 2);
  59. BinaryWriter.prototype.writeUInt32LE = _write(Buffer.prototype.writeUInt32LE, 4);
  60. BinaryWriter.prototype.writeUInt32BE = _write(Buffer.prototype.writeUInt32BE, 4);
  61. BinaryWriter.prototype.writeInt8 = _write(Buffer.prototype.writeInt8, 1);
  62. BinaryWriter.prototype.writeInt16LE = _write(Buffer.prototype.writeInt16LE, 2);
  63. BinaryWriter.prototype.writeInt16BE = _write(Buffer.prototype.writeInt16BE, 2);
  64. BinaryWriter.prototype.writeInt32LE = _write(Buffer.prototype.writeInt32LE, 4);
  65. BinaryWriter.prototype.writeInt32BE = _write(Buffer.prototype.writeInt32BE, 4);
  66. BinaryWriter.prototype.writeFloatLE = _write(Buffer.prototype.writeFloatLE, 4);
  67. BinaryWriter.prototype.writeFloatBE = _write(Buffer.prototype.writeFloatBE, 4);
  68. BinaryWriter.prototype.writeDoubleLE = _write(Buffer.prototype.writeDoubleLE, 8);
  69. BinaryWriter.prototype.writeDoubleBE = _write(Buffer.prototype.writeDoubleBE, 8);
  70. BinaryWriter.prototype.writeBuffer = function (buffer) {
  71. this.ensureSize(buffer.length);
  72. buffer.copy(this.buffer, this.position, 0, buffer.length);
  73. this.position += buffer.length;
  74. };
  75. BinaryWriter.prototype.writeVarInt = function (value) {
  76. var length = 1;
  77. while ((value & 0xFFFFFF80) !== 0) {
  78. this.writeUInt8((value & 0x7F) | 0x80);
  79. value >>>= 7;
  80. length++;
  81. }
  82. this.writeUInt8(value & 0x7F);
  83. return length;
  84. };
  85. BinaryWriter.prototype.ensureSize = function (size) {
  86. if (this.buffer.length < this.position + size) {
  87. if (this.allowResize) {
  88. var tempBuffer = new Buffer(this.position + size);
  89. this.buffer.copy(tempBuffer, 0, 0, this.buffer.length);
  90. this.buffer = tempBuffer;
  91. }
  92. else {
  93. throw new RangeError('index out of range');
  94. }
  95. }
  96. };
  97. }).call(this,require("buffer").Buffer)
  98. },{"buffer":"buffer"}],3:[function(require,module,exports){
  99. (function (Buffer){
  100. module.exports = Geometry;
  101. var Types = require('./types');
  102. var Point = require('./point');
  103. var LineString = require('./linestring');
  104. var Polygon = require('./polygon');
  105. var MultiPoint = require('./multipoint');
  106. var MultiLineString = require('./multilinestring');
  107. var MultiPolygon = require('./multipolygon');
  108. var GeometryCollection = require('./geometrycollection');
  109. var BinaryReader = require('./binaryreader');
  110. var BinaryWriter = require('./binarywriter');
  111. var WktParser = require('./wktparser');
  112. var ZigZag = require('./zigzag.js');
  113. function Geometry() {
  114. this.srid = undefined;
  115. this.hasZ = false;
  116. this.hasM = false;
  117. }
  118. Geometry.parse = function (value, options) {
  119. var valueType = typeof value;
  120. if (valueType === 'string' || value instanceof WktParser)
  121. return Geometry._parseWkt(value);
  122. else if (Buffer.isBuffer(value) || value instanceof BinaryReader)
  123. return Geometry._parseWkb(value, options);
  124. else
  125. throw new Error('first argument must be a string or Buffer');
  126. };
  127. Geometry._parseWkt = function (value) {
  128. var wktParser,
  129. srid;
  130. if (value instanceof WktParser)
  131. wktParser = value;
  132. else
  133. wktParser = new WktParser(value);
  134. var match = wktParser.matchRegex([/^SRID=(\d+);/]);
  135. if (match)
  136. srid = parseInt(match[1], 10);
  137. var geometryType = wktParser.matchType();
  138. var dimension = wktParser.matchDimension();
  139. var options = {
  140. srid: srid,
  141. hasZ: dimension.hasZ,
  142. hasM: dimension.hasM
  143. };
  144. switch (geometryType) {
  145. case Types.wkt.Point:
  146. return Point._parseWkt(wktParser, options);
  147. case Types.wkt.LineString:
  148. return LineString._parseWkt(wktParser, options);
  149. case Types.wkt.Polygon:
  150. return Polygon._parseWkt(wktParser, options);
  151. case Types.wkt.MultiPoint:
  152. return MultiPoint._parseWkt(wktParser, options);
  153. case Types.wkt.MultiLineString:
  154. return MultiLineString._parseWkt(wktParser, options);
  155. case Types.wkt.MultiPolygon:
  156. return MultiPolygon._parseWkt(wktParser, options);
  157. case Types.wkt.GeometryCollection:
  158. return GeometryCollection._parseWkt(wktParser, options);
  159. }
  160. };
  161. Geometry._parseWkb = function (value, parentOptions) {
  162. var binaryReader,
  163. wkbType,
  164. geometryType,
  165. options = {};
  166. if (value instanceof BinaryReader)
  167. binaryReader = value;
  168. else
  169. binaryReader = new BinaryReader(value);
  170. binaryReader.isBigEndian = !binaryReader.readInt8();
  171. wkbType = binaryReader.readUInt32();
  172. options.hasSrid = (wkbType & 0x20000000) === 0x20000000;
  173. options.isEwkb = (wkbType & 0x20000000) || (wkbType & 0x40000000) || (wkbType & 0x80000000);
  174. if (options.hasSrid)
  175. options.srid = binaryReader.readUInt32();
  176. options.hasZ = false;
  177. options.hasM = false;
  178. if (!options.isEwkb && (!parentOptions || !parentOptions.isEwkb)) {
  179. if (wkbType >= 1000 && wkbType < 2000) {
  180. options.hasZ = true;
  181. geometryType = wkbType - 1000;
  182. }
  183. else if (wkbType >= 2000 && wkbType < 3000) {
  184. options.hasM = true;
  185. geometryType = wkbType - 2000;
  186. }
  187. else if (wkbType >= 3000 && wkbType < 4000) {
  188. options.hasZ = true;
  189. options.hasM = true;
  190. geometryType = wkbType - 3000;
  191. }
  192. else {
  193. geometryType = wkbType;
  194. }
  195. }
  196. else {
  197. if (wkbType & 0x80000000)
  198. options.hasZ = true;
  199. if (wkbType & 0x40000000)
  200. options.hasM = true;
  201. geometryType = wkbType & 0xF;
  202. }
  203. switch (geometryType) {
  204. case Types.wkb.Point:
  205. return Point._parseWkb(binaryReader, options);
  206. case Types.wkb.LineString:
  207. return LineString._parseWkb(binaryReader, options);
  208. case Types.wkb.Polygon:
  209. return Polygon._parseWkb(binaryReader, options);
  210. case Types.wkb.MultiPoint:
  211. return MultiPoint._parseWkb(binaryReader, options);
  212. case Types.wkb.MultiLineString:
  213. return MultiLineString._parseWkb(binaryReader, options);
  214. case Types.wkb.MultiPolygon:
  215. return MultiPolygon._parseWkb(binaryReader, options);
  216. case Types.wkb.GeometryCollection:
  217. return GeometryCollection._parseWkb(binaryReader, options);
  218. default:
  219. throw new Error('GeometryType ' + geometryType + ' not supported');
  220. }
  221. };
  222. Geometry.parseTwkb = function (value) {
  223. var binaryReader,
  224. options = {};
  225. if (value instanceof BinaryReader)
  226. binaryReader = value;
  227. else
  228. binaryReader = new BinaryReader(value);
  229. var type = binaryReader.readUInt8();
  230. var metadataHeader = binaryReader.readUInt8();
  231. var geometryType = type & 0x0F;
  232. options.precision = ZigZag.decode(type >> 4);
  233. options.precisionFactor = Math.pow(10, options.precision);
  234. options.hasBoundingBox = metadataHeader >> 0 & 1;
  235. options.hasSizeAttribute = metadataHeader >> 1 & 1;
  236. options.hasIdList = metadataHeader >> 2 & 1;
  237. options.hasExtendedPrecision = metadataHeader >> 3 & 1;
  238. options.isEmpty = metadataHeader >> 4 & 1;
  239. if (options.hasExtendedPrecision) {
  240. var extendedPrecision = binaryReader.readUInt8();
  241. options.hasZ = (extendedPrecision & 0x01) === 0x01;
  242. options.hasM = (extendedPrecision & 0x02) === 0x02;
  243. options.zPrecision = ZigZag.decode((extendedPrecision & 0x1C) >> 2);
  244. options.zPrecisionFactor = Math.pow(10, options.zPrecision);
  245. options.mPrecision = ZigZag.decode((extendedPrecision & 0xE0) >> 5);
  246. options.mPrecisionFactor = Math.pow(10, options.mPrecision);
  247. }
  248. else {
  249. options.hasZ = false;
  250. options.hasM = false;
  251. }
  252. if (options.hasSizeAttribute)
  253. binaryReader.readVarInt();
  254. if (options.hasBoundingBox) {
  255. var dimensions = 2;
  256. if (options.hasZ)
  257. dimensions++;
  258. if (options.hasM)
  259. dimensions++;
  260. for (var i = 0; i < dimensions; i++) {
  261. binaryReader.readVarInt();
  262. binaryReader.readVarInt();
  263. }
  264. }
  265. switch (geometryType) {
  266. case Types.wkb.Point:
  267. return Point._parseTwkb(binaryReader, options);
  268. case Types.wkb.LineString:
  269. return LineString._parseTwkb(binaryReader, options);
  270. case Types.wkb.Polygon:
  271. return Polygon._parseTwkb(binaryReader, options);
  272. case Types.wkb.MultiPoint:
  273. return MultiPoint._parseTwkb(binaryReader, options);
  274. case Types.wkb.MultiLineString:
  275. return MultiLineString._parseTwkb(binaryReader, options);
  276. case Types.wkb.MultiPolygon:
  277. return MultiPolygon._parseTwkb(binaryReader, options);
  278. case Types.wkb.GeometryCollection:
  279. return GeometryCollection._parseTwkb(binaryReader, options);
  280. default:
  281. throw new Error('GeometryType ' + geometryType + ' not supported');
  282. }
  283. };
  284. Geometry.parseGeoJSON = function (value) {
  285. return Geometry._parseGeoJSON(value);
  286. };
  287. Geometry._parseGeoJSON = function (value, isSubGeometry) {
  288. var geometry;
  289. switch (value.type) {
  290. case Types.geoJSON.Point:
  291. geometry = Point._parseGeoJSON(value); break;
  292. case Types.geoJSON.LineString:
  293. geometry = LineString._parseGeoJSON(value); break;
  294. case Types.geoJSON.Polygon:
  295. geometry = Polygon._parseGeoJSON(value); break;
  296. case Types.geoJSON.MultiPoint:
  297. geometry = MultiPoint._parseGeoJSON(value); break;
  298. case Types.geoJSON.MultiLineString:
  299. geometry = MultiLineString._parseGeoJSON(value); break;
  300. case Types.geoJSON.MultiPolygon:
  301. geometry = MultiPolygon._parseGeoJSON(value); break;
  302. case Types.geoJSON.GeometryCollection:
  303. geometry = GeometryCollection._parseGeoJSON(value); break;
  304. default:
  305. throw new Error('GeometryType ' + value.type + ' not supported');
  306. }
  307. if (value.crs && value.crs.type && value.crs.type === 'name' && value.crs.properties && value.crs.properties.name) {
  308. var crs = value.crs.properties.name;
  309. if (crs.indexOf('EPSG:') === 0)
  310. geometry.srid = parseInt(crs.substring(5));
  311. else if (crs.indexOf('urn:ogc:def:crs:EPSG::') === 0)
  312. geometry.srid = parseInt(crs.substring(22));
  313. else
  314. throw new Error('Unsupported crs: ' + crs);
  315. }
  316. else if (!isSubGeometry) {
  317. geometry.srid = 4326;
  318. }
  319. return geometry;
  320. };
  321. Geometry.prototype.toEwkt = function () {
  322. return 'SRID=' + this.srid + ';' + this.toWkt();
  323. };
  324. Geometry.prototype.toEwkb = function () {
  325. var ewkb = new BinaryWriter(this._getWkbSize() + 4);
  326. var wkb = this.toWkb();
  327. ewkb.writeInt8(1);
  328. ewkb.writeUInt32LE((wkb.slice(1, 5).readUInt32LE(0) | 0x20000000) >>> 0, true);
  329. ewkb.writeUInt32LE(this.srid);
  330. ewkb.writeBuffer(wkb.slice(5));
  331. return ewkb.buffer;
  332. };
  333. Geometry.prototype._getWktType = function (wktType, isEmpty) {
  334. var wkt = wktType;
  335. if (this.hasZ && this.hasM)
  336. wkt += ' ZM ';
  337. else if (this.hasZ)
  338. wkt += ' Z ';
  339. else if (this.hasM)
  340. wkt += ' M ';
  341. if (isEmpty && !this.hasZ && !this.hasM)
  342. wkt += ' ';
  343. if (isEmpty)
  344. wkt += 'EMPTY';
  345. return wkt;
  346. };
  347. Geometry.prototype._getWktCoordinate = function (point) {
  348. var coordinates = point.x + ' ' + point.y;
  349. if (this.hasZ)
  350. coordinates += ' ' + point.z;
  351. if (this.hasM)
  352. coordinates += ' ' + point.m;
  353. return coordinates;
  354. };
  355. Geometry.prototype._writeWkbType = function (wkb, geometryType, parentOptions) {
  356. var dimensionType = 0;
  357. if (typeof this.srid === 'undefined' && (!parentOptions || typeof parentOptions.srid === 'undefined')) {
  358. if (this.hasZ && this.hasM)
  359. dimensionType += 3000;
  360. else if (this.hasZ)
  361. dimensionType += 1000;
  362. else if (this.hasM)
  363. dimensionType += 2000;
  364. }
  365. else {
  366. if (this.hasZ)
  367. dimensionType |= 0x80000000;
  368. if (this.hasM)
  369. dimensionType |= 0x40000000;
  370. }
  371. wkb.writeUInt32LE((dimensionType + geometryType) >>> 0, true);
  372. };
  373. Geometry.getTwkbPrecision = function (xyPrecision, zPrecision, mPrecision) {
  374. return {
  375. xy: xyPrecision,
  376. z: zPrecision,
  377. m: mPrecision,
  378. xyFactor: Math.pow(10, xyPrecision),
  379. zFactor: Math.pow(10, zPrecision),
  380. mFactor: Math.pow(10, mPrecision)
  381. };
  382. };
  383. Geometry.prototype._writeTwkbHeader = function (twkb, geometryType, precision, isEmpty) {
  384. var type = (ZigZag.encode(precision.xy) << 4) + geometryType;
  385. var metadataHeader = (this.hasZ || this.hasM) << 3;
  386. metadataHeader += isEmpty << 4;
  387. twkb.writeUInt8(type);
  388. twkb.writeUInt8(metadataHeader);
  389. if (this.hasZ || this.hasM) {
  390. var extendedPrecision = 0;
  391. if (this.hasZ)
  392. extendedPrecision |= 0x1;
  393. if (this.hasM)
  394. extendedPrecision |= 0x2;
  395. twkb.writeUInt8(extendedPrecision);
  396. }
  397. };
  398. Geometry.prototype.toGeoJSON = function (options) {
  399. var geoJSON = {};
  400. if (this.srid) {
  401. if (options) {
  402. if (options.shortCrs) {
  403. geoJSON.crs = {
  404. type: 'name',
  405. properties: {
  406. name: 'EPSG:' + this.srid
  407. }
  408. };
  409. }
  410. else if (options.longCrs) {
  411. geoJSON.crs = {
  412. type: 'name',
  413. properties: {
  414. name: 'urn:ogc:def:crs:EPSG::' + this.srid
  415. }
  416. };
  417. }
  418. }
  419. }
  420. return geoJSON;
  421. };
  422. }).call(this,{"isBuffer":require("../node_modules/is-buffer/index.js")})
  423. },{"../node_modules/is-buffer/index.js":17,"./binaryreader":1,"./binarywriter":2,"./geometrycollection":4,"./linestring":5,"./multilinestring":6,"./multipoint":7,"./multipolygon":8,"./point":9,"./polygon":10,"./types":11,"./wktparser":12,"./zigzag.js":13}],4:[function(require,module,exports){
  424. module.exports = GeometryCollection;
  425. var util = require('util');
  426. var Types = require('./types');
  427. var Geometry = require('./geometry');
  428. var BinaryWriter = require('./binarywriter');
  429. function GeometryCollection(geometries, srid) {
  430. Geometry.call(this);
  431. this.geometries = geometries || [];
  432. this.srid = srid;
  433. if (this.geometries.length > 0) {
  434. this.hasZ = this.geometries[0].hasZ;
  435. this.hasM = this.geometries[0].hasM;
  436. }
  437. }
  438. util.inherits(GeometryCollection, Geometry);
  439. GeometryCollection.Z = function (geometries, srid) {
  440. var geometryCollection = new GeometryCollection(geometries, srid);
  441. geometryCollection.hasZ = true;
  442. return geometryCollection;
  443. };
  444. GeometryCollection.M = function (geometries, srid) {
  445. var geometryCollection = new GeometryCollection(geometries, srid);
  446. geometryCollection.hasM = true;
  447. return geometryCollection;
  448. };
  449. GeometryCollection.ZM = function (geometries, srid) {
  450. var geometryCollection = new GeometryCollection(geometries, srid);
  451. geometryCollection.hasZ = true;
  452. geometryCollection.hasM = true;
  453. return geometryCollection;
  454. };
  455. GeometryCollection._parseWkt = function (value, options) {
  456. var geometryCollection = new GeometryCollection();
  457. geometryCollection.srid = options.srid;
  458. geometryCollection.hasZ = options.hasZ;
  459. geometryCollection.hasM = options.hasM;
  460. if (value.isMatch(['EMPTY']))
  461. return geometryCollection;
  462. value.expectGroupStart();
  463. do {
  464. geometryCollection.geometries.push(Geometry.parse(value));
  465. } while (value.isMatch([',']));
  466. value.expectGroupEnd();
  467. return geometryCollection;
  468. };
  469. GeometryCollection._parseWkb = function (value, options) {
  470. var geometryCollection = new GeometryCollection();
  471. geometryCollection.srid = options.srid;
  472. geometryCollection.hasZ = options.hasZ;
  473. geometryCollection.hasM = options.hasM;
  474. var geometryCount = value.readUInt32();
  475. for (var i = 0; i < geometryCount; i++)
  476. geometryCollection.geometries.push(Geometry.parse(value, options));
  477. return geometryCollection;
  478. };
  479. GeometryCollection._parseTwkb = function (value, options) {
  480. var geometryCollection = new GeometryCollection();
  481. geometryCollection.hasZ = options.hasZ;
  482. geometryCollection.hasM = options.hasM;
  483. if (options.isEmpty)
  484. return geometryCollection;
  485. var geometryCount = value.readVarInt();
  486. for (var i = 0; i < geometryCount; i++)
  487. geometryCollection.geometries.push(Geometry.parseTwkb(value));
  488. return geometryCollection;
  489. };
  490. GeometryCollection._parseGeoJSON = function (value) {
  491. var geometryCollection = new GeometryCollection();
  492. for (var i = 0; i < value.geometries.length; i++)
  493. geometryCollection.geometries.push(Geometry._parseGeoJSON(value.geometries[i], true));
  494. if (geometryCollection.geometries.length > 0)
  495. geometryCollection.hasZ = geometryCollection.geometries[0].hasZ;
  496. return geometryCollection;
  497. };
  498. GeometryCollection.prototype.toWkt = function () {
  499. if (this.geometries.length === 0)
  500. return this._getWktType(Types.wkt.GeometryCollection, true);
  501. var wkt = this._getWktType(Types.wkt.GeometryCollection, false) + '(';
  502. for (var i = 0; i < this.geometries.length; i++)
  503. wkt += this.geometries[i].toWkt() + ',';
  504. wkt = wkt.slice(0, -1);
  505. wkt += ')';
  506. return wkt;
  507. };
  508. GeometryCollection.prototype.toWkb = function () {
  509. var wkb = new BinaryWriter(this._getWkbSize());
  510. wkb.writeInt8(1);
  511. this._writeWkbType(wkb, Types.wkb.GeometryCollection);
  512. wkb.writeUInt32LE(this.geometries.length);
  513. for (var i = 0; i < this.geometries.length; i++)
  514. wkb.writeBuffer(this.geometries[i].toWkb({ srid: this.srid }));
  515. return wkb.buffer;
  516. };
  517. GeometryCollection.prototype.toTwkb = function () {
  518. var twkb = new BinaryWriter(0, true);
  519. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  520. var isEmpty = this.geometries.length === 0;
  521. this._writeTwkbHeader(twkb, Types.wkb.GeometryCollection, precision, isEmpty);
  522. if (this.geometries.length > 0) {
  523. twkb.writeVarInt(this.geometries.length);
  524. for (var i = 0; i < this.geometries.length; i++)
  525. twkb.writeBuffer(this.geometries[i].toTwkb());
  526. }
  527. return twkb.buffer;
  528. };
  529. GeometryCollection.prototype._getWkbSize = function () {
  530. var size = 1 + 4 + 4;
  531. for (var i = 0; i < this.geometries.length; i++)
  532. size += this.geometries[i]._getWkbSize();
  533. return size;
  534. };
  535. GeometryCollection.prototype.toGeoJSON = function (options) {
  536. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  537. geoJSON.type = Types.geoJSON.GeometryCollection;
  538. geoJSON.geometries = [];
  539. for (var i = 0; i < this.geometries.length; i++)
  540. geoJSON.geometries.push(this.geometries[i].toGeoJSON());
  541. return geoJSON;
  542. };
  543. },{"./binarywriter":2,"./geometry":3,"./types":11,"util":20}],5:[function(require,module,exports){
  544. module.exports = LineString;
  545. var util = require('util');
  546. var Geometry = require('./geometry');
  547. var Types = require('./types');
  548. var Point = require('./point');
  549. var BinaryWriter = require('./binarywriter');
  550. function LineString(points, srid) {
  551. Geometry.call(this);
  552. this.points = points || [];
  553. this.srid = srid;
  554. if (this.points.length > 0) {
  555. this.hasZ = this.points[0].hasZ;
  556. this.hasM = this.points[0].hasM;
  557. }
  558. }
  559. util.inherits(LineString, Geometry);
  560. LineString.Z = function (points, srid) {
  561. var lineString = new LineString(points, srid);
  562. lineString.hasZ = true;
  563. return lineString;
  564. };
  565. LineString.M = function (points, srid) {
  566. var lineString = new LineString(points, srid);
  567. lineString.hasM = true;
  568. return lineString;
  569. };
  570. LineString.ZM = function (points, srid) {
  571. var lineString = new LineString(points, srid);
  572. lineString.hasZ = true;
  573. lineString.hasM = true;
  574. return lineString;
  575. };
  576. LineString._parseWkt = function (value, options) {
  577. var lineString = new LineString();
  578. lineString.srid = options.srid;
  579. lineString.hasZ = options.hasZ;
  580. lineString.hasM = options.hasM;
  581. if (value.isMatch(['EMPTY']))
  582. return lineString;
  583. value.expectGroupStart();
  584. lineString.points.push.apply(lineString.points, value.matchCoordinates(options));
  585. value.expectGroupEnd();
  586. return lineString;
  587. };
  588. LineString._parseWkb = function (value, options) {
  589. var lineString = new LineString();
  590. lineString.srid = options.srid;
  591. lineString.hasZ = options.hasZ;
  592. lineString.hasM = options.hasM;
  593. var pointCount = value.readUInt32();
  594. for (var i = 0; i < pointCount; i++)
  595. lineString.points.push(Point._readWkbPoint(value, options));
  596. return lineString;
  597. };
  598. LineString._parseTwkb = function (value, options) {
  599. var lineString = new LineString();
  600. lineString.hasZ = options.hasZ;
  601. lineString.hasM = options.hasM;
  602. if (options.isEmpty)
  603. return lineString;
  604. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  605. var pointCount = value.readVarInt();
  606. for (var i = 0; i < pointCount; i++)
  607. lineString.points.push(Point._readTwkbPoint(value, options, previousPoint));
  608. return lineString;
  609. };
  610. LineString._parseGeoJSON = function (value) {
  611. var lineString = new LineString();
  612. if (value.coordinates.length > 0)
  613. lineString.hasZ = value.coordinates[0].length > 2;
  614. for (var i = 0; i < value.coordinates.length; i++)
  615. lineString.points.push(Point._readGeoJSONPoint(value.coordinates[i]));
  616. return lineString;
  617. };
  618. LineString.prototype.toWkt = function () {
  619. if (this.points.length === 0)
  620. return this._getWktType(Types.wkt.LineString, true);
  621. return this._getWktType(Types.wkt.LineString, false) + this._toInnerWkt();
  622. };
  623. LineString.prototype._toInnerWkt = function () {
  624. var innerWkt = '(';
  625. for (var i = 0; i < this.points.length; i++)
  626. innerWkt += this._getWktCoordinate(this.points[i]) + ',';
  627. innerWkt = innerWkt.slice(0, -1);
  628. innerWkt += ')';
  629. return innerWkt;
  630. };
  631. LineString.prototype.toWkb = function (parentOptions) {
  632. var wkb = new BinaryWriter(this._getWkbSize());
  633. wkb.writeInt8(1);
  634. this._writeWkbType(wkb, Types.wkb.LineString, parentOptions);
  635. wkb.writeUInt32LE(this.points.length);
  636. for (var i = 0; i < this.points.length; i++)
  637. this.points[i]._writeWkbPoint(wkb);
  638. return wkb.buffer;
  639. };
  640. LineString.prototype.toTwkb = function () {
  641. var twkb = new BinaryWriter(0, true);
  642. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  643. var isEmpty = this.points.length === 0;
  644. this._writeTwkbHeader(twkb, Types.wkb.LineString, precision, isEmpty);
  645. if (this.points.length > 0) {
  646. twkb.writeVarInt(this.points.length);
  647. var previousPoint = new Point(0, 0, 0, 0);
  648. for (var i = 0; i < this.points.length; i++)
  649. this.points[i]._writeTwkbPoint(twkb, precision, previousPoint);
  650. }
  651. return twkb.buffer;
  652. };
  653. LineString.prototype._getWkbSize = function () {
  654. var coordinateSize = 16;
  655. if (this.hasZ)
  656. coordinateSize += 8;
  657. if (this.hasM)
  658. coordinateSize += 8;
  659. return 1 + 4 + 4 + (this.points.length * coordinateSize);
  660. };
  661. LineString.prototype.toGeoJSON = function (options) {
  662. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  663. geoJSON.type = Types.geoJSON.LineString;
  664. geoJSON.coordinates = [];
  665. for (var i = 0; i < this.points.length; i++) {
  666. if (this.hasZ)
  667. geoJSON.coordinates.push([this.points[i].x, this.points[i].y, this.points[i].z]);
  668. else
  669. geoJSON.coordinates.push([this.points[i].x, this.points[i].y]);
  670. }
  671. return geoJSON;
  672. };
  673. },{"./binarywriter":2,"./geometry":3,"./point":9,"./types":11,"util":20}],6:[function(require,module,exports){
  674. module.exports = MultiLineString;
  675. var util = require('util');
  676. var Types = require('./types');
  677. var Geometry = require('./geometry');
  678. var Point = require('./point');
  679. var LineString = require('./linestring');
  680. var BinaryWriter = require('./binarywriter');
  681. function MultiLineString(lineStrings, srid) {
  682. Geometry.call(this);
  683. this.lineStrings = lineStrings || [];
  684. this.srid = srid;
  685. if (this.lineStrings.length > 0) {
  686. this.hasZ = this.lineStrings[0].hasZ;
  687. this.hasM = this.lineStrings[0].hasM;
  688. }
  689. }
  690. util.inherits(MultiLineString, Geometry);
  691. MultiLineString.Z = function (lineStrings, srid) {
  692. var multiLineString = new MultiLineString(lineStrings, srid);
  693. multiLineString.hasZ = true;
  694. return multiLineString;
  695. };
  696. MultiLineString.M = function (lineStrings, srid) {
  697. var multiLineString = new MultiLineString(lineStrings, srid);
  698. multiLineString.hasM = true;
  699. return multiLineString;
  700. };
  701. MultiLineString.ZM = function (lineStrings, srid) {
  702. var multiLineString = new MultiLineString(lineStrings, srid);
  703. multiLineString.hasZ = true;
  704. multiLineString.hasM = true;
  705. return multiLineString;
  706. };
  707. MultiLineString._parseWkt = function (value, options) {
  708. var multiLineString = new MultiLineString();
  709. multiLineString.srid = options.srid;
  710. multiLineString.hasZ = options.hasZ;
  711. multiLineString.hasM = options.hasM;
  712. if (value.isMatch(['EMPTY']))
  713. return multiLineString;
  714. value.expectGroupStart();
  715. do {
  716. value.expectGroupStart();
  717. multiLineString.lineStrings.push(new LineString(value.matchCoordinates(options)));
  718. value.expectGroupEnd();
  719. } while (value.isMatch([',']));
  720. value.expectGroupEnd();
  721. return multiLineString;
  722. };
  723. MultiLineString._parseWkb = function (value, options) {
  724. var multiLineString = new MultiLineString();
  725. multiLineString.srid = options.srid;
  726. multiLineString.hasZ = options.hasZ;
  727. multiLineString.hasM = options.hasM;
  728. var lineStringCount = value.readUInt32();
  729. for (var i = 0; i < lineStringCount; i++)
  730. multiLineString.lineStrings.push(Geometry.parse(value, options));
  731. return multiLineString;
  732. };
  733. MultiLineString._parseTwkb = function (value, options) {
  734. var multiLineString = new MultiLineString();
  735. multiLineString.hasZ = options.hasZ;
  736. multiLineString.hasM = options.hasM;
  737. if (options.isEmpty)
  738. return multiLineString;
  739. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  740. var lineStringCount = value.readVarInt();
  741. for (var i = 0; i < lineStringCount; i++) {
  742. var lineString = new LineString();
  743. lineString.hasZ = options.hasZ;
  744. lineString.hasM = options.hasM;
  745. var pointCount = value.readVarInt();
  746. for (var j = 0; j < pointCount; j++)
  747. lineString.points.push(Point._readTwkbPoint(value, options, previousPoint));
  748. multiLineString.lineStrings.push(lineString);
  749. }
  750. return multiLineString;
  751. };
  752. MultiLineString._parseGeoJSON = function (value) {
  753. var multiLineString = new MultiLineString();
  754. if (value.coordinates.length > 0 && value.coordinates[0].length > 0)
  755. multiLineString.hasZ = value.coordinates[0][0].length > 2;
  756. for (var i = 0; i < value.coordinates.length; i++)
  757. multiLineString.lineStrings.push(LineString._parseGeoJSON({ coordinates: value.coordinates[i] }));
  758. return multiLineString;
  759. };
  760. MultiLineString.prototype.toWkt = function () {
  761. if (this.lineStrings.length === 0)
  762. return this._getWktType(Types.wkt.MultiLineString, true);
  763. var wkt = this._getWktType(Types.wkt.MultiLineString, false) + '(';
  764. for (var i = 0; i < this.lineStrings.length; i++)
  765. wkt += this.lineStrings[i]._toInnerWkt() + ',';
  766. wkt = wkt.slice(0, -1);
  767. wkt += ')';
  768. return wkt;
  769. };
  770. MultiLineString.prototype.toWkb = function () {
  771. var wkb = new BinaryWriter(this._getWkbSize());
  772. wkb.writeInt8(1);
  773. this._writeWkbType(wkb, Types.wkb.MultiLineString);
  774. wkb.writeUInt32LE(this.lineStrings.length);
  775. for (var i = 0; i < this.lineStrings.length; i++)
  776. wkb.writeBuffer(this.lineStrings[i].toWkb({ srid: this.srid }));
  777. return wkb.buffer;
  778. };
  779. MultiLineString.prototype.toTwkb = function () {
  780. var twkb = new BinaryWriter(0, true);
  781. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  782. var isEmpty = this.lineStrings.length === 0;
  783. this._writeTwkbHeader(twkb, Types.wkb.MultiLineString, precision, isEmpty);
  784. if (this.lineStrings.length > 0) {
  785. twkb.writeVarInt(this.lineStrings.length);
  786. var previousPoint = new Point(0, 0, 0, 0);
  787. for (var i = 0; i < this.lineStrings.length; i++) {
  788. twkb.writeVarInt(this.lineStrings[i].points.length);
  789. for (var j = 0; j < this.lineStrings[i].points.length; j++)
  790. this.lineStrings[i].points[j]._writeTwkbPoint(twkb, precision, previousPoint);
  791. }
  792. }
  793. return twkb.buffer;
  794. };
  795. MultiLineString.prototype._getWkbSize = function () {
  796. var size = 1 + 4 + 4;
  797. for (var i = 0; i < this.lineStrings.length; i++)
  798. size += this.lineStrings[i]._getWkbSize();
  799. return size;
  800. };
  801. MultiLineString.prototype.toGeoJSON = function (options) {
  802. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  803. geoJSON.type = Types.geoJSON.MultiLineString;
  804. geoJSON.coordinates = [];
  805. for (var i = 0; i < this.lineStrings.length; i++)
  806. geoJSON.coordinates.push(this.lineStrings[i].toGeoJSON().coordinates);
  807. return geoJSON;
  808. };
  809. },{"./binarywriter":2,"./geometry":3,"./linestring":5,"./point":9,"./types":11,"util":20}],7:[function(require,module,exports){
  810. module.exports = MultiPoint;
  811. var util = require('util');
  812. var Types = require('./types');
  813. var Geometry = require('./geometry');
  814. var Point = require('./point');
  815. var BinaryWriter = require('./binarywriter');
  816. function MultiPoint(points, srid) {
  817. Geometry.call(this);
  818. this.points = points || [];
  819. this.srid = srid;
  820. if (this.points.length > 0) {
  821. this.hasZ = this.points[0].hasZ;
  822. this.hasM = this.points[0].hasM;
  823. }
  824. }
  825. util.inherits(MultiPoint, Geometry);
  826. MultiPoint.Z = function (points, srid) {
  827. var multiPoint = new MultiPoint(points, srid);
  828. multiPoint.hasZ = true;
  829. return multiPoint;
  830. };
  831. MultiPoint.M = function (points, srid) {
  832. var multiPoint = new MultiPoint(points, srid);
  833. multiPoint.hasM = true;
  834. return multiPoint;
  835. };
  836. MultiPoint.ZM = function (points, srid) {
  837. var multiPoint = new MultiPoint(points, srid);
  838. multiPoint.hasZ = true;
  839. multiPoint.hasM = true;
  840. return multiPoint;
  841. };
  842. MultiPoint._parseWkt = function (value, options) {
  843. var multiPoint = new MultiPoint();
  844. multiPoint.srid = options.srid;
  845. multiPoint.hasZ = options.hasZ;
  846. multiPoint.hasM = options.hasM;
  847. if (value.isMatch(['EMPTY']))
  848. return multiPoint;
  849. value.expectGroupStart();
  850. multiPoint.points.push.apply(multiPoint.points, value.matchCoordinates(options));
  851. value.expectGroupEnd();
  852. return multiPoint;
  853. };
  854. MultiPoint._parseWkb = function (value, options) {
  855. var multiPoint = new MultiPoint();
  856. multiPoint.srid = options.srid;
  857. multiPoint.hasZ = options.hasZ;
  858. multiPoint.hasM = options.hasM;
  859. var pointCount = value.readUInt32();
  860. for (var i = 0; i < pointCount; i++)
  861. multiPoint.points.push(Geometry.parse(value, options));
  862. return multiPoint;
  863. };
  864. MultiPoint._parseTwkb = function (value, options) {
  865. var multiPoint = new MultiPoint();
  866. multiPoint.hasZ = options.hasZ;
  867. multiPoint.hasM = options.hasM;
  868. if (options.isEmpty)
  869. return multiPoint;
  870. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  871. var pointCount = value.readVarInt();
  872. for (var i = 0; i < pointCount; i++)
  873. multiPoint.points.push(Point._readTwkbPoint(value, options, previousPoint));
  874. return multiPoint;
  875. };
  876. MultiPoint._parseGeoJSON = function (value) {
  877. var multiPoint = new MultiPoint();
  878. if (value.coordinates.length > 0)
  879. multiPoint.hasZ = value.coordinates[0].length > 2;
  880. for (var i = 0; i < value.coordinates.length; i++)
  881. multiPoint.points.push(Point._parseGeoJSON({ coordinates: value.coordinates[i] }));
  882. return multiPoint;
  883. };
  884. MultiPoint.prototype.toWkt = function () {
  885. if (this.points.length === 0)
  886. return this._getWktType(Types.wkt.MultiPoint, true);
  887. var wkt = this._getWktType(Types.wkt.MultiPoint, false) + '(';
  888. for (var i = 0; i < this.points.length; i++)
  889. wkt += this._getWktCoordinate(this.points[i]) + ',';
  890. wkt = wkt.slice(0, -1);
  891. wkt += ')';
  892. return wkt;
  893. };
  894. MultiPoint.prototype.toWkb = function () {
  895. var wkb = new BinaryWriter(this._getWkbSize());
  896. wkb.writeInt8(1);
  897. this._writeWkbType(wkb, Types.wkb.MultiPoint);
  898. wkb.writeUInt32LE(this.points.length);
  899. for (var i = 0; i < this.points.length; i++)
  900. wkb.writeBuffer(this.points[i].toWkb({ srid: this.srid }));
  901. return wkb.buffer;
  902. };
  903. MultiPoint.prototype.toTwkb = function () {
  904. var twkb = new BinaryWriter(0, true);
  905. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  906. var isEmpty = this.points.length === 0;
  907. this._writeTwkbHeader(twkb, Types.wkb.MultiPoint, precision, isEmpty);
  908. if (this.points.length > 0) {
  909. twkb.writeVarInt(this.points.length);
  910. var previousPoint = new Point(0, 0, 0, 0);
  911. for (var i = 0; i < this.points.length; i++)
  912. this.points[i]._writeTwkbPoint(twkb, precision, previousPoint);
  913. }
  914. return twkb.buffer;
  915. };
  916. MultiPoint.prototype._getWkbSize = function () {
  917. var coordinateSize = 16;
  918. if (this.hasZ)
  919. coordinateSize += 8;
  920. if (this.hasM)
  921. coordinateSize += 8;
  922. coordinateSize += 5;
  923. return 1 + 4 + 4 + (this.points.length * coordinateSize);
  924. };
  925. MultiPoint.prototype.toGeoJSON = function (options) {
  926. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  927. geoJSON.type = Types.geoJSON.MultiPoint;
  928. geoJSON.coordinates = [];
  929. for (var i = 0; i < this.points.length; i++)
  930. geoJSON.coordinates.push(this.points[i].toGeoJSON().coordinates);
  931. return geoJSON;
  932. };
  933. },{"./binarywriter":2,"./geometry":3,"./point":9,"./types":11,"util":20}],8:[function(require,module,exports){
  934. module.exports = MultiPolygon;
  935. var util = require('util');
  936. var Types = require('./types');
  937. var Geometry = require('./geometry');
  938. var Point = require('./point');
  939. var Polygon = require('./polygon');
  940. var BinaryWriter = require('./binarywriter');
  941. function MultiPolygon(polygons, srid) {
  942. Geometry.call(this);
  943. this.polygons = polygons || [];
  944. this.srid = srid;
  945. if (this.polygons.length > 0) {
  946. this.hasZ = this.polygons[0].hasZ;
  947. this.hasM = this.polygons[0].hasM;
  948. }
  949. }
  950. util.inherits(MultiPolygon, Geometry);
  951. MultiPolygon.Z = function (polygons, srid) {
  952. var multiPolygon = new MultiPolygon(polygons, srid);
  953. multiPolygon.hasZ = true;
  954. return multiPolygon;
  955. };
  956. MultiPolygon.M = function (polygons, srid) {
  957. var multiPolygon = new MultiPolygon(polygons, srid);
  958. multiPolygon.hasM = true;
  959. return multiPolygon;
  960. };
  961. MultiPolygon.ZM = function (polygons, srid) {
  962. var multiPolygon = new MultiPolygon(polygons, srid);
  963. multiPolygon.hasZ = true;
  964. multiPolygon.hasM = true;
  965. return multiPolygon;
  966. };
  967. MultiPolygon._parseWkt = function (value, options) {
  968. var multiPolygon = new MultiPolygon();
  969. multiPolygon.srid = options.srid;
  970. multiPolygon.hasZ = options.hasZ;
  971. multiPolygon.hasM = options.hasM;
  972. if (value.isMatch(['EMPTY']))
  973. return multiPolygon;
  974. value.expectGroupStart();
  975. do {
  976. value.expectGroupStart();
  977. var exteriorRing = [];
  978. var interiorRings = [];
  979. value.expectGroupStart();
  980. exteriorRing.push.apply(exteriorRing, value.matchCoordinates(options));
  981. value.expectGroupEnd();
  982. while (value.isMatch([','])) {
  983. value.expectGroupStart();
  984. interiorRings.push(value.matchCoordinates(options));
  985. value.expectGroupEnd();
  986. }
  987. multiPolygon.polygons.push(new Polygon(exteriorRing, interiorRings));
  988. value.expectGroupEnd();
  989. } while (value.isMatch([',']));
  990. value.expectGroupEnd();
  991. return multiPolygon;
  992. };
  993. MultiPolygon._parseWkb = function (value, options) {
  994. var multiPolygon = new MultiPolygon();
  995. multiPolygon.srid = options.srid;
  996. multiPolygon.hasZ = options.hasZ;
  997. multiPolygon.hasM = options.hasM;
  998. var polygonCount = value.readUInt32();
  999. for (var i = 0; i < polygonCount; i++)
  1000. multiPolygon.polygons.push(Geometry.parse(value, options));
  1001. return multiPolygon;
  1002. };
  1003. MultiPolygon._parseTwkb = function (value, options) {
  1004. var multiPolygon = new MultiPolygon();
  1005. multiPolygon.hasZ = options.hasZ;
  1006. multiPolygon.hasM = options.hasM;
  1007. if (options.isEmpty)
  1008. return multiPolygon;
  1009. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  1010. var polygonCount = value.readVarInt();
  1011. for (var i = 0; i < polygonCount; i++) {
  1012. var polygon = new Polygon();
  1013. polygon.hasZ = options.hasZ;
  1014. polygon.hasM = options.hasM;
  1015. var ringCount = value.readVarInt();
  1016. var exteriorRingCount = value.readVarInt();
  1017. for (var j = 0; j < exteriorRingCount; j++)
  1018. polygon.exteriorRing.push(Point._readTwkbPoint(value, options, previousPoint));
  1019. for (j = 1; j < ringCount; j++) {
  1020. var interiorRing = [];
  1021. var interiorRingCount = value.readVarInt();
  1022. for (var k = 0; k < interiorRingCount; k++)
  1023. interiorRing.push(Point._readTwkbPoint(value, options, previousPoint));
  1024. polygon.interiorRings.push(interiorRing);
  1025. }
  1026. multiPolygon.polygons.push(polygon);
  1027. }
  1028. return multiPolygon;
  1029. };
  1030. MultiPolygon._parseGeoJSON = function (value) {
  1031. var multiPolygon = new MultiPolygon();
  1032. if (value.coordinates.length > 0 && value.coordinates[0].length > 0 && value.coordinates[0][0].length > 0)
  1033. multiPolygon.hasZ = value.coordinates[0][0][0].length > 2;
  1034. for (var i = 0; i < value.coordinates.length; i++)
  1035. multiPolygon.polygons.push(Polygon._parseGeoJSON({ coordinates: value.coordinates[i] }));
  1036. return multiPolygon;
  1037. };
  1038. MultiPolygon.prototype.toWkt = function () {
  1039. if (this.polygons.length === 0)
  1040. return this._getWktType(Types.wkt.MultiPolygon, true);
  1041. var wkt = this._getWktType(Types.wkt.MultiPolygon, false) + '(';
  1042. for (var i = 0; i < this.polygons.length; i++)
  1043. wkt += this.polygons[i]._toInnerWkt() + ',';
  1044. wkt = wkt.slice(0, -1);
  1045. wkt += ')';
  1046. return wkt;
  1047. };
  1048. MultiPolygon.prototype.toWkb = function () {
  1049. var wkb = new BinaryWriter(this._getWkbSize());
  1050. wkb.writeInt8(1);
  1051. this._writeWkbType(wkb, Types.wkb.MultiPolygon);
  1052. wkb.writeUInt32LE(this.polygons.length);
  1053. for (var i = 0; i < this.polygons.length; i++)
  1054. wkb.writeBuffer(this.polygons[i].toWkb({ srid: this.srid }));
  1055. return wkb.buffer;
  1056. };
  1057. MultiPolygon.prototype.toTwkb = function () {
  1058. var twkb = new BinaryWriter(0, true);
  1059. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  1060. var isEmpty = this.polygons.length === 0;
  1061. this._writeTwkbHeader(twkb, Types.wkb.MultiPolygon, precision, isEmpty);
  1062. if (this.polygons.length > 0) {
  1063. twkb.writeVarInt(this.polygons.length);
  1064. var previousPoint = new Point(0, 0, 0, 0);
  1065. for (var i = 0; i < this.polygons.length; i++) {
  1066. twkb.writeVarInt(1 + this.polygons[i].interiorRings.length);
  1067. twkb.writeVarInt(this.polygons[i].exteriorRing.length);
  1068. for (var j = 0; j < this.polygons[i].exteriorRing.length; j++)
  1069. this.polygons[i].exteriorRing[j]._writeTwkbPoint(twkb, precision, previousPoint);
  1070. for (j = 0; j < this.polygons[i].interiorRings.length; j++) {
  1071. twkb.writeVarInt(this.polygons[i].interiorRings[j].length);
  1072. for (var k = 0; k < this.polygons[i].interiorRings[j].length; k++)
  1073. this.polygons[i].interiorRings[j][k]._writeTwkbPoint(twkb, precision, previousPoint);
  1074. }
  1075. }
  1076. }
  1077. return twkb.buffer;
  1078. };
  1079. MultiPolygon.prototype._getWkbSize = function () {
  1080. var size = 1 + 4 + 4;
  1081. for (var i = 0; i < this.polygons.length; i++)
  1082. size += this.polygons[i]._getWkbSize();
  1083. return size;
  1084. };
  1085. MultiPolygon.prototype.toGeoJSON = function (options) {
  1086. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  1087. geoJSON.type = Types.geoJSON.MultiPolygon;
  1088. geoJSON.coordinates = [];
  1089. for (var i = 0; i < this.polygons.length; i++)
  1090. geoJSON.coordinates.push(this.polygons[i].toGeoJSON().coordinates);
  1091. return geoJSON;
  1092. };
  1093. },{"./binarywriter":2,"./geometry":3,"./point":9,"./polygon":10,"./types":11,"util":20}],9:[function(require,module,exports){
  1094. module.exports = Point;
  1095. var util = require('util');
  1096. var Geometry = require('./geometry');
  1097. var Types = require('./types');
  1098. var BinaryWriter = require('./binarywriter');
  1099. var ZigZag = require('./zigzag.js');
  1100. function Point(x, y, z, m, srid) {
  1101. Geometry.call(this);
  1102. this.x = x;
  1103. this.y = y;
  1104. this.z = z;
  1105. this.m = m;
  1106. this.srid = srid;
  1107. this.hasZ = typeof this.z !== 'undefined';
  1108. this.hasM = typeof this.m !== 'undefined';
  1109. }
  1110. util.inherits(Point, Geometry);
  1111. Point.Z = function (x, y, z, srid) {
  1112. var point = new Point(x, y, z, undefined, srid);
  1113. point.hasZ = true;
  1114. return point;
  1115. };
  1116. Point.M = function (x, y, m, srid) {
  1117. var point = new Point(x, y, undefined, m, srid);
  1118. point.hasM = true;
  1119. return point;
  1120. };
  1121. Point.ZM = function (x, y, z, m, srid) {
  1122. var point = new Point(x, y, z, m, srid);
  1123. point.hasZ = true;
  1124. point.hasM = true;
  1125. return point;
  1126. };
  1127. Point._parseWkt = function (value, options) {
  1128. var point = new Point();
  1129. point.srid = options.srid;
  1130. point.hasZ = options.hasZ;
  1131. point.hasM = options.hasM;
  1132. if (value.isMatch(['EMPTY']))
  1133. return point;
  1134. value.expectGroupStart();
  1135. var coordinate = value.matchCoordinate(options);
  1136. point.x = coordinate.x;
  1137. point.y = coordinate.y;
  1138. point.z = coordinate.z;
  1139. point.m = coordinate.m;
  1140. value.expectGroupEnd();
  1141. return point;
  1142. };
  1143. Point._parseWkb = function (value, options) {
  1144. var point = Point._readWkbPoint(value, options);
  1145. point.srid = options.srid;
  1146. return point;
  1147. };
  1148. Point._readWkbPoint = function (value, options) {
  1149. return new Point(value.readDouble(), value.readDouble(),
  1150. options.hasZ ? value.readDouble() : undefined,
  1151. options.hasM ? value.readDouble() : undefined);
  1152. };
  1153. Point._parseTwkb = function (value, options) {
  1154. var point = new Point();
  1155. point.hasZ = options.hasZ;
  1156. point.hasM = options.hasM;
  1157. if (options.isEmpty)
  1158. return point;
  1159. point.x = ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  1160. point.y = ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  1161. point.z = options.hasZ ? ZigZag.decode(value.readVarInt()) / options.zPrecisionFactor : undefined;
  1162. point.m = options.hasM ? ZigZag.decode(value.readVarInt()) / options.mPrecisionFactor : undefined;
  1163. return point;
  1164. };
  1165. Point._readTwkbPoint = function (value, options, previousPoint) {
  1166. previousPoint.x += ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  1167. previousPoint.y += ZigZag.decode(value.readVarInt()) / options.precisionFactor;
  1168. if (options.hasZ)
  1169. previousPoint.z += ZigZag.decode(value.readVarInt()) / options.zPrecisionFactor;
  1170. if (options.hasM)
  1171. previousPoint.m += ZigZag.decode(value.readVarInt()) / options.mPrecisionFactor;
  1172. return new Point(previousPoint.x, previousPoint.y, previousPoint.z, previousPoint.m);
  1173. };
  1174. Point._parseGeoJSON = function (value) {
  1175. return Point._readGeoJSONPoint(value.coordinates);
  1176. };
  1177. Point._readGeoJSONPoint = function (coordinates) {
  1178. if (coordinates.length === 0)
  1179. return new Point();
  1180. if (coordinates.length > 2)
  1181. return new Point(coordinates[0], coordinates[1], coordinates[2]);
  1182. return new Point(coordinates[0], coordinates[1]);
  1183. };
  1184. Point.prototype.toWkt = function () {
  1185. if (typeof this.x === 'undefined' && typeof this.y === 'undefined' &&
  1186. typeof this.z === 'undefined' && typeof this.m === 'undefined')
  1187. return this._getWktType(Types.wkt.Point, true);
  1188. return this._getWktType(Types.wkt.Point, false) + '(' + this._getWktCoordinate(this) + ')';
  1189. };
  1190. Point.prototype.toWkb = function (parentOptions) {
  1191. var wkb = new BinaryWriter(this._getWkbSize());
  1192. wkb.writeInt8(1);
  1193. this._writeWkbType(wkb, Types.wkb.Point, parentOptions);
  1194. if (typeof this.x === 'undefined' && typeof this.y === 'undefined') {
  1195. wkb.writeDoubleLE(NaN);
  1196. wkb.writeDoubleLE(NaN);
  1197. if (this.hasZ)
  1198. wkb.writeDoubleLE(NaN);
  1199. if (this.hasM)
  1200. wkb.writeDoubleLE(NaN);
  1201. }
  1202. else {
  1203. this._writeWkbPoint(wkb);
  1204. }
  1205. return wkb.buffer;
  1206. };
  1207. Point.prototype._writeWkbPoint = function (wkb) {
  1208. wkb.writeDoubleLE(this.x);
  1209. wkb.writeDoubleLE(this.y);
  1210. if (this.hasZ)
  1211. wkb.writeDoubleLE(this.z);
  1212. if (this.hasM)
  1213. wkb.writeDoubleLE(this.m);
  1214. };
  1215. Point.prototype.toTwkb = function () {
  1216. var twkb = new BinaryWriter(0, true);
  1217. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  1218. var isEmpty = typeof this.x === 'undefined' && typeof this.y === 'undefined';
  1219. this._writeTwkbHeader(twkb, Types.wkb.Point, precision, isEmpty);
  1220. if (!isEmpty)
  1221. this._writeTwkbPoint(twkb, precision, new Point(0, 0, 0, 0));
  1222. return twkb.buffer;
  1223. };
  1224. Point.prototype._writeTwkbPoint = function (twkb, precision, previousPoint) {
  1225. var x = this.x * precision.xyFactor;
  1226. var y = this.y * precision.xyFactor;
  1227. var z = this.z * precision.zFactor;
  1228. var m = this.m * precision.mFactor;
  1229. twkb.writeVarInt(ZigZag.encode(x - previousPoint.x));
  1230. twkb.writeVarInt(ZigZag.encode(y - previousPoint.y));
  1231. if (this.hasZ)
  1232. twkb.writeVarInt(ZigZag.encode(z - previousPoint.z));
  1233. if (this.hasM)
  1234. twkb.writeVarInt(ZigZag.encode(m - previousPoint.m));
  1235. previousPoint.x = x;
  1236. previousPoint.y = y;
  1237. previousPoint.z = z;
  1238. previousPoint.m = m;
  1239. };
  1240. Point.prototype._getWkbSize = function () {
  1241. var size = 1 + 4 + 8 + 8;
  1242. if (this.hasZ)
  1243. size += 8;
  1244. if (this.hasM)
  1245. size += 8;
  1246. return size;
  1247. };
  1248. Point.prototype.toGeoJSON = function (options) {
  1249. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  1250. geoJSON.type = Types.geoJSON.Point;
  1251. if (typeof this.x === 'undefined' && typeof this.y === 'undefined')
  1252. geoJSON.coordinates = [];
  1253. else if (typeof this.z !== 'undefined')
  1254. geoJSON.coordinates = [this.x, this.y, this.z];
  1255. else
  1256. geoJSON.coordinates = [this.x, this.y];
  1257. return geoJSON;
  1258. };
  1259. },{"./binarywriter":2,"./geometry":3,"./types":11,"./zigzag.js":13,"util":20}],10:[function(require,module,exports){
  1260. module.exports = Polygon;
  1261. var util = require('util');
  1262. var Geometry = require('./geometry');
  1263. var Types = require('./types');
  1264. var Point = require('./point');
  1265. var BinaryWriter = require('./binarywriter');
  1266. function Polygon(exteriorRing, interiorRings, srid) {
  1267. Geometry.call(this);
  1268. this.exteriorRing = exteriorRing || [];
  1269. this.interiorRings = interiorRings || [];
  1270. this.srid = srid;
  1271. if (this.exteriorRing.length > 0) {
  1272. this.hasZ = this.exteriorRing[0].hasZ;
  1273. this.hasM = this.exteriorRing[0].hasM;
  1274. }
  1275. }
  1276. util.inherits(Polygon, Geometry);
  1277. Polygon.Z = function (exteriorRing, interiorRings, srid) {
  1278. var polygon = new Polygon(exteriorRing, interiorRings, srid);
  1279. polygon.hasZ = true;
  1280. return polygon;
  1281. };
  1282. Polygon.M = function (exteriorRing, interiorRings, srid) {
  1283. var polygon = new Polygon(exteriorRing, interiorRings, srid);
  1284. polygon.hasM = true;
  1285. return polygon;
  1286. };
  1287. Polygon.ZM = function (exteriorRing, interiorRings, srid) {
  1288. var polygon = new Polygon(exteriorRing, interiorRings, srid);
  1289. polygon.hasZ = true;
  1290. polygon.hasM = true;
  1291. return polygon;
  1292. };
  1293. Polygon._parseWkt = function (value, options) {
  1294. var polygon = new Polygon();
  1295. polygon.srid = options.srid;
  1296. polygon.hasZ = options.hasZ;
  1297. polygon.hasM = options.hasM;
  1298. if (value.isMatch(['EMPTY']))
  1299. return polygon;
  1300. value.expectGroupStart();
  1301. value.expectGroupStart();
  1302. polygon.exteriorRing.push.apply(polygon.exteriorRing, value.matchCoordinates(options));
  1303. value.expectGroupEnd();
  1304. while (value.isMatch([','])) {
  1305. value.expectGroupStart();
  1306. polygon.interiorRings.push(value.matchCoordinates(options));
  1307. value.expectGroupEnd();
  1308. }
  1309. value.expectGroupEnd();
  1310. return polygon;
  1311. };
  1312. Polygon._parseWkb = function (value, options) {
  1313. var polygon = new Polygon();
  1314. polygon.srid = options.srid;
  1315. polygon.hasZ = options.hasZ;
  1316. polygon.hasM = options.hasM;
  1317. var ringCount = value.readUInt32();
  1318. if (ringCount > 0) {
  1319. var exteriorRingCount = value.readUInt32();
  1320. for (var i = 0; i < exteriorRingCount; i++)
  1321. polygon.exteriorRing.push(Point._readWkbPoint(value, options));
  1322. for (i = 1; i < ringCount; i++) {
  1323. var interiorRing = [];
  1324. var interiorRingCount = value.readUInt32();
  1325. for (var j = 0; j < interiorRingCount; j++)
  1326. interiorRing.push(Point._readWkbPoint(value, options));
  1327. polygon.interiorRings.push(interiorRing);
  1328. }
  1329. }
  1330. return polygon;
  1331. };
  1332. Polygon._parseTwkb = function (value, options) {
  1333. var polygon = new Polygon();
  1334. polygon.hasZ = options.hasZ;
  1335. polygon.hasM = options.hasM;
  1336. if (options.isEmpty)
  1337. return polygon;
  1338. var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
  1339. var ringCount = value.readVarInt();
  1340. var exteriorRingCount = value.readVarInt();
  1341. for (var i = 0; i < exteriorRingCount; i++)
  1342. polygon.exteriorRing.push(Point._readTwkbPoint(value, options, previousPoint));
  1343. for (i = 1; i < ringCount; i++) {
  1344. var interiorRing = [];
  1345. var interiorRingCount = value.readVarInt();
  1346. for (var j = 0; j < interiorRingCount; j++)
  1347. interiorRing.push(Point._readTwkbPoint(value, options, previousPoint));
  1348. polygon.interiorRings.push(interiorRing);
  1349. }
  1350. return polygon;
  1351. };
  1352. Polygon._parseGeoJSON = function (value) {
  1353. var polygon = new Polygon();
  1354. if (value.coordinates.length > 0 && value.coordinates[0].length > 0)
  1355. polygon.hasZ = value.coordinates[0][0].length > 2;
  1356. for (var i = 0; i < value.coordinates.length; i++) {
  1357. if (i > 0)
  1358. polygon.interiorRings.push([]);
  1359. for (var j = 0; j < value.coordinates[i].length; j++) {
  1360. if (i === 0)
  1361. polygon.exteriorRing.push(Point._readGeoJSONPoint(value.coordinates[i][j]));
  1362. else
  1363. polygon.interiorRings[i - 1].push(Point._readGeoJSONPoint(value.coordinates[i][j]));
  1364. }
  1365. }
  1366. return polygon;
  1367. };
  1368. Polygon.prototype.toWkt = function () {
  1369. if (this.exteriorRing.length === 0)
  1370. return this._getWktType(Types.wkt.Polygon, true);
  1371. return this._getWktType(Types.wkt.Polygon, false) + this._toInnerWkt();
  1372. };
  1373. Polygon.prototype._toInnerWkt = function () {
  1374. var innerWkt = '((';
  1375. for (var i = 0; i < this.exteriorRing.length; i++)
  1376. innerWkt += this._getWktCoordinate(this.exteriorRing[i]) + ',';
  1377. innerWkt = innerWkt.slice(0, -1);
  1378. innerWkt += ')';
  1379. for (i = 0; i < this.interiorRings.length; i++) {
  1380. innerWkt += ',(';
  1381. for (var j = 0; j < this.interiorRings[i].length; j++) {
  1382. innerWkt += this._getWktCoordinate(this.interiorRings[i][j]) + ',';
  1383. }
  1384. innerWkt = innerWkt.slice(0, -1);
  1385. innerWkt += ')';
  1386. }
  1387. innerWkt += ')';
  1388. return innerWkt;
  1389. };
  1390. Polygon.prototype.toWkb = function (parentOptions) {
  1391. var wkb = new BinaryWriter(this._getWkbSize());
  1392. wkb.writeInt8(1);
  1393. this._writeWkbType(wkb, Types.wkb.Polygon, parentOptions);
  1394. if (this.exteriorRing.length > 0) {
  1395. wkb.writeUInt32LE(1 + this.interiorRings.length);
  1396. wkb.writeUInt32LE(this.exteriorRing.length);
  1397. }
  1398. else {
  1399. wkb.writeUInt32LE(0);
  1400. }
  1401. for (var i = 0; i < this.exteriorRing.length; i++)
  1402. this.exteriorRing[i]._writeWkbPoint(wkb);
  1403. for (i = 0; i < this.interiorRings.length; i++) {
  1404. wkb.writeUInt32LE(this.interiorRings[i].length);
  1405. for (var j = 0; j < this.interiorRings[i].length; j++)
  1406. this.interiorRings[i][j]._writeWkbPoint(wkb);
  1407. }
  1408. return wkb.buffer;
  1409. };
  1410. Polygon.prototype.toTwkb = function () {
  1411. var twkb = new BinaryWriter(0, true);
  1412. var precision = Geometry.getTwkbPrecision(5, 0, 0);
  1413. var isEmpty = this.exteriorRing.length === 0;
  1414. this._writeTwkbHeader(twkb, Types.wkb.Polygon, precision, isEmpty);
  1415. if (this.exteriorRing.length > 0) {
  1416. twkb.writeVarInt(1 + this.interiorRings.length);
  1417. twkb.writeVarInt(this.exteriorRing.length);
  1418. var previousPoint = new Point(0, 0, 0, 0);
  1419. for (var i = 0; i < this.exteriorRing.length; i++)
  1420. this.exteriorRing[i]._writeTwkbPoint(twkb, precision, previousPoint);
  1421. for (i = 0; i < this.interiorRings.length; i++) {
  1422. twkb.writeVarInt(this.interiorRings[i].length);
  1423. for (var j = 0; j < this.interiorRings[i].length; j++)
  1424. this.interiorRings[i][j]._writeTwkbPoint(twkb, precision, previousPoint);
  1425. }
  1426. }
  1427. return twkb.buffer;
  1428. };
  1429. Polygon.prototype._getWkbSize = function () {
  1430. var coordinateSize = 16;
  1431. if (this.hasZ)
  1432. coordinateSize += 8;
  1433. if (this.hasM)
  1434. coordinateSize += 8;
  1435. var size = 1 + 4 + 4;
  1436. if (this.exteriorRing.length > 0)
  1437. size += 4 + (this.exteriorRing.length * coordinateSize);
  1438. for (var i = 0; i < this.interiorRings.length; i++)
  1439. size += 4 + (this.interiorRings[i].length * coordinateSize);
  1440. return size;
  1441. };
  1442. Polygon.prototype.toGeoJSON = function (options) {
  1443. var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
  1444. geoJSON.type = Types.geoJSON.Polygon;
  1445. geoJSON.coordinates = [];
  1446. if (this.exteriorRing.length > 0) {
  1447. var exteriorRing = [];
  1448. for (var i = 0; i < this.exteriorRing.length; i++) {
  1449. if (this.hasZ)
  1450. exteriorRing.push([this.exteriorRing[i].x, this.exteriorRing[i].y, this.exteriorRing[i].z]);
  1451. else
  1452. exteriorRing.push([this.exteriorRing[i].x, this.exteriorRing[i].y]);
  1453. }
  1454. geoJSON.coordinates.push(exteriorRing);
  1455. }
  1456. for (var j = 0; j < this.interiorRings.length; j++) {
  1457. var interiorRing = [];
  1458. for (var k = 0; k < this.interiorRings[j].length; k++) {
  1459. if (this.hasZ)
  1460. interiorRing.push([this.interiorRings[j][k].x, this.interiorRings[j][k].y, this.interiorRings[j][k].z]);
  1461. else
  1462. interiorRing.push([this.interiorRings[j][k].x, this.interiorRings[j][k].y]);
  1463. }
  1464. geoJSON.coordinates.push(interiorRing);
  1465. }
  1466. return geoJSON;
  1467. };
  1468. },{"./binarywriter":2,"./geometry":3,"./point":9,"./types":11,"util":20}],11:[function(require,module,exports){
  1469. module.exports = {
  1470. wkt: {
  1471. Point: 'POINT',
  1472. LineString: 'LINESTRING',
  1473. Polygon: 'POLYGON',
  1474. MultiPoint: 'MULTIPOINT',
  1475. MultiLineString: 'MULTILINESTRING',
  1476. MultiPolygon: 'MULTIPOLYGON',
  1477. GeometryCollection: 'GEOMETRYCOLLECTION'
  1478. },
  1479. wkb: {
  1480. Point: 1,
  1481. LineString: 2,
  1482. Polygon: 3,
  1483. MultiPoint: 4,
  1484. MultiLineString: 5,
  1485. MultiPolygon: 6,
  1486. GeometryCollection: 7
  1487. },
  1488. geoJSON: {
  1489. Point: 'Point',
  1490. LineString: 'LineString',
  1491. Polygon: 'Polygon',
  1492. MultiPoint: 'MultiPoint',
  1493. MultiLineString: 'MultiLineString',
  1494. MultiPolygon: 'MultiPolygon',
  1495. GeometryCollection: 'GeometryCollection'
  1496. }
  1497. };
  1498. },{}],12:[function(require,module,exports){
  1499. module.exports = WktParser;
  1500. var Types = require('./types');
  1501. var Point = require('./point');
  1502. function WktParser(value) {
  1503. this.value = value;
  1504. this.position = 0;
  1505. }
  1506. WktParser.prototype.match = function (tokens) {
  1507. this.skipWhitespaces();
  1508. for (var i = 0; i < tokens.length; i++) {
  1509. if (this.value.substring(this.position).indexOf(tokens[i]) === 0) {
  1510. this.position += tokens[i].length;
  1511. return tokens[i];
  1512. }
  1513. }
  1514. return null;
  1515. };
  1516. WktParser.prototype.matchRegex = function (tokens) {
  1517. this.skipWhitespaces();
  1518. for (var i = 0; i < tokens.length; i++) {
  1519. var match = this.value.substring(this.position).match(tokens[i]);
  1520. if (match) {
  1521. this.position += match[0].length;
  1522. return match;
  1523. }
  1524. }
  1525. return null;
  1526. };
  1527. WktParser.prototype.isMatch = function (tokens) {
  1528. this.skipWhitespaces();
  1529. for (var i = 0; i < tokens.length; i++) {
  1530. if (this.value.substring(this.position).indexOf(tokens[i]) === 0) {
  1531. this.position += tokens[i].length;
  1532. return true;
  1533. }
  1534. }
  1535. return false;
  1536. };
  1537. WktParser.prototype.matchType = function () {
  1538. var geometryType = this.match([Types.wkt.Point, Types.wkt.LineString, Types.wkt.Polygon, Types.wkt.MultiPoint,
  1539. Types.wkt.MultiLineString, Types.wkt.MultiPolygon, Types.wkt.GeometryCollection]);
  1540. if (!geometryType)
  1541. throw new Error('Expected geometry type');
  1542. return geometryType;
  1543. };
  1544. WktParser.prototype.matchDimension = function () {
  1545. var dimension = this.match(['ZM', 'Z', 'M']);
  1546. switch (dimension) {
  1547. case 'ZM': return { hasZ: true, hasM: true };
  1548. case 'Z': return { hasZ: true, hasM: false };
  1549. case 'M': return { hasZ: false, hasM: true };
  1550. default: return { hasZ: false, hasM: false };
  1551. }
  1552. };
  1553. WktParser.prototype.expectGroupStart = function () {
  1554. if (!this.isMatch(['(']))
  1555. throw new Error('Expected group start');
  1556. };
  1557. WktParser.prototype.expectGroupEnd = function () {
  1558. if (!this.isMatch([')']))
  1559. throw new Error('Expected group end');
  1560. };
  1561. WktParser.prototype.matchCoordinate = function (options) {
  1562. var match;
  1563. if (options.hasZ && options.hasM)
  1564. match = this.matchRegex([/^(\S*)\s+(\S*)\s+(\S*)\s+([^\s,)]*)/]);
  1565. else if (options.hasZ || options.hasM)
  1566. match = this.matchRegex([/^(\S*)\s+(\S*)\s+([^\s,)]*)/]);
  1567. else
  1568. match = this.matchRegex([/^(\S*)\s+([^\s,)]*)/]);
  1569. if (!match)
  1570. throw new Error('Expected coordinates');
  1571. if (options.hasZ && options.hasM)
  1572. return new Point(parseFloat(match[1]), parseFloat(match[2]), parseFloat(match[3]), parseFloat(match[4]));
  1573. else if (options.hasZ)
  1574. return new Point(parseFloat(match[1]), parseFloat(match[2]), parseFloat(match[3]));
  1575. else if (options.hasM)
  1576. return new Point(parseFloat(match[1]), parseFloat(match[2]), undefined, parseFloat(match[3]));
  1577. else
  1578. return new Point(parseFloat(match[1]), parseFloat(match[2]));
  1579. };
  1580. WktParser.prototype.matchCoordinates = function (options) {
  1581. var coordinates = [];
  1582. do {
  1583. var startsWithBracket = this.isMatch(['(']);
  1584. coordinates.push(this.matchCoordinate(options));
  1585. if (startsWithBracket)
  1586. this.expectGroupEnd();
  1587. } while (this.isMatch([',']));
  1588. return coordinates;
  1589. };
  1590. WktParser.prototype.skipWhitespaces = function () {
  1591. while (this.position < this.value.length && this.value[this.position] === ' ')
  1592. this.position++;
  1593. };
  1594. },{"./point":9,"./types":11}],13:[function(require,module,exports){
  1595. module.exports = {
  1596. encode: function (value) {
  1597. return (value << 1) ^ (value >> 31);
  1598. },
  1599. decode: function (value) {
  1600. return (value >> 1) ^ (-(value & 1));
  1601. }
  1602. };
  1603. },{}],14:[function(require,module,exports){
  1604. 'use strict'
  1605. exports.byteLength = byteLength
  1606. exports.toByteArray = toByteArray
  1607. exports.fromByteArray = fromByteArray
  1608. var lookup = []
  1609. var revLookup = []
  1610. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  1611. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  1612. for (var i = 0, len = code.length; i < len; ++i) {
  1613. lookup[i] = code[i]
  1614. revLookup[code.charCodeAt(i)] = i
  1615. }
  1616. // Support decoding URL-safe base64 strings, as Node.js does.
  1617. // See: https://en.wikipedia.org/wiki/Base64#URL_applications
  1618. revLookup['-'.charCodeAt(0)] = 62
  1619. revLookup['_'.charCodeAt(0)] = 63
  1620. function getLens (b64) {
  1621. var len = b64.length
  1622. if (len % 4 > 0) {
  1623. throw new Error('Invalid string. Length must be a multiple of 4')
  1624. }
  1625. // Trim off extra bytes after placeholder bytes are found
  1626. // See: https://github.com/beatgammit/base64-js/issues/42
  1627. var validLen = b64.indexOf('=')
  1628. if (validLen === -1) validLen = len
  1629. var placeHoldersLen = validLen === len
  1630. ? 0
  1631. : 4 - (validLen % 4)
  1632. return [validLen, placeHoldersLen]
  1633. }
  1634. // base64 is 4/3 + up to two characters of the original data
  1635. function byteLength (b64) {
  1636. var lens = getLens(b64)
  1637. var validLen = lens[0]
  1638. var placeHoldersLen = lens[1]
  1639. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  1640. }
  1641. function _byteLength (b64, validLen, placeHoldersLen) {
  1642. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  1643. }
  1644. function toByteArray (b64) {
  1645. var tmp
  1646. var lens = getLens(b64)
  1647. var validLen = lens[0]
  1648. var placeHoldersLen = lens[1]
  1649. var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
  1650. var curByte = 0
  1651. // if there are placeholders, only get up to the last complete 4 chars
  1652. var len = placeHoldersLen > 0
  1653. ? validLen - 4
  1654. : validLen
  1655. var i
  1656. for (i = 0; i < len; i += 4) {
  1657. tmp =
  1658. (revLookup[b64.charCodeAt(i)] << 18) |
  1659. (revLookup[b64.charCodeAt(i + 1)] << 12) |
  1660. (revLookup[b64.charCodeAt(i + 2)] << 6) |
  1661. revLookup[b64.charCodeAt(i + 3)]
  1662. arr[curByte++] = (tmp >> 16) & 0xFF
  1663. arr[curByte++] = (tmp >> 8) & 0xFF
  1664. arr[curByte++] = tmp & 0xFF
  1665. }
  1666. if (placeHoldersLen === 2) {
  1667. tmp =
  1668. (revLookup[b64.charCodeAt(i)] << 2) |
  1669. (revLookup[b64.charCodeAt(i + 1)] >> 4)
  1670. arr[curByte++] = tmp & 0xFF
  1671. }
  1672. if (placeHoldersLen === 1) {
  1673. tmp =
  1674. (revLookup[b64.charCodeAt(i)] << 10) |
  1675. (revLookup[b64.charCodeAt(i + 1)] << 4) |
  1676. (revLookup[b64.charCodeAt(i + 2)] >> 2)
  1677. arr[curByte++] = (tmp >> 8) & 0xFF
  1678. arr[curByte++] = tmp & 0xFF
  1679. }
  1680. return arr
  1681. }
  1682. function tripletToBase64 (num) {
  1683. return lookup[num >> 18 & 0x3F] +
  1684. lookup[num >> 12 & 0x3F] +
  1685. lookup[num >> 6 & 0x3F] +
  1686. lookup[num & 0x3F]
  1687. }
  1688. function encodeChunk (uint8, start, end) {
  1689. var tmp
  1690. var output = []
  1691. for (var i = start; i < end; i += 3) {
  1692. tmp =
  1693. ((uint8[i] << 16) & 0xFF0000) +
  1694. ((uint8[i + 1] << 8) & 0xFF00) +
  1695. (uint8[i + 2] & 0xFF)
  1696. output.push(tripletToBase64(tmp))
  1697. }
  1698. return output.join('')
  1699. }
  1700. function fromByteArray (uint8) {
  1701. var tmp
  1702. var len = uint8.length
  1703. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  1704. var parts = []
  1705. var maxChunkLength = 16383 // must be multiple of 3
  1706. // go through the array every three bytes, we'll deal with trailing stuff later
  1707. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  1708. parts.push(encodeChunk(
  1709. uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)
  1710. ))
  1711. }
  1712. // pad the end with zeros, but make sure to not forget the extra bytes
  1713. if (extraBytes === 1) {
  1714. tmp = uint8[len - 1]
  1715. parts.push(
  1716. lookup[tmp >> 2] +
  1717. lookup[(tmp << 4) & 0x3F] +
  1718. '=='
  1719. )
  1720. } else if (extraBytes === 2) {
  1721. tmp = (uint8[len - 2] << 8) + uint8[len - 1]
  1722. parts.push(
  1723. lookup[tmp >> 10] +
  1724. lookup[(tmp >> 4) & 0x3F] +
  1725. lookup[(tmp << 2) & 0x3F] +
  1726. '='
  1727. )
  1728. }
  1729. return parts.join('')
  1730. }
  1731. },{}],15:[function(require,module,exports){
  1732. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  1733. var e, m
  1734. var eLen = (nBytes * 8) - mLen - 1
  1735. var eMax = (1 << eLen) - 1
  1736. var eBias = eMax >> 1
  1737. var nBits = -7
  1738. var i = isLE ? (nBytes - 1) : 0
  1739. var d = isLE ? -1 : 1
  1740. var s = buffer[offset + i]
  1741. i += d
  1742. e = s & ((1 << (-nBits)) - 1)
  1743. s >>= (-nBits)
  1744. nBits += eLen
  1745. for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  1746. m = e & ((1 << (-nBits)) - 1)
  1747. e >>= (-nBits)
  1748. nBits += mLen
  1749. for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  1750. if (e === 0) {
  1751. e = 1 - eBias
  1752. } else if (e === eMax) {
  1753. return m ? NaN : ((s ? -1 : 1) * Infinity)
  1754. } else {
  1755. m = m + Math.pow(2, mLen)
  1756. e = e - eBias
  1757. }
  1758. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  1759. }
  1760. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  1761. var e, m, c
  1762. var eLen = (nBytes * 8) - mLen - 1
  1763. var eMax = (1 << eLen) - 1
  1764. var eBias = eMax >> 1
  1765. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  1766. var i = isLE ? 0 : (nBytes - 1)
  1767. var d = isLE ? 1 : -1
  1768. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  1769. value = Math.abs(value)
  1770. if (isNaN(value) || value === Infinity) {
  1771. m = isNaN(value) ? 1 : 0
  1772. e = eMax
  1773. } else {
  1774. e = Math.floor(Math.log(value) / Math.LN2)
  1775. if (value * (c = Math.pow(2, -e)) < 1) {
  1776. e--
  1777. c *= 2
  1778. }
  1779. if (e + eBias >= 1) {
  1780. value += rt / c
  1781. } else {
  1782. value += rt * Math.pow(2, 1 - eBias)
  1783. }
  1784. if (value * c >= 2) {
  1785. e++
  1786. c /= 2
  1787. }
  1788. if (e + eBias >= eMax) {
  1789. m = 0
  1790. e = eMax
  1791. } else if (e + eBias >= 1) {
  1792. m = ((value * c) - 1) * Math.pow(2, mLen)
  1793. e = e + eBias
  1794. } else {
  1795. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  1796. e = 0
  1797. }
  1798. }
  1799. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  1800. e = (e << mLen) | m
  1801. eLen += mLen
  1802. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  1803. buffer[offset + i - d] |= s * 128
  1804. }
  1805. },{}],16:[function(require,module,exports){
  1806. if (typeof Object.create === 'function') {
  1807. // implementation from standard node.js 'util' module
  1808. module.exports = function inherits(ctor, superCtor) {
  1809. ctor.super_ = superCtor
  1810. ctor.prototype = Object.create(superCtor.prototype, {
  1811. constructor: {
  1812. value: ctor,
  1813. enumerable: false,
  1814. writable: true,
  1815. configurable: true
  1816. }
  1817. });
  1818. };
  1819. } else {
  1820. // old school shim for old browsers
  1821. module.exports = function inherits(ctor, superCtor) {
  1822. ctor.super_ = superCtor
  1823. var TempCtor = function () {}
  1824. TempCtor.prototype = superCtor.prototype
  1825. ctor.prototype = new TempCtor()
  1826. ctor.prototype.constructor = ctor
  1827. }
  1828. }
  1829. },{}],17:[function(require,module,exports){
  1830. /*!
  1831. * Determine if an object is a Buffer
  1832. *
  1833. * @author Feross Aboukhadijeh <https://feross.org>
  1834. * @license MIT
  1835. */
  1836. // The _isBuffer check is for Safari 5-7 support, because it's missing
  1837. // Object.prototype.constructor. Remove this eventually
  1838. module.exports = function (obj) {
  1839. return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer)
  1840. }
  1841. function isBuffer (obj) {
  1842. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  1843. }
  1844. // For Node v0.10 support. Remove this eventually.
  1845. function isSlowBuffer (obj) {
  1846. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0))
  1847. }
  1848. },{}],18:[function(require,module,exports){
  1849. // shim for using process in browser
  1850. var process = module.exports = {};
  1851. // cached from whatever global is present so that test runners that stub it
  1852. // don't break things. But we need to wrap it in a try catch in case it is
  1853. // wrapped in strict mode code which doesn't define any globals. It's inside a
  1854. // function because try/catches deoptimize in certain engines.
  1855. var cachedSetTimeout;
  1856. var cachedClearTimeout;
  1857. function defaultSetTimout() {
  1858. throw new Error('setTimeout has not been defined');
  1859. }
  1860. function defaultClearTimeout () {
  1861. throw new Error('clearTimeout has not been defined');
  1862. }
  1863. (function () {
  1864. try {
  1865. if (typeof setTimeout === 'function') {
  1866. cachedSetTimeout = setTimeout;
  1867. } else {
  1868. cachedSetTimeout = defaultSetTimout;
  1869. }
  1870. } catch (e) {
  1871. cachedSetTimeout = defaultSetTimout;
  1872. }
  1873. try {
  1874. if (typeof clearTimeout === 'function') {
  1875. cachedClearTimeout = clearTimeout;
  1876. } else {
  1877. cachedClearTimeout = defaultClearTimeout;
  1878. }
  1879. } catch (e) {
  1880. cachedClearTimeout = defaultClearTimeout;
  1881. }
  1882. } ())
  1883. function runTimeout(fun) {
  1884. if (cachedSetTimeout === setTimeout) {
  1885. //normal enviroments in sane situations
  1886. return setTimeout(fun, 0);
  1887. }
  1888. // if setTimeout wasn't available but was latter defined
  1889. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  1890. cachedSetTimeout = setTimeout;
  1891. return setTimeout(fun, 0);
  1892. }
  1893. try {
  1894. // when when somebody has screwed with setTimeout but no I.E. maddness
  1895. return cachedSetTimeout(fun, 0);
  1896. } catch(e){
  1897. try {
  1898. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1899. return cachedSetTimeout.call(null, fun, 0);
  1900. } catch(e){
  1901. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  1902. return cachedSetTimeout.call(this, fun, 0);
  1903. }
  1904. }
  1905. }
  1906. function runClearTimeout(marker) {
  1907. if (cachedClearTimeout === clearTimeout) {
  1908. //normal enviroments in sane situations
  1909. return clearTimeout(marker);
  1910. }
  1911. // if clearTimeout wasn't available but was latter defined
  1912. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  1913. cachedClearTimeout = clearTimeout;
  1914. return clearTimeout(marker);
  1915. }
  1916. try {
  1917. // when when somebody has screwed with setTimeout but no I.E. maddness
  1918. return cachedClearTimeout(marker);
  1919. } catch (e){
  1920. try {
  1921. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1922. return cachedClearTimeout.call(null, marker);
  1923. } catch (e){
  1924. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  1925. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  1926. return cachedClearTimeout.call(this, marker);
  1927. }
  1928. }
  1929. }
  1930. var queue = [];
  1931. var draining = false;
  1932. var currentQueue;
  1933. var queueIndex = -1;
  1934. function cleanUpNextTick() {
  1935. if (!draining || !currentQueue) {
  1936. return;
  1937. }
  1938. draining = false;
  1939. if (currentQueue.length) {
  1940. queue = currentQueue.concat(queue);
  1941. } else {
  1942. queueIndex = -1;
  1943. }
  1944. if (queue.length) {
  1945. drainQueue();
  1946. }
  1947. }
  1948. function drainQueue() {
  1949. if (draining) {
  1950. return;
  1951. }
  1952. var timeout = runTimeout(cleanUpNextTick);
  1953. draining = true;
  1954. var len = queue.length;
  1955. while(len) {
  1956. currentQueue = queue;
  1957. queue = [];
  1958. while (++queueIndex < len) {
  1959. if (currentQueue) {
  1960. currentQueue[queueIndex].run();
  1961. }
  1962. }
  1963. queueIndex = -1;
  1964. len = queue.length;
  1965. }
  1966. currentQueue = null;
  1967. draining = false;
  1968. runClearTimeout(timeout);
  1969. }
  1970. process.nextTick = function (fun) {
  1971. var args = new Array(arguments.length - 1);
  1972. if (arguments.length > 1) {
  1973. for (var i = 1; i < arguments.length; i++) {
  1974. args[i - 1] = arguments[i];
  1975. }
  1976. }
  1977. queue.push(new Item(fun, args));
  1978. if (queue.length === 1 && !draining) {
  1979. runTimeout(drainQueue);
  1980. }
  1981. };
  1982. // v8 likes predictible objects
  1983. function Item(fun, array) {
  1984. this.fun = fun;
  1985. this.array = array;
  1986. }
  1987. Item.prototype.run = function () {
  1988. this.fun.apply(null, this.array);
  1989. };
  1990. process.title = 'browser';
  1991. process.browser = true;
  1992. process.env = {};
  1993. process.argv = [];
  1994. process.version = ''; // empty string to avoid regexp issues
  1995. process.versions = {};
  1996. function noop() {}
  1997. process.on = noop;
  1998. process.addListener = noop;
  1999. process.once = noop;
  2000. process.off = noop;
  2001. process.removeListener = noop;
  2002. process.removeAllListeners = noop;
  2003. process.emit = noop;
  2004. process.prependListener = noop;
  2005. process.prependOnceListener = noop;
  2006. process.listeners = function (name) { return [] }
  2007. process.binding = function (name) {
  2008. throw new Error('process.binding is not supported');
  2009. };
  2010. process.cwd = function () { return '/' };
  2011. process.chdir = function (dir) {
  2012. throw new Error('process.chdir is not supported');
  2013. };
  2014. process.umask = function() { return 0; };
  2015. },{}],19:[function(require,module,exports){
  2016. module.exports = function isBuffer(arg) {
  2017. return arg && typeof arg === 'object'
  2018. && typeof arg.copy === 'function'
  2019. && typeof arg.fill === 'function'
  2020. && typeof arg.readUInt8 === 'function';
  2021. }
  2022. },{}],20:[function(require,module,exports){
  2023. (function (process,global){
  2024. // Copyright Joyent, Inc. and other Node contributors.
  2025. //
  2026. // Permission is hereby granted, free of charge, to any person obtaining a
  2027. // copy of this software and associated documentation files (the
  2028. // "Software"), to deal in the Software without restriction, including
  2029. // without limitation the rights to use, copy, modify, merge, publish,
  2030. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2031. // persons to whom the Software is furnished to do so, subject to the
  2032. // following conditions:
  2033. //
  2034. // The above copyright notice and this permission notice shall be included
  2035. // in all copies or substantial portions of the Software.
  2036. //
  2037. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2038. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2039. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2040. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2041. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2042. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2043. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2044. var formatRegExp = /%[sdj%]/g;
  2045. exports.format = function(f) {
  2046. if (!isString(f)) {
  2047. var objects = [];
  2048. for (var i = 0; i < arguments.length; i++) {
  2049. objects.push(inspect(arguments[i]));
  2050. }
  2051. return objects.join(' ');
  2052. }
  2053. var i = 1;
  2054. var args = arguments;
  2055. var len = args.length;
  2056. var str = String(f).replace(formatRegExp, function(x) {
  2057. if (x === '%%') return '%';
  2058. if (i >= len) return x;
  2059. switch (x) {
  2060. case '%s': return String(args[i++]);
  2061. case '%d': return Number(args[i++]);
  2062. case '%j':
  2063. try {
  2064. return JSON.stringify(args[i++]);
  2065. } catch (_) {
  2066. return '[Circular]';
  2067. }
  2068. default:
  2069. return x;
  2070. }
  2071. });
  2072. for (var x = args[i]; i < len; x = args[++i]) {
  2073. if (isNull(x) || !isObject(x)) {
  2074. str += ' ' + x;
  2075. } else {
  2076. str += ' ' + inspect(x);
  2077. }
  2078. }
  2079. return str;
  2080. };
  2081. // Mark that a method should not be used.
  2082. // Returns a modified function which warns once by default.
  2083. // If --no-deprecation is set, then it is a no-op.
  2084. exports.deprecate = function(fn, msg) {
  2085. // Allow for deprecating things in the process of starting up.
  2086. if (isUndefined(global.process)) {
  2087. return function() {
  2088. return exports.deprecate(fn, msg).apply(this, arguments);
  2089. };
  2090. }
  2091. if (process.noDeprecation === true) {
  2092. return fn;
  2093. }
  2094. var warned = false;
  2095. function deprecated() {
  2096. if (!warned) {
  2097. if (process.throwDeprecation) {
  2098. throw new Error(msg);
  2099. } else if (process.traceDeprecation) {
  2100. console.trace(msg);
  2101. } else {
  2102. console.error(msg);
  2103. }
  2104. warned = true;
  2105. }
  2106. return fn.apply(this, arguments);
  2107. }
  2108. return deprecated;
  2109. };
  2110. var debugs = {};
  2111. var debugEnviron;
  2112. exports.debuglog = function(set) {
  2113. if (isUndefined(debugEnviron))
  2114. debugEnviron = process.env.NODE_DEBUG || '';
  2115. set = set.toUpperCase();
  2116. if (!debugs[set]) {
  2117. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  2118. var pid = process.pid;
  2119. debugs[set] = function() {
  2120. var msg = exports.format.apply(exports, arguments);
  2121. console.error('%s %d: %s', set, pid, msg);
  2122. };
  2123. } else {
  2124. debugs[set] = function() {};
  2125. }
  2126. }
  2127. return debugs[set];
  2128. };
  2129. /**
  2130. * Echos the value of a value. Trys to print the value out
  2131. * in the best way possible given the different types.
  2132. *
  2133. * @param {Object} obj The object to print out.
  2134. * @param {Object} opts Optional options object that alters the output.
  2135. */
  2136. /* legacy: obj, showHidden, depth, colors*/
  2137. function inspect(obj, opts) {
  2138. // default options
  2139. var ctx = {
  2140. seen: [],
  2141. stylize: stylizeNoColor
  2142. };
  2143. // legacy...
  2144. if (arguments.length >= 3) ctx.depth = arguments[2];
  2145. if (arguments.length >= 4) ctx.colors = arguments[3];
  2146. if (isBoolean(opts)) {
  2147. // legacy...
  2148. ctx.showHidden = opts;
  2149. } else if (opts) {
  2150. // got an "options" object
  2151. exports._extend(ctx, opts);
  2152. }
  2153. // set default options
  2154. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  2155. if (isUndefined(ctx.depth)) ctx.depth = 2;
  2156. if (isUndefined(ctx.colors)) ctx.colors = false;
  2157. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  2158. if (ctx.colors) ctx.stylize = stylizeWithColor;
  2159. return formatValue(ctx, obj, ctx.depth);
  2160. }
  2161. exports.inspect = inspect;
  2162. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  2163. inspect.colors = {
  2164. 'bold' : [1, 22],
  2165. 'italic' : [3, 23],
  2166. 'underline' : [4, 24],
  2167. 'inverse' : [7, 27],
  2168. 'white' : [37, 39],
  2169. 'grey' : [90, 39],
  2170. 'black' : [30, 39],
  2171. 'blue' : [34, 39],
  2172. 'cyan' : [36, 39],
  2173. 'green' : [32, 39],
  2174. 'magenta' : [35, 39],
  2175. 'red' : [31, 39],
  2176. 'yellow' : [33, 39]
  2177. };
  2178. // Don't use 'blue' not visible on cmd.exe
  2179. inspect.styles = {
  2180. 'special': 'cyan',
  2181. 'number': 'yellow',
  2182. 'boolean': 'yellow',
  2183. 'undefined': 'grey',
  2184. 'null': 'bold',
  2185. 'string': 'green',
  2186. 'date': 'magenta',
  2187. // "name": intentionally not styling
  2188. 'regexp': 'red'
  2189. };
  2190. function stylizeWithColor(str, styleType) {
  2191. var style = inspect.styles[styleType];
  2192. if (style) {
  2193. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  2194. '\u001b[' + inspect.colors[style][1] + 'm';
  2195. } else {
  2196. return str;
  2197. }
  2198. }
  2199. function stylizeNoColor(str, styleType) {
  2200. return str;
  2201. }
  2202. function arrayToHash(array) {
  2203. var hash = {};
  2204. array.forEach(function(val, idx) {
  2205. hash[val] = true;
  2206. });
  2207. return hash;
  2208. }
  2209. function formatValue(ctx, value, recurseTimes) {
  2210. // Provide a hook for user-specified inspect functions.
  2211. // Check that value is an object with an inspect function on it
  2212. if (ctx.customInspect &&
  2213. value &&
  2214. isFunction(value.inspect) &&
  2215. // Filter out the util module, it's inspect function is special
  2216. value.inspect !== exports.inspect &&
  2217. // Also filter out any prototype objects using the circular check.
  2218. !(value.constructor && value.constructor.prototype === value)) {
  2219. var ret = value.inspect(recurseTimes, ctx);
  2220. if (!isString(ret)) {
  2221. ret = formatValue(ctx, ret, recurseTimes);
  2222. }
  2223. return ret;
  2224. }
  2225. // Primitive types cannot have properties
  2226. var primitive = formatPrimitive(ctx, value);
  2227. if (primitive) {
  2228. return primitive;
  2229. }
  2230. // Look up the keys of the object.
  2231. var keys = Object.keys(value);
  2232. var visibleKeys = arrayToHash(keys);
  2233. if (ctx.showHidden) {
  2234. keys = Object.getOwnPropertyNames(value);
  2235. }
  2236. // IE doesn't make error fields non-enumerable
  2237. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  2238. if (isError(value)
  2239. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  2240. return formatError(value);
  2241. }
  2242. // Some type of object without properties can be shortcutted.
  2243. if (keys.length === 0) {
  2244. if (isFunction(value)) {
  2245. var name = value.name ? ': ' + value.name : '';
  2246. return ctx.stylize('[Function' + name + ']', 'special');
  2247. }
  2248. if (isRegExp(value)) {
  2249. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2250. }
  2251. if (isDate(value)) {
  2252. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  2253. }
  2254. if (isError(value)) {
  2255. return formatError(value);
  2256. }
  2257. }
  2258. var base = '', array = false, braces = ['{', '}'];
  2259. // Make Array say that they are Array
  2260. if (isArray(value)) {
  2261. array = true;
  2262. braces = ['[', ']'];
  2263. }
  2264. // Make functions say that they are functions
  2265. if (isFunction(value)) {
  2266. var n = value.name ? ': ' + value.name : '';
  2267. base = ' [Function' + n + ']';
  2268. }
  2269. // Make RegExps say that they are RegExps
  2270. if (isRegExp(value)) {
  2271. base = ' ' + RegExp.prototype.toString.call(value);
  2272. }
  2273. // Make dates with properties first say the date
  2274. if (isDate(value)) {
  2275. base = ' ' + Date.prototype.toUTCString.call(value);
  2276. }
  2277. // Make error with message first say the error
  2278. if (isError(value)) {
  2279. base = ' ' + formatError(value);
  2280. }
  2281. if (keys.length === 0 && (!array || value.length == 0)) {
  2282. return braces[0] + base + braces[1];
  2283. }
  2284. if (recurseTimes < 0) {
  2285. if (isRegExp(value)) {
  2286. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2287. } else {
  2288. return ctx.stylize('[Object]', 'special');
  2289. }
  2290. }
  2291. ctx.seen.push(value);
  2292. var output;
  2293. if (array) {
  2294. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  2295. } else {
  2296. output = keys.map(function(key) {
  2297. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  2298. });
  2299. }
  2300. ctx.seen.pop();
  2301. return reduceToSingleString(output, base, braces);
  2302. }
  2303. function formatPrimitive(ctx, value) {
  2304. if (isUndefined(value))
  2305. return ctx.stylize('undefined', 'undefined');
  2306. if (isString(value)) {
  2307. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  2308. .replace(/'/g, "\\'")
  2309. .replace(/\\"/g, '"') + '\'';
  2310. return ctx.stylize(simple, 'string');
  2311. }
  2312. if (isNumber(value))
  2313. return ctx.stylize('' + value, 'number');
  2314. if (isBoolean(value))
  2315. return ctx.stylize('' + value, 'boolean');
  2316. // For some reason typeof null is "object", so special case here.
  2317. if (isNull(value))
  2318. return ctx.stylize('null', 'null');
  2319. }
  2320. function formatError(value) {
  2321. return '[' + Error.prototype.toString.call(value) + ']';
  2322. }
  2323. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  2324. var output = [];
  2325. for (var i = 0, l = value.length; i < l; ++i) {
  2326. if (hasOwnProperty(value, String(i))) {
  2327. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2328. String(i), true));
  2329. } else {
  2330. output.push('');
  2331. }
  2332. }
  2333. keys.forEach(function(key) {
  2334. if (!key.match(/^\d+$/)) {
  2335. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2336. key, true));
  2337. }
  2338. });
  2339. return output;
  2340. }
  2341. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  2342. var name, str, desc;
  2343. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  2344. if (desc.get) {
  2345. if (desc.set) {
  2346. str = ctx.stylize('[Getter/Setter]', 'special');
  2347. } else {
  2348. str = ctx.stylize('[Getter]', 'special');
  2349. }
  2350. } else {
  2351. if (desc.set) {
  2352. str = ctx.stylize('[Setter]', 'special');
  2353. }
  2354. }
  2355. if (!hasOwnProperty(visibleKeys, key)) {
  2356. name = '[' + key + ']';
  2357. }
  2358. if (!str) {
  2359. if (ctx.seen.indexOf(desc.value) < 0) {
  2360. if (isNull(recurseTimes)) {
  2361. str = formatValue(ctx, desc.value, null);
  2362. } else {
  2363. str = formatValue(ctx, desc.value, recurseTimes - 1);
  2364. }
  2365. if (str.indexOf('\n') > -1) {
  2366. if (array) {
  2367. str = str.split('\n').map(function(line) {
  2368. return ' ' + line;
  2369. }).join('\n').substr(2);
  2370. } else {
  2371. str = '\n' + str.split('\n').map(function(line) {
  2372. return ' ' + line;
  2373. }).join('\n');
  2374. }
  2375. }
  2376. } else {
  2377. str = ctx.stylize('[Circular]', 'special');
  2378. }
  2379. }
  2380. if (isUndefined(name)) {
  2381. if (array && key.match(/^\d+$/)) {
  2382. return str;
  2383. }
  2384. name = JSON.stringify('' + key);
  2385. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  2386. name = name.substr(1, name.length - 2);
  2387. name = ctx.stylize(name, 'name');
  2388. } else {
  2389. name = name.replace(/'/g, "\\'")
  2390. .replace(/\\"/g, '"')
  2391. .replace(/(^"|"$)/g, "'");
  2392. name = ctx.stylize(name, 'string');
  2393. }
  2394. }
  2395. return name + ': ' + str;
  2396. }
  2397. function reduceToSingleString(output, base, braces) {
  2398. var numLinesEst = 0;
  2399. var length = output.reduce(function(prev, cur) {
  2400. numLinesEst++;
  2401. if (cur.indexOf('\n') >= 0) numLinesEst++;
  2402. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  2403. }, 0);
  2404. if (length > 60) {
  2405. return braces[0] +
  2406. (base === '' ? '' : base + '\n ') +
  2407. ' ' +
  2408. output.join(',\n ') +
  2409. ' ' +
  2410. braces[1];
  2411. }
  2412. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  2413. }
  2414. // NOTE: These type checking functions intentionally don't use `instanceof`
  2415. // because it is fragile and can be easily faked with `Object.create()`.
  2416. function isArray(ar) {
  2417. return Array.isArray(ar);
  2418. }
  2419. exports.isArray = isArray;
  2420. function isBoolean(arg) {
  2421. return typeof arg === 'boolean';
  2422. }
  2423. exports.isBoolean = isBoolean;
  2424. function isNull(arg) {
  2425. return arg === null;
  2426. }
  2427. exports.isNull = isNull;
  2428. function isNullOrUndefined(arg) {
  2429. return arg == null;
  2430. }
  2431. exports.isNullOrUndefined = isNullOrUndefined;
  2432. function isNumber(arg) {
  2433. return typeof arg === 'number';
  2434. }
  2435. exports.isNumber = isNumber;
  2436. function isString(arg) {
  2437. return typeof arg === 'string';
  2438. }
  2439. exports.isString = isString;
  2440. function isSymbol(arg) {
  2441. return typeof arg === 'symbol';
  2442. }
  2443. exports.isSymbol = isSymbol;
  2444. function isUndefined(arg) {
  2445. return arg === void 0;
  2446. }
  2447. exports.isUndefined = isUndefined;
  2448. function isRegExp(re) {
  2449. return isObject(re) && objectToString(re) === '[object RegExp]';
  2450. }
  2451. exports.isRegExp = isRegExp;
  2452. function isObject(arg) {
  2453. return typeof arg === 'object' && arg !== null;
  2454. }
  2455. exports.isObject = isObject;
  2456. function isDate(d) {
  2457. return isObject(d) && objectToString(d) === '[object Date]';
  2458. }
  2459. exports.isDate = isDate;
  2460. function isError(e) {
  2461. return isObject(e) &&
  2462. (objectToString(e) === '[object Error]' || e instanceof Error);
  2463. }
  2464. exports.isError = isError;
  2465. function isFunction(arg) {
  2466. return typeof arg === 'function';
  2467. }
  2468. exports.isFunction = isFunction;
  2469. function isPrimitive(arg) {
  2470. return arg === null ||
  2471. typeof arg === 'boolean' ||
  2472. typeof arg === 'number' ||
  2473. typeof arg === 'string' ||
  2474. typeof arg === 'symbol' || // ES6 symbol
  2475. typeof arg === 'undefined';
  2476. }
  2477. exports.isPrimitive = isPrimitive;
  2478. exports.isBuffer = require('./support/isBuffer');
  2479. function objectToString(o) {
  2480. return Object.prototype.toString.call(o);
  2481. }
  2482. function pad(n) {
  2483. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  2484. }
  2485. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  2486. 'Oct', 'Nov', 'Dec'];
  2487. // 26 Feb 16:19:34
  2488. function timestamp() {
  2489. var d = new Date();
  2490. var time = [pad(d.getHours()),
  2491. pad(d.getMinutes()),
  2492. pad(d.getSeconds())].join(':');
  2493. return [d.getDate(), months[d.getMonth()], time].join(' ');
  2494. }
  2495. // log is just a thin wrapper to console.log that prepends a timestamp
  2496. exports.log = function() {
  2497. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  2498. };
  2499. /**
  2500. * Inherit the prototype methods from one constructor into another.
  2501. *
  2502. * The Function.prototype.inherits from lang.js rewritten as a standalone
  2503. * function (not on Function.prototype). NOTE: If this file is to be loaded
  2504. * during bootstrapping this function needs to be rewritten using some native
  2505. * functions as prototype setup using normal JavaScript does not work as
  2506. * expected during bootstrapping (see mirror.js in r114903).
  2507. *
  2508. * @param {function} ctor Constructor function which needs to inherit the
  2509. * prototype.
  2510. * @param {function} superCtor Constructor function to inherit prototype from.
  2511. */
  2512. exports.inherits = require('inherits');
  2513. exports._extend = function(origin, add) {
  2514. // Don't do anything if add isn't an object
  2515. if (!add || !isObject(add)) return origin;
  2516. var keys = Object.keys(add);
  2517. var i = keys.length;
  2518. while (i--) {
  2519. origin[keys[i]] = add[keys[i]];
  2520. }
  2521. return origin;
  2522. };
  2523. function hasOwnProperty(obj, prop) {
  2524. return Object.prototype.hasOwnProperty.call(obj, prop);
  2525. }
  2526. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2527. },{"./support/isBuffer":19,"_process":18,"inherits":16}],"buffer":[function(require,module,exports){
  2528. (function (Buffer){
  2529. /*!
  2530. * The buffer module from node.js, for the browser.
  2531. *
  2532. * @author Feross Aboukhadijeh <https://feross.org>
  2533. * @license MIT
  2534. */
  2535. /* eslint-disable no-proto */
  2536. 'use strict'
  2537. var base64 = require('base64-js')
  2538. var ieee754 = require('ieee754')
  2539. var customInspectSymbol =
  2540. (typeof Symbol === 'function' && typeof Symbol.for === 'function')
  2541. ? Symbol.for('nodejs.util.inspect.custom')
  2542. : null
  2543. exports.Buffer = Buffer
  2544. exports.SlowBuffer = SlowBuffer
  2545. exports.INSPECT_MAX_BYTES = 50
  2546. var K_MAX_LENGTH = 0x7fffffff
  2547. exports.kMaxLength = K_MAX_LENGTH
  2548. /**
  2549. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  2550. * === true Use Uint8Array implementation (fastest)
  2551. * === false Print warning and recommend using `buffer` v4.x which has an Object
  2552. * implementation (most compatible, even IE6)
  2553. *
  2554. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  2555. * Opera 11.6+, iOS 4.2+.
  2556. *
  2557. * We report that the browser does not support typed arrays if the are not subclassable
  2558. * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
  2559. * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
  2560. * for __proto__ and has a buggy typed array implementation.
  2561. */
  2562. Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
  2563. if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
  2564. typeof console.error === 'function') {
  2565. console.error(
  2566. 'This browser lacks typed array (Uint8Array) support which is required by ' +
  2567. '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
  2568. )
  2569. }
  2570. function typedArraySupport () {
  2571. // Can typed array instances can be augmented?
  2572. try {
  2573. var arr = new Uint8Array(1)
  2574. var proto = { foo: function () { return 42 } }
  2575. Object.setPrototypeOf(proto, Uint8Array.prototype)
  2576. Object.setPrototypeOf(arr, proto)
  2577. return arr.foo() === 42
  2578. } catch (e) {
  2579. return false
  2580. }
  2581. }
  2582. Object.defineProperty(Buffer.prototype, 'parent', {
  2583. enumerable: true,
  2584. get: function () {
  2585. if (!Buffer.isBuffer(this)) return undefined
  2586. return this.buffer
  2587. }
  2588. })
  2589. Object.defineProperty(Buffer.prototype, 'offset', {
  2590. enumerable: true,
  2591. get: function () {
  2592. if (!Buffer.isBuffer(this)) return undefined
  2593. return this.byteOffset
  2594. }
  2595. })
  2596. function createBuffer (length) {
  2597. if (length > K_MAX_LENGTH) {
  2598. throw new RangeError('The value "' + length + '" is invalid for option "size"')
  2599. }
  2600. // Return an augmented `Uint8Array` instance
  2601. var buf = new Uint8Array(length)
  2602. Object.setPrototypeOf(buf, Buffer.prototype)
  2603. return buf
  2604. }
  2605. /**
  2606. * The Buffer constructor returns instances of `Uint8Array` that have their
  2607. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  2608. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  2609. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  2610. * returns a single octet.
  2611. *
  2612. * The `Uint8Array` prototype remains unmodified.
  2613. */
  2614. function Buffer (arg, encodingOrOffset, length) {
  2615. // Common case.
  2616. if (typeof arg === 'number') {
  2617. if (typeof encodingOrOffset === 'string') {
  2618. throw new TypeError(
  2619. 'The "string" argument must be of type string. Received type number'
  2620. )
  2621. }
  2622. return allocUnsafe(arg)
  2623. }
  2624. return from(arg, encodingOrOffset, length)
  2625. }
  2626. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  2627. if (typeof Symbol !== 'undefined' && Symbol.species != null &&
  2628. Buffer[Symbol.species] === Buffer) {
  2629. Object.defineProperty(Buffer, Symbol.species, {
  2630. value: null,
  2631. configurable: true,
  2632. enumerable: false,
  2633. writable: false
  2634. })
  2635. }
  2636. Buffer.poolSize = 8192 // not used by this implementation
  2637. function from (value, encodingOrOffset, length) {
  2638. if (typeof value === 'string') {
  2639. return fromString(value, encodingOrOffset)
  2640. }
  2641. if (ArrayBuffer.isView(value)) {
  2642. return fromArrayLike(value)
  2643. }
  2644. if (value == null) {
  2645. throw new TypeError(
  2646. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  2647. 'or Array-like Object. Received type ' + (typeof value)
  2648. )
  2649. }
  2650. if (isInstance(value, ArrayBuffer) ||
  2651. (value && isInstance(value.buffer, ArrayBuffer))) {
  2652. return fromArrayBuffer(value, encodingOrOffset, length)
  2653. }
  2654. if (typeof SharedArrayBuffer !== 'undefined' &&
  2655. (isInstance(value, SharedArrayBuffer) ||
  2656. (value && isInstance(value.buffer, SharedArrayBuffer)))) {
  2657. return fromArrayBuffer(value, encodingOrOffset, length)
  2658. }
  2659. if (typeof value === 'number') {
  2660. throw new TypeError(
  2661. 'The "value" argument must not be of type number. Received type number'
  2662. )
  2663. }
  2664. var valueOf = value.valueOf && value.valueOf()
  2665. if (valueOf != null && valueOf !== value) {
  2666. return Buffer.from(valueOf, encodingOrOffset, length)
  2667. }
  2668. var b = fromObject(value)
  2669. if (b) return b
  2670. if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
  2671. typeof value[Symbol.toPrimitive] === 'function') {
  2672. return Buffer.from(
  2673. value[Symbol.toPrimitive]('string'), encodingOrOffset, length
  2674. )
  2675. }
  2676. throw new TypeError(
  2677. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  2678. 'or Array-like Object. Received type ' + (typeof value)
  2679. )
  2680. }
  2681. /**
  2682. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  2683. * if value is a number.
  2684. * Buffer.from(str[, encoding])
  2685. * Buffer.from(array)
  2686. * Buffer.from(buffer)
  2687. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  2688. **/
  2689. Buffer.from = function (value, encodingOrOffset, length) {
  2690. return from(value, encodingOrOffset, length)
  2691. }
  2692. // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
  2693. // https://github.com/feross/buffer/pull/148
  2694. Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype)
  2695. Object.setPrototypeOf(Buffer, Uint8Array)
  2696. function assertSize (size) {
  2697. if (typeof size !== 'number') {
  2698. throw new TypeError('"size" argument must be of type number')
  2699. } else if (size < 0) {
  2700. throw new RangeError('The value "' + size + '" is invalid for option "size"')
  2701. }
  2702. }
  2703. function alloc (size, fill, encoding) {
  2704. assertSize(size)
  2705. if (size <= 0) {
  2706. return createBuffer(size)
  2707. }
  2708. if (fill !== undefined) {
  2709. // Only pay attention to encoding if it's a string. This
  2710. // prevents accidentally sending in a number that would
  2711. // be interpretted as a start offset.
  2712. return typeof encoding === 'string'
  2713. ? createBuffer(size).fill(fill, encoding)
  2714. : createBuffer(size).fill(fill)
  2715. }
  2716. return createBuffer(size)
  2717. }
  2718. /**
  2719. * Creates a new filled Buffer instance.
  2720. * alloc(size[, fill[, encoding]])
  2721. **/
  2722. Buffer.alloc = function (size, fill, encoding) {
  2723. return alloc(size, fill, encoding)
  2724. }
  2725. function allocUnsafe (size) {
  2726. assertSize(size)
  2727. return createBuffer(size < 0 ? 0 : checked(size) | 0)
  2728. }
  2729. /**
  2730. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  2731. * */
  2732. Buffer.allocUnsafe = function (size) {
  2733. return allocUnsafe(size)
  2734. }
  2735. /**
  2736. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  2737. */
  2738. Buffer.allocUnsafeSlow = function (size) {
  2739. return allocUnsafe(size)
  2740. }
  2741. function fromString (string, encoding) {
  2742. if (typeof encoding !== 'string' || encoding === '') {
  2743. encoding = 'utf8'
  2744. }
  2745. if (!Buffer.isEncoding(encoding)) {
  2746. throw new TypeError('Unknown encoding: ' + encoding)
  2747. }
  2748. var length = byteLength(string, encoding) | 0
  2749. var buf = createBuffer(length)
  2750. var actual = buf.write(string, encoding)
  2751. if (actual !== length) {
  2752. // Writing a hex string, for example, that contains invalid characters will
  2753. // cause everything after the first invalid character to be ignored. (e.g.
  2754. // 'abxxcd' will be treated as 'ab')
  2755. buf = buf.slice(0, actual)
  2756. }
  2757. return buf
  2758. }
  2759. function fromArrayLike (array) {
  2760. var length = array.length < 0 ? 0 : checked(array.length) | 0
  2761. var buf = createBuffer(length)
  2762. for (var i = 0; i < length; i += 1) {
  2763. buf[i] = array[i] & 255
  2764. }
  2765. return buf
  2766. }
  2767. function fromArrayBuffer (array, byteOffset, length) {
  2768. if (byteOffset < 0 || array.byteLength < byteOffset) {
  2769. throw new RangeError('"offset" is outside of buffer bounds')
  2770. }
  2771. if (array.byteLength < byteOffset + (length || 0)) {
  2772. throw new RangeError('"length" is outside of buffer bounds')
  2773. }
  2774. var buf
  2775. if (byteOffset === undefined && length === undefined) {
  2776. buf = new Uint8Array(array)
  2777. } else if (length === undefined) {
  2778. buf = new Uint8Array(array, byteOffset)
  2779. } else {
  2780. buf = new Uint8Array(array, byteOffset, length)
  2781. }
  2782. // Return an augmented `Uint8Array` instance
  2783. Object.setPrototypeOf(buf, Buffer.prototype)
  2784. return buf
  2785. }
  2786. function fromObject (obj) {
  2787. if (Buffer.isBuffer(obj)) {
  2788. var len = checked(obj.length) | 0
  2789. var buf = createBuffer(len)
  2790. if (buf.length === 0) {
  2791. return buf
  2792. }
  2793. obj.copy(buf, 0, 0, len)
  2794. return buf
  2795. }
  2796. if (obj.length !== undefined) {
  2797. if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
  2798. return createBuffer(0)
  2799. }
  2800. return fromArrayLike(obj)
  2801. }
  2802. if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
  2803. return fromArrayLike(obj.data)
  2804. }
  2805. }
  2806. function checked (length) {
  2807. // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
  2808. // length is NaN (which is otherwise coerced to zero.)
  2809. if (length >= K_MAX_LENGTH) {
  2810. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  2811. 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
  2812. }
  2813. return length | 0
  2814. }
  2815. function SlowBuffer (length) {
  2816. if (+length != length) { // eslint-disable-line eqeqeq
  2817. length = 0
  2818. }
  2819. return Buffer.alloc(+length)
  2820. }
  2821. Buffer.isBuffer = function isBuffer (b) {
  2822. return b != null && b._isBuffer === true &&
  2823. b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
  2824. }
  2825. Buffer.compare = function compare (a, b) {
  2826. if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
  2827. if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
  2828. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  2829. throw new TypeError(
  2830. 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
  2831. )
  2832. }
  2833. if (a === b) return 0
  2834. var x = a.length
  2835. var y = b.length
  2836. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  2837. if (a[i] !== b[i]) {
  2838. x = a[i]
  2839. y = b[i]
  2840. break
  2841. }
  2842. }
  2843. if (x < y) return -1
  2844. if (y < x) return 1
  2845. return 0
  2846. }
  2847. Buffer.isEncoding = function isEncoding (encoding) {
  2848. switch (String(encoding).toLowerCase()) {
  2849. case 'hex':
  2850. case 'utf8':
  2851. case 'utf-8':
  2852. case 'ascii':
  2853. case 'latin1':
  2854. case 'binary':
  2855. case 'base64':
  2856. case 'ucs2':
  2857. case 'ucs-2':
  2858. case 'utf16le':
  2859. case 'utf-16le':
  2860. return true
  2861. default:
  2862. return false
  2863. }
  2864. }
  2865. Buffer.concat = function concat (list, length) {
  2866. if (!Array.isArray(list)) {
  2867. throw new TypeError('"list" argument must be an Array of Buffers')
  2868. }
  2869. if (list.length === 0) {
  2870. return Buffer.alloc(0)
  2871. }
  2872. var i
  2873. if (length === undefined) {
  2874. length = 0
  2875. for (i = 0; i < list.length; ++i) {
  2876. length += list[i].length
  2877. }
  2878. }
  2879. var buffer = Buffer.allocUnsafe(length)
  2880. var pos = 0
  2881. for (i = 0; i < list.length; ++i) {
  2882. var buf = list[i]
  2883. if (isInstance(buf, Uint8Array)) {
  2884. buf = Buffer.from(buf)
  2885. }
  2886. if (!Buffer.isBuffer(buf)) {
  2887. throw new TypeError('"list" argument must be an Array of Buffers')
  2888. }
  2889. buf.copy(buffer, pos)
  2890. pos += buf.length
  2891. }
  2892. return buffer
  2893. }
  2894. function byteLength (string, encoding) {
  2895. if (Buffer.isBuffer(string)) {
  2896. return string.length
  2897. }
  2898. if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
  2899. return string.byteLength
  2900. }
  2901. if (typeof string !== 'string') {
  2902. throw new TypeError(
  2903. 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
  2904. 'Received type ' + typeof string
  2905. )
  2906. }
  2907. var len = string.length
  2908. var mustMatch = (arguments.length > 2 && arguments[2] === true)
  2909. if (!mustMatch && len === 0) return 0
  2910. // Use a for loop to avoid recursion
  2911. var loweredCase = false
  2912. for (;;) {
  2913. switch (encoding) {
  2914. case 'ascii':
  2915. case 'latin1':
  2916. case 'binary':
  2917. return len
  2918. case 'utf8':
  2919. case 'utf-8':
  2920. return utf8ToBytes(string).length
  2921. case 'ucs2':
  2922. case 'ucs-2':
  2923. case 'utf16le':
  2924. case 'utf-16le':
  2925. return len * 2
  2926. case 'hex':
  2927. return len >>> 1
  2928. case 'base64':
  2929. return base64ToBytes(string).length
  2930. default:
  2931. if (loweredCase) {
  2932. return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
  2933. }
  2934. encoding = ('' + encoding).toLowerCase()
  2935. loweredCase = true
  2936. }
  2937. }
  2938. }
  2939. Buffer.byteLength = byteLength
  2940. function slowToString (encoding, start, end) {
  2941. var loweredCase = false
  2942. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  2943. // property of a typed array.
  2944. // This behaves neither like String nor Uint8Array in that we set start/end
  2945. // to their upper/lower bounds if the value passed is out of range.
  2946. // undefined is handled specially as per ECMA-262 6th Edition,
  2947. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  2948. if (start === undefined || start < 0) {
  2949. start = 0
  2950. }
  2951. // Return early if start > this.length. Done here to prevent potential uint32
  2952. // coercion fail below.
  2953. if (start > this.length) {
  2954. return ''
  2955. }
  2956. if (end === undefined || end > this.length) {
  2957. end = this.length
  2958. }
  2959. if (end <= 0) {
  2960. return ''
  2961. }
  2962. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  2963. end >>>= 0
  2964. start >>>= 0
  2965. if (end <= start) {
  2966. return ''
  2967. }
  2968. if (!encoding) encoding = 'utf8'
  2969. while (true) {
  2970. switch (encoding) {
  2971. case 'hex':
  2972. return hexSlice(this, start, end)
  2973. case 'utf8':
  2974. case 'utf-8':
  2975. return utf8Slice(this, start, end)
  2976. case 'ascii':
  2977. return asciiSlice(this, start, end)
  2978. case 'latin1':
  2979. case 'binary':
  2980. return latin1Slice(this, start, end)
  2981. case 'base64':
  2982. return base64Slice(this, start, end)
  2983. case 'ucs2':
  2984. case 'ucs-2':
  2985. case 'utf16le':
  2986. case 'utf-16le':
  2987. return utf16leSlice(this, start, end)
  2988. default:
  2989. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  2990. encoding = (encoding + '').toLowerCase()
  2991. loweredCase = true
  2992. }
  2993. }
  2994. }
  2995. // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
  2996. // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
  2997. // reliably in a browserify context because there could be multiple different
  2998. // copies of the 'buffer' package in use. This method works even for Buffer
  2999. // instances that were created from another copy of the `buffer` package.
  3000. // See: https://github.com/feross/buffer/issues/154
  3001. Buffer.prototype._isBuffer = true
  3002. function swap (b, n, m) {
  3003. var i = b[n]
  3004. b[n] = b[m]
  3005. b[m] = i
  3006. }
  3007. Buffer.prototype.swap16 = function swap16 () {
  3008. var len = this.length
  3009. if (len % 2 !== 0) {
  3010. throw new RangeError('Buffer size must be a multiple of 16-bits')
  3011. }
  3012. for (var i = 0; i < len; i += 2) {
  3013. swap(this, i, i + 1)
  3014. }
  3015. return this
  3016. }
  3017. Buffer.prototype.swap32 = function swap32 () {
  3018. var len = this.length
  3019. if (len % 4 !== 0) {
  3020. throw new RangeError('Buffer size must be a multiple of 32-bits')
  3021. }
  3022. for (var i = 0; i < len; i += 4) {
  3023. swap(this, i, i + 3)
  3024. swap(this, i + 1, i + 2)
  3025. }
  3026. return this
  3027. }
  3028. Buffer.prototype.swap64 = function swap64 () {
  3029. var len = this.length
  3030. if (len % 8 !== 0) {
  3031. throw new RangeError('Buffer size must be a multiple of 64-bits')
  3032. }
  3033. for (var i = 0; i < len; i += 8) {
  3034. swap(this, i, i + 7)
  3035. swap(this, i + 1, i + 6)
  3036. swap(this, i + 2, i + 5)
  3037. swap(this, i + 3, i + 4)
  3038. }
  3039. return this
  3040. }
  3041. Buffer.prototype.toString = function toString () {
  3042. var length = this.length
  3043. if (length === 0) return ''
  3044. if (arguments.length === 0) return utf8Slice(this, 0, length)
  3045. return slowToString.apply(this, arguments)
  3046. }
  3047. Buffer.prototype.toLocaleString = Buffer.prototype.toString
  3048. Buffer.prototype.equals = function equals (b) {
  3049. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  3050. if (this === b) return true
  3051. return Buffer.compare(this, b) === 0
  3052. }
  3053. Buffer.prototype.inspect = function inspect () {
  3054. var str = ''
  3055. var max = exports.INSPECT_MAX_BYTES
  3056. str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
  3057. if (this.length > max) str += ' ... '
  3058. return '<Buffer ' + str + '>'
  3059. }
  3060. if (customInspectSymbol) {
  3061. Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect
  3062. }
  3063. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  3064. if (isInstance(target, Uint8Array)) {
  3065. target = Buffer.from(target, target.offset, target.byteLength)
  3066. }
  3067. if (!Buffer.isBuffer(target)) {
  3068. throw new TypeError(
  3069. 'The "target" argument must be one of type Buffer or Uint8Array. ' +
  3070. 'Received type ' + (typeof target)
  3071. )
  3072. }
  3073. if (start === undefined) {
  3074. start = 0
  3075. }
  3076. if (end === undefined) {
  3077. end = target ? target.length : 0
  3078. }
  3079. if (thisStart === undefined) {
  3080. thisStart = 0
  3081. }
  3082. if (thisEnd === undefined) {
  3083. thisEnd = this.length
  3084. }
  3085. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  3086. throw new RangeError('out of range index')
  3087. }
  3088. if (thisStart >= thisEnd && start >= end) {
  3089. return 0
  3090. }
  3091. if (thisStart >= thisEnd) {
  3092. return -1
  3093. }
  3094. if (start >= end) {
  3095. return 1
  3096. }
  3097. start >>>= 0
  3098. end >>>= 0
  3099. thisStart >>>= 0
  3100. thisEnd >>>= 0
  3101. if (this === target) return 0
  3102. var x = thisEnd - thisStart
  3103. var y = end - start
  3104. var len = Math.min(x, y)
  3105. var thisCopy = this.slice(thisStart, thisEnd)
  3106. var targetCopy = target.slice(start, end)
  3107. for (var i = 0; i < len; ++i) {
  3108. if (thisCopy[i] !== targetCopy[i]) {
  3109. x = thisCopy[i]
  3110. y = targetCopy[i]
  3111. break
  3112. }
  3113. }
  3114. if (x < y) return -1
  3115. if (y < x) return 1
  3116. return 0
  3117. }
  3118. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  3119. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  3120. //
  3121. // Arguments:
  3122. // - buffer - a Buffer to search
  3123. // - val - a string, Buffer, or number
  3124. // - byteOffset - an index into `buffer`; will be clamped to an int32
  3125. // - encoding - an optional encoding, relevant is val is a string
  3126. // - dir - true for indexOf, false for lastIndexOf
  3127. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  3128. // Empty buffer means no match
  3129. if (buffer.length === 0) return -1
  3130. // Normalize byteOffset
  3131. if (typeof byteOffset === 'string') {
  3132. encoding = byteOffset
  3133. byteOffset = 0
  3134. } else if (byteOffset > 0x7fffffff) {
  3135. byteOffset = 0x7fffffff
  3136. } else if (byteOffset < -0x80000000) {
  3137. byteOffset = -0x80000000
  3138. }
  3139. byteOffset = +byteOffset // Coerce to Number.
  3140. if (numberIsNaN(byteOffset)) {
  3141. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  3142. byteOffset = dir ? 0 : (buffer.length - 1)
  3143. }
  3144. // Normalize byteOffset: negative offsets start from the end of the buffer
  3145. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  3146. if (byteOffset >= buffer.length) {
  3147. if (dir) return -1
  3148. else byteOffset = buffer.length - 1
  3149. } else if (byteOffset < 0) {
  3150. if (dir) byteOffset = 0
  3151. else return -1
  3152. }
  3153. // Normalize val
  3154. if (typeof val === 'string') {
  3155. val = Buffer.from(val, encoding)
  3156. }
  3157. // Finally, search either indexOf (if dir is true) or lastIndexOf
  3158. if (Buffer.isBuffer(val)) {
  3159. // Special case: looking for empty string/buffer always fails
  3160. if (val.length === 0) {
  3161. return -1
  3162. }
  3163. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  3164. } else if (typeof val === 'number') {
  3165. val = val & 0xFF // Search for a byte value [0-255]
  3166. if (typeof Uint8Array.prototype.indexOf === 'function') {
  3167. if (dir) {
  3168. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  3169. } else {
  3170. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  3171. }
  3172. }
  3173. return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
  3174. }
  3175. throw new TypeError('val must be string, number or Buffer')
  3176. }
  3177. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  3178. var indexSize = 1
  3179. var arrLength = arr.length
  3180. var valLength = val.length
  3181. if (encoding !== undefined) {
  3182. encoding = String(encoding).toLowerCase()
  3183. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  3184. encoding === 'utf16le' || encoding === 'utf-16le') {
  3185. if (arr.length < 2 || val.length < 2) {
  3186. return -1
  3187. }
  3188. indexSize = 2
  3189. arrLength /= 2
  3190. valLength /= 2
  3191. byteOffset /= 2
  3192. }
  3193. }
  3194. function read (buf, i) {
  3195. if (indexSize === 1) {
  3196. return buf[i]
  3197. } else {
  3198. return buf.readUInt16BE(i * indexSize)
  3199. }
  3200. }
  3201. var i
  3202. if (dir) {
  3203. var foundIndex = -1
  3204. for (i = byteOffset; i < arrLength; i++) {
  3205. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  3206. if (foundIndex === -1) foundIndex = i
  3207. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  3208. } else {
  3209. if (foundIndex !== -1) i -= i - foundIndex
  3210. foundIndex = -1
  3211. }
  3212. }
  3213. } else {
  3214. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  3215. for (i = byteOffset; i >= 0; i--) {
  3216. var found = true
  3217. for (var j = 0; j < valLength; j++) {
  3218. if (read(arr, i + j) !== read(val, j)) {
  3219. found = false
  3220. break
  3221. }
  3222. }
  3223. if (found) return i
  3224. }
  3225. }
  3226. return -1
  3227. }
  3228. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  3229. return this.indexOf(val, byteOffset, encoding) !== -1
  3230. }
  3231. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  3232. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  3233. }
  3234. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  3235. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  3236. }
  3237. function hexWrite (buf, string, offset, length) {
  3238. offset = Number(offset) || 0
  3239. var remaining = buf.length - offset
  3240. if (!length) {
  3241. length = remaining
  3242. } else {
  3243. length = Number(length)
  3244. if (length > remaining) {
  3245. length = remaining
  3246. }
  3247. }
  3248. var strLen = string.length
  3249. if (length > strLen / 2) {
  3250. length = strLen / 2
  3251. }
  3252. for (var i = 0; i < length; ++i) {
  3253. var parsed = parseInt(string.substr(i * 2, 2), 16)
  3254. if (numberIsNaN(parsed)) return i
  3255. buf[offset + i] = parsed
  3256. }
  3257. return i
  3258. }
  3259. function utf8Write (buf, string, offset, length) {
  3260. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  3261. }
  3262. function asciiWrite (buf, string, offset, length) {
  3263. return blitBuffer(asciiToBytes(string), buf, offset, length)
  3264. }
  3265. function latin1Write (buf, string, offset, length) {
  3266. return asciiWrite(buf, string, offset, length)
  3267. }
  3268. function base64Write (buf, string, offset, length) {
  3269. return blitBuffer(base64ToBytes(string), buf, offset, length)
  3270. }
  3271. function ucs2Write (buf, string, offset, length) {
  3272. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  3273. }
  3274. Buffer.prototype.write = function write (string, offset, length, encoding) {
  3275. // Buffer#write(string)
  3276. if (offset === undefined) {
  3277. encoding = 'utf8'
  3278. length = this.length
  3279. offset = 0
  3280. // Buffer#write(string, encoding)
  3281. } else if (length === undefined && typeof offset === 'string') {
  3282. encoding = offset
  3283. length = this.length
  3284. offset = 0
  3285. // Buffer#write(string, offset[, length][, encoding])
  3286. } else if (isFinite(offset)) {
  3287. offset = offset >>> 0
  3288. if (isFinite(length)) {
  3289. length = length >>> 0
  3290. if (encoding === undefined) encoding = 'utf8'
  3291. } else {
  3292. encoding = length
  3293. length = undefined
  3294. }
  3295. } else {
  3296. throw new Error(
  3297. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  3298. )
  3299. }
  3300. var remaining = this.length - offset
  3301. if (length === undefined || length > remaining) length = remaining
  3302. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  3303. throw new RangeError('Attempt to write outside buffer bounds')
  3304. }
  3305. if (!encoding) encoding = 'utf8'
  3306. var loweredCase = false
  3307. for (;;) {
  3308. switch (encoding) {
  3309. case 'hex':
  3310. return hexWrite(this, string, offset, length)
  3311. case 'utf8':
  3312. case 'utf-8':
  3313. return utf8Write(this, string, offset, length)
  3314. case 'ascii':
  3315. return asciiWrite(this, string, offset, length)
  3316. case 'latin1':
  3317. case 'binary':
  3318. return latin1Write(this, string, offset, length)
  3319. case 'base64':
  3320. // Warning: maxLength not taken into account in base64Write
  3321. return base64Write(this, string, offset, length)
  3322. case 'ucs2':
  3323. case 'ucs-2':
  3324. case 'utf16le':
  3325. case 'utf-16le':
  3326. return ucs2Write(this, string, offset, length)
  3327. default:
  3328. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  3329. encoding = ('' + encoding).toLowerCase()
  3330. loweredCase = true
  3331. }
  3332. }
  3333. }
  3334. Buffer.prototype.toJSON = function toJSON () {
  3335. return {
  3336. type: 'Buffer',
  3337. data: Array.prototype.slice.call(this._arr || this, 0)
  3338. }
  3339. }
  3340. function base64Slice (buf, start, end) {
  3341. if (start === 0 && end === buf.length) {
  3342. return base64.fromByteArray(buf)
  3343. } else {
  3344. return base64.fromByteArray(buf.slice(start, end))
  3345. }
  3346. }
  3347. function utf8Slice (buf, start, end) {
  3348. end = Math.min(buf.length, end)
  3349. var res = []
  3350. var i = start
  3351. while (i < end) {
  3352. var firstByte = buf[i]
  3353. var codePoint = null
  3354. var bytesPerSequence = (firstByte > 0xEF) ? 4
  3355. : (firstByte > 0xDF) ? 3
  3356. : (firstByte > 0xBF) ? 2
  3357. : 1
  3358. if (i + bytesPerSequence <= end) {
  3359. var secondByte, thirdByte, fourthByte, tempCodePoint
  3360. switch (bytesPerSequence) {
  3361. case 1:
  3362. if (firstByte < 0x80) {
  3363. codePoint = firstByte
  3364. }
  3365. break
  3366. case 2:
  3367. secondByte = buf[i + 1]
  3368. if ((secondByte & 0xC0) === 0x80) {
  3369. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  3370. if (tempCodePoint > 0x7F) {
  3371. codePoint = tempCodePoint
  3372. }
  3373. }
  3374. break
  3375. case 3:
  3376. secondByte = buf[i + 1]
  3377. thirdByte = buf[i + 2]
  3378. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  3379. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  3380. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  3381. codePoint = tempCodePoint
  3382. }
  3383. }
  3384. break
  3385. case 4:
  3386. secondByte = buf[i + 1]
  3387. thirdByte = buf[i + 2]
  3388. fourthByte = buf[i + 3]
  3389. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  3390. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  3391. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  3392. codePoint = tempCodePoint
  3393. }
  3394. }
  3395. }
  3396. }
  3397. if (codePoint === null) {
  3398. // we did not generate a valid codePoint so insert a
  3399. // replacement char (U+FFFD) and advance only 1 byte
  3400. codePoint = 0xFFFD
  3401. bytesPerSequence = 1
  3402. } else if (codePoint > 0xFFFF) {
  3403. // encode to utf16 (surrogate pair dance)
  3404. codePoint -= 0x10000
  3405. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  3406. codePoint = 0xDC00 | codePoint & 0x3FF
  3407. }
  3408. res.push(codePoint)
  3409. i += bytesPerSequence
  3410. }
  3411. return decodeCodePointsArray(res)
  3412. }
  3413. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  3414. // the lowest limit is Chrome, with 0x10000 args.
  3415. // We go 1 magnitude less, for safety
  3416. var MAX_ARGUMENTS_LENGTH = 0x1000
  3417. function decodeCodePointsArray (codePoints) {
  3418. var len = codePoints.length
  3419. if (len <= MAX_ARGUMENTS_LENGTH) {
  3420. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  3421. }
  3422. // Decode in chunks to avoid "call stack size exceeded".
  3423. var res = ''
  3424. var i = 0
  3425. while (i < len) {
  3426. res += String.fromCharCode.apply(
  3427. String,
  3428. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  3429. )
  3430. }
  3431. return res
  3432. }
  3433. function asciiSlice (buf, start, end) {
  3434. var ret = ''
  3435. end = Math.min(buf.length, end)
  3436. for (var i = start; i < end; ++i) {
  3437. ret += String.fromCharCode(buf[i] & 0x7F)
  3438. }
  3439. return ret
  3440. }
  3441. function latin1Slice (buf, start, end) {
  3442. var ret = ''
  3443. end = Math.min(buf.length, end)
  3444. for (var i = start; i < end; ++i) {
  3445. ret += String.fromCharCode(buf[i])
  3446. }
  3447. return ret
  3448. }
  3449. function hexSlice (buf, start, end) {
  3450. var len = buf.length
  3451. if (!start || start < 0) start = 0
  3452. if (!end || end < 0 || end > len) end = len
  3453. var out = ''
  3454. for (var i = start; i < end; ++i) {
  3455. out += hexSliceLookupTable[buf[i]]
  3456. }
  3457. return out
  3458. }
  3459. function utf16leSlice (buf, start, end) {
  3460. var bytes = buf.slice(start, end)
  3461. var res = ''
  3462. for (var i = 0; i < bytes.length; i += 2) {
  3463. res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
  3464. }
  3465. return res
  3466. }
  3467. Buffer.prototype.slice = function slice (start, end) {
  3468. var len = this.length
  3469. start = ~~start
  3470. end = end === undefined ? len : ~~end
  3471. if (start < 0) {
  3472. start += len
  3473. if (start < 0) start = 0
  3474. } else if (start > len) {
  3475. start = len
  3476. }
  3477. if (end < 0) {
  3478. end += len
  3479. if (end < 0) end = 0
  3480. } else if (end > len) {
  3481. end = len
  3482. }
  3483. if (end < start) end = start
  3484. var newBuf = this.subarray(start, end)
  3485. // Return an augmented `Uint8Array` instance
  3486. Object.setPrototypeOf(newBuf, Buffer.prototype)
  3487. return newBuf
  3488. }
  3489. /*
  3490. * Need to make sure that buffer isn't trying to write out of bounds.
  3491. */
  3492. function checkOffset (offset, ext, length) {
  3493. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  3494. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  3495. }
  3496. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  3497. offset = offset >>> 0
  3498. byteLength = byteLength >>> 0
  3499. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3500. var val = this[offset]
  3501. var mul = 1
  3502. var i = 0
  3503. while (++i < byteLength && (mul *= 0x100)) {
  3504. val += this[offset + i] * mul
  3505. }
  3506. return val
  3507. }
  3508. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  3509. offset = offset >>> 0
  3510. byteLength = byteLength >>> 0
  3511. if (!noAssert) {
  3512. checkOffset(offset, byteLength, this.length)
  3513. }
  3514. var val = this[offset + --byteLength]
  3515. var mul = 1
  3516. while (byteLength > 0 && (mul *= 0x100)) {
  3517. val += this[offset + --byteLength] * mul
  3518. }
  3519. return val
  3520. }
  3521. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  3522. offset = offset >>> 0
  3523. if (!noAssert) checkOffset(offset, 1, this.length)
  3524. return this[offset]
  3525. }
  3526. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  3527. offset = offset >>> 0
  3528. if (!noAssert) checkOffset(offset, 2, this.length)
  3529. return this[offset] | (this[offset + 1] << 8)
  3530. }
  3531. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  3532. offset = offset >>> 0
  3533. if (!noAssert) checkOffset(offset, 2, this.length)
  3534. return (this[offset] << 8) | this[offset + 1]
  3535. }
  3536. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  3537. offset = offset >>> 0
  3538. if (!noAssert) checkOffset(offset, 4, this.length)
  3539. return ((this[offset]) |
  3540. (this[offset + 1] << 8) |
  3541. (this[offset + 2] << 16)) +
  3542. (this[offset + 3] * 0x1000000)
  3543. }
  3544. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  3545. offset = offset >>> 0
  3546. if (!noAssert) checkOffset(offset, 4, this.length)
  3547. return (this[offset] * 0x1000000) +
  3548. ((this[offset + 1] << 16) |
  3549. (this[offset + 2] << 8) |
  3550. this[offset + 3])
  3551. }
  3552. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  3553. offset = offset >>> 0
  3554. byteLength = byteLength >>> 0
  3555. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3556. var val = this[offset]
  3557. var mul = 1
  3558. var i = 0
  3559. while (++i < byteLength && (mul *= 0x100)) {
  3560. val += this[offset + i] * mul
  3561. }
  3562. mul *= 0x80
  3563. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  3564. return val
  3565. }
  3566. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  3567. offset = offset >>> 0
  3568. byteLength = byteLength >>> 0
  3569. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3570. var i = byteLength
  3571. var mul = 1
  3572. var val = this[offset + --i]
  3573. while (i > 0 && (mul *= 0x100)) {
  3574. val += this[offset + --i] * mul
  3575. }
  3576. mul *= 0x80
  3577. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  3578. return val
  3579. }
  3580. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  3581. offset = offset >>> 0
  3582. if (!noAssert) checkOffset(offset, 1, this.length)
  3583. if (!(this[offset] & 0x80)) return (this[offset])
  3584. return ((0xff - this[offset] + 1) * -1)
  3585. }
  3586. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  3587. offset = offset >>> 0
  3588. if (!noAssert) checkOffset(offset, 2, this.length)
  3589. var val = this[offset] | (this[offset + 1] << 8)
  3590. return (val & 0x8000) ? val | 0xFFFF0000 : val
  3591. }
  3592. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  3593. offset = offset >>> 0
  3594. if (!noAssert) checkOffset(offset, 2, this.length)
  3595. var val = this[offset + 1] | (this[offset] << 8)
  3596. return (val & 0x8000) ? val | 0xFFFF0000 : val
  3597. }
  3598. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  3599. offset = offset >>> 0
  3600. if (!noAssert) checkOffset(offset, 4, this.length)
  3601. return (this[offset]) |
  3602. (this[offset + 1] << 8) |
  3603. (this[offset + 2] << 16) |
  3604. (this[offset + 3] << 24)
  3605. }
  3606. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  3607. offset = offset >>> 0
  3608. if (!noAssert) checkOffset(offset, 4, this.length)
  3609. return (this[offset] << 24) |
  3610. (this[offset + 1] << 16) |
  3611. (this[offset + 2] << 8) |
  3612. (this[offset + 3])
  3613. }
  3614. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  3615. offset = offset >>> 0
  3616. if (!noAssert) checkOffset(offset, 4, this.length)
  3617. return ieee754.read(this, offset, true, 23, 4)
  3618. }
  3619. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  3620. offset = offset >>> 0
  3621. if (!noAssert) checkOffset(offset, 4, this.length)
  3622. return ieee754.read(this, offset, false, 23, 4)
  3623. }
  3624. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  3625. offset = offset >>> 0
  3626. if (!noAssert) checkOffset(offset, 8, this.length)
  3627. return ieee754.read(this, offset, true, 52, 8)
  3628. }
  3629. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  3630. offset = offset >>> 0
  3631. if (!noAssert) checkOffset(offset, 8, this.length)
  3632. return ieee754.read(this, offset, false, 52, 8)
  3633. }
  3634. function checkInt (buf, value, offset, ext, max, min) {
  3635. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  3636. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  3637. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  3638. }
  3639. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  3640. value = +value
  3641. offset = offset >>> 0
  3642. byteLength = byteLength >>> 0
  3643. if (!noAssert) {
  3644. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  3645. checkInt(this, value, offset, byteLength, maxBytes, 0)
  3646. }
  3647. var mul = 1
  3648. var i = 0
  3649. this[offset] = value & 0xFF
  3650. while (++i < byteLength && (mul *= 0x100)) {
  3651. this[offset + i] = (value / mul) & 0xFF
  3652. }
  3653. return offset + byteLength
  3654. }
  3655. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  3656. value = +value
  3657. offset = offset >>> 0
  3658. byteLength = byteLength >>> 0
  3659. if (!noAssert) {
  3660. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  3661. checkInt(this, value, offset, byteLength, maxBytes, 0)
  3662. }
  3663. var i = byteLength - 1
  3664. var mul = 1
  3665. this[offset + i] = value & 0xFF
  3666. while (--i >= 0 && (mul *= 0x100)) {
  3667. this[offset + i] = (value / mul) & 0xFF
  3668. }
  3669. return offset + byteLength
  3670. }
  3671. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  3672. value = +value
  3673. offset = offset >>> 0
  3674. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  3675. this[offset] = (value & 0xff)
  3676. return offset + 1
  3677. }
  3678. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  3679. value = +value
  3680. offset = offset >>> 0
  3681. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  3682. this[offset] = (value & 0xff)
  3683. this[offset + 1] = (value >>> 8)
  3684. return offset + 2
  3685. }
  3686. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  3687. value = +value
  3688. offset = offset >>> 0
  3689. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  3690. this[offset] = (value >>> 8)
  3691. this[offset + 1] = (value & 0xff)
  3692. return offset + 2
  3693. }
  3694. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  3695. value = +value
  3696. offset = offset >>> 0
  3697. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  3698. this[offset + 3] = (value >>> 24)
  3699. this[offset + 2] = (value >>> 16)
  3700. this[offset + 1] = (value >>> 8)
  3701. this[offset] = (value & 0xff)
  3702. return offset + 4
  3703. }
  3704. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  3705. value = +value
  3706. offset = offset >>> 0
  3707. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  3708. this[offset] = (value >>> 24)
  3709. this[offset + 1] = (value >>> 16)
  3710. this[offset + 2] = (value >>> 8)
  3711. this[offset + 3] = (value & 0xff)
  3712. return offset + 4
  3713. }
  3714. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  3715. value = +value
  3716. offset = offset >>> 0
  3717. if (!noAssert) {
  3718. var limit = Math.pow(2, (8 * byteLength) - 1)
  3719. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  3720. }
  3721. var i = 0
  3722. var mul = 1
  3723. var sub = 0
  3724. this[offset] = value & 0xFF
  3725. while (++i < byteLength && (mul *= 0x100)) {
  3726. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  3727. sub = 1
  3728. }
  3729. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  3730. }
  3731. return offset + byteLength
  3732. }
  3733. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  3734. value = +value
  3735. offset = offset >>> 0
  3736. if (!noAssert) {
  3737. var limit = Math.pow(2, (8 * byteLength) - 1)
  3738. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  3739. }
  3740. var i = byteLength - 1
  3741. var mul = 1
  3742. var sub = 0
  3743. this[offset + i] = value & 0xFF
  3744. while (--i >= 0 && (mul *= 0x100)) {
  3745. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  3746. sub = 1
  3747. }
  3748. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  3749. }
  3750. return offset + byteLength
  3751. }
  3752. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  3753. value = +value
  3754. offset = offset >>> 0
  3755. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  3756. if (value < 0) value = 0xff + value + 1
  3757. this[offset] = (value & 0xff)
  3758. return offset + 1
  3759. }
  3760. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  3761. value = +value
  3762. offset = offset >>> 0
  3763. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3764. this[offset] = (value & 0xff)
  3765. this[offset + 1] = (value >>> 8)
  3766. return offset + 2
  3767. }
  3768. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  3769. value = +value
  3770. offset = offset >>> 0
  3771. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3772. this[offset] = (value >>> 8)
  3773. this[offset + 1] = (value & 0xff)
  3774. return offset + 2
  3775. }
  3776. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  3777. value = +value
  3778. offset = offset >>> 0
  3779. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3780. this[offset] = (value & 0xff)
  3781. this[offset + 1] = (value >>> 8)
  3782. this[offset + 2] = (value >>> 16)
  3783. this[offset + 3] = (value >>> 24)
  3784. return offset + 4
  3785. }
  3786. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  3787. value = +value
  3788. offset = offset >>> 0
  3789. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3790. if (value < 0) value = 0xffffffff + value + 1
  3791. this[offset] = (value >>> 24)
  3792. this[offset + 1] = (value >>> 16)
  3793. this[offset + 2] = (value >>> 8)
  3794. this[offset + 3] = (value & 0xff)
  3795. return offset + 4
  3796. }
  3797. function checkIEEE754 (buf, value, offset, ext, max, min) {
  3798. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  3799. if (offset < 0) throw new RangeError('Index out of range')
  3800. }
  3801. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  3802. value = +value
  3803. offset = offset >>> 0
  3804. if (!noAssert) {
  3805. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  3806. }
  3807. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  3808. return offset + 4
  3809. }
  3810. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  3811. return writeFloat(this, value, offset, true, noAssert)
  3812. }
  3813. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  3814. return writeFloat(this, value, offset, false, noAssert)
  3815. }
  3816. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  3817. value = +value
  3818. offset = offset >>> 0
  3819. if (!noAssert) {
  3820. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  3821. }
  3822. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  3823. return offset + 8
  3824. }
  3825. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  3826. return writeDouble(this, value, offset, true, noAssert)
  3827. }
  3828. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  3829. return writeDouble(this, value, offset, false, noAssert)
  3830. }
  3831. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  3832. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  3833. if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
  3834. if (!start) start = 0
  3835. if (!end && end !== 0) end = this.length
  3836. if (targetStart >= target.length) targetStart = target.length
  3837. if (!targetStart) targetStart = 0
  3838. if (end > 0 && end < start) end = start
  3839. // Copy 0 bytes; we're done
  3840. if (end === start) return 0
  3841. if (target.length === 0 || this.length === 0) return 0
  3842. // Fatal error conditions
  3843. if (targetStart < 0) {
  3844. throw new RangeError('targetStart out of bounds')
  3845. }
  3846. if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
  3847. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  3848. // Are we oob?
  3849. if (end > this.length) end = this.length
  3850. if (target.length - targetStart < end - start) {
  3851. end = target.length - targetStart + start
  3852. }
  3853. var len = end - start
  3854. if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
  3855. // Use built-in when available, missing from IE11
  3856. this.copyWithin(targetStart, start, end)
  3857. } else if (this === target && start < targetStart && targetStart < end) {
  3858. // descending copy from end
  3859. for (var i = len - 1; i >= 0; --i) {
  3860. target[i + targetStart] = this[i + start]
  3861. }
  3862. } else {
  3863. Uint8Array.prototype.set.call(
  3864. target,
  3865. this.subarray(start, end),
  3866. targetStart
  3867. )
  3868. }
  3869. return len
  3870. }
  3871. // Usage:
  3872. // buffer.fill(number[, offset[, end]])
  3873. // buffer.fill(buffer[, offset[, end]])
  3874. // buffer.fill(string[, offset[, end]][, encoding])
  3875. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  3876. // Handle string cases:
  3877. if (typeof val === 'string') {
  3878. if (typeof start === 'string') {
  3879. encoding = start
  3880. start = 0
  3881. end = this.length
  3882. } else if (typeof end === 'string') {
  3883. encoding = end
  3884. end = this.length
  3885. }
  3886. if (encoding !== undefined && typeof encoding !== 'string') {
  3887. throw new TypeError('encoding must be a string')
  3888. }
  3889. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  3890. throw new TypeError('Unknown encoding: ' + encoding)
  3891. }
  3892. if (val.length === 1) {
  3893. var code = val.charCodeAt(0)
  3894. if ((encoding === 'utf8' && code < 128) ||
  3895. encoding === 'latin1') {
  3896. // Fast path: If `val` fits into a single byte, use that numeric value.
  3897. val = code
  3898. }
  3899. }
  3900. } else if (typeof val === 'number') {
  3901. val = val & 255
  3902. } else if (typeof val === 'boolean') {
  3903. val = Number(val)
  3904. }
  3905. // Invalid ranges are not set to a default, so can range check early.
  3906. if (start < 0 || this.length < start || this.length < end) {
  3907. throw new RangeError('Out of range index')
  3908. }
  3909. if (end <= start) {
  3910. return this
  3911. }
  3912. start = start >>> 0
  3913. end = end === undefined ? this.length : end >>> 0
  3914. if (!val) val = 0
  3915. var i
  3916. if (typeof val === 'number') {
  3917. for (i = start; i < end; ++i) {
  3918. this[i] = val
  3919. }
  3920. } else {
  3921. var bytes = Buffer.isBuffer(val)
  3922. ? val
  3923. : Buffer.from(val, encoding)
  3924. var len = bytes.length
  3925. if (len === 0) {
  3926. throw new TypeError('The value "' + val +
  3927. '" is invalid for argument "value"')
  3928. }
  3929. for (i = 0; i < end - start; ++i) {
  3930. this[i + start] = bytes[i % len]
  3931. }
  3932. }
  3933. return this
  3934. }
  3935. // HELPER FUNCTIONS
  3936. // ================
  3937. var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
  3938. function base64clean (str) {
  3939. // Node takes equal signs as end of the Base64 encoding
  3940. str = str.split('=')[0]
  3941. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  3942. str = str.trim().replace(INVALID_BASE64_RE, '')
  3943. // Node converts strings with length < 2 to ''
  3944. if (str.length < 2) return ''
  3945. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  3946. while (str.length % 4 !== 0) {
  3947. str = str + '='
  3948. }
  3949. return str
  3950. }
  3951. function utf8ToBytes (string, units) {
  3952. units = units || Infinity
  3953. var codePoint
  3954. var length = string.length
  3955. var leadSurrogate = null
  3956. var bytes = []
  3957. for (var i = 0; i < length; ++i) {
  3958. codePoint = string.charCodeAt(i)
  3959. // is surrogate component
  3960. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  3961. // last char was a lead
  3962. if (!leadSurrogate) {
  3963. // no lead yet
  3964. if (codePoint > 0xDBFF) {
  3965. // unexpected trail
  3966. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3967. continue
  3968. } else if (i + 1 === length) {
  3969. // unpaired lead
  3970. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3971. continue
  3972. }
  3973. // valid lead
  3974. leadSurrogate = codePoint
  3975. continue
  3976. }
  3977. // 2 leads in a row
  3978. if (codePoint < 0xDC00) {
  3979. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3980. leadSurrogate = codePoint
  3981. continue
  3982. }
  3983. // valid surrogate pair
  3984. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  3985. } else if (leadSurrogate) {
  3986. // valid bmp char, but last char was a lead
  3987. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3988. }
  3989. leadSurrogate = null
  3990. // encode utf8
  3991. if (codePoint < 0x80) {
  3992. if ((units -= 1) < 0) break
  3993. bytes.push(codePoint)
  3994. } else if (codePoint < 0x800) {
  3995. if ((units -= 2) < 0) break
  3996. bytes.push(
  3997. codePoint >> 0x6 | 0xC0,
  3998. codePoint & 0x3F | 0x80
  3999. )
  4000. } else if (codePoint < 0x10000) {
  4001. if ((units -= 3) < 0) break
  4002. bytes.push(
  4003. codePoint >> 0xC | 0xE0,
  4004. codePoint >> 0x6 & 0x3F | 0x80,
  4005. codePoint & 0x3F | 0x80
  4006. )
  4007. } else if (codePoint < 0x110000) {
  4008. if ((units -= 4) < 0) break
  4009. bytes.push(
  4010. codePoint >> 0x12 | 0xF0,
  4011. codePoint >> 0xC & 0x3F | 0x80,
  4012. codePoint >> 0x6 & 0x3F | 0x80,
  4013. codePoint & 0x3F | 0x80
  4014. )
  4015. } else {
  4016. throw new Error('Invalid code point')
  4017. }
  4018. }
  4019. return bytes
  4020. }
  4021. function asciiToBytes (str) {
  4022. var byteArray = []
  4023. for (var i = 0; i < str.length; ++i) {
  4024. // Node's code seems to be doing this and not & 0x7F..
  4025. byteArray.push(str.charCodeAt(i) & 0xFF)
  4026. }
  4027. return byteArray
  4028. }
  4029. function utf16leToBytes (str, units) {
  4030. var c, hi, lo
  4031. var byteArray = []
  4032. for (var i = 0; i < str.length; ++i) {
  4033. if ((units -= 2) < 0) break
  4034. c = str.charCodeAt(i)
  4035. hi = c >> 8
  4036. lo = c % 256
  4037. byteArray.push(lo)
  4038. byteArray.push(hi)
  4039. }
  4040. return byteArray
  4041. }
  4042. function base64ToBytes (str) {
  4043. return base64.toByteArray(base64clean(str))
  4044. }
  4045. function blitBuffer (src, dst, offset, length) {
  4046. for (var i = 0; i < length; ++i) {
  4047. if ((i + offset >= dst.length) || (i >= src.length)) break
  4048. dst[i + offset] = src[i]
  4049. }
  4050. return i
  4051. }
  4052. // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
  4053. // the `instanceof` check but they should be treated as of that type.
  4054. // See: https://github.com/feross/buffer/issues/166
  4055. function isInstance (obj, type) {
  4056. return obj instanceof type ||
  4057. (obj != null && obj.constructor != null && obj.constructor.name != null &&
  4058. obj.constructor.name === type.name)
  4059. }
  4060. function numberIsNaN (obj) {
  4061. // For IE11 support
  4062. return obj !== obj // eslint-disable-line no-self-compare
  4063. }
  4064. // Create lookup table for `toString('hex')`
  4065. // See: https://github.com/feross/buffer/issues/219
  4066. var hexSliceLookupTable = (function () {
  4067. var alphabet = '0123456789abcdef'
  4068. var table = new Array(256)
  4069. for (var i = 0; i < 16; ++i) {
  4070. var i16 = i * 16
  4071. for (var j = 0; j < 16; ++j) {
  4072. table[i16 + j] = alphabet[i] + alphabet[j]
  4073. }
  4074. }
  4075. return table
  4076. })()
  4077. }).call(this,require("buffer").Buffer)
  4078. },{"base64-js":14,"buffer":"buffer","ieee754":15}],"wkx":[function(require,module,exports){
  4079. exports.Types = require('./types');
  4080. exports.Geometry = require('./geometry');
  4081. exports.Point = require('./point');
  4082. exports.LineString = require('./linestring');
  4083. exports.Polygon = require('./polygon');
  4084. exports.MultiPoint = require('./multipoint');
  4085. exports.MultiLineString = require('./multilinestring');
  4086. exports.MultiPolygon = require('./multipolygon');
  4087. exports.GeometryCollection = require('./geometrycollection');
  4088. },{"./geometry":3,"./geometrycollection":4,"./linestring":5,"./multilinestring":6,"./multipoint":7,"./multipolygon":8,"./point":9,"./polygon":10,"./types":11}]},{},["wkx"]);