wkx.js 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990
  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. for (var i = 0; i < len; i += 4) {
  1656. tmp =
  1657. (revLookup[b64.charCodeAt(i)] << 18) |
  1658. (revLookup[b64.charCodeAt(i + 1)] << 12) |
  1659. (revLookup[b64.charCodeAt(i + 2)] << 6) |
  1660. revLookup[b64.charCodeAt(i + 3)]
  1661. arr[curByte++] = (tmp >> 16) & 0xFF
  1662. arr[curByte++] = (tmp >> 8) & 0xFF
  1663. arr[curByte++] = tmp & 0xFF
  1664. }
  1665. if (placeHoldersLen === 2) {
  1666. tmp =
  1667. (revLookup[b64.charCodeAt(i)] << 2) |
  1668. (revLookup[b64.charCodeAt(i + 1)] >> 4)
  1669. arr[curByte++] = tmp & 0xFF
  1670. }
  1671. if (placeHoldersLen === 1) {
  1672. tmp =
  1673. (revLookup[b64.charCodeAt(i)] << 10) |
  1674. (revLookup[b64.charCodeAt(i + 1)] << 4) |
  1675. (revLookup[b64.charCodeAt(i + 2)] >> 2)
  1676. arr[curByte++] = (tmp >> 8) & 0xFF
  1677. arr[curByte++] = tmp & 0xFF
  1678. }
  1679. return arr
  1680. }
  1681. function tripletToBase64 (num) {
  1682. return lookup[num >> 18 & 0x3F] +
  1683. lookup[num >> 12 & 0x3F] +
  1684. lookup[num >> 6 & 0x3F] +
  1685. lookup[num & 0x3F]
  1686. }
  1687. function encodeChunk (uint8, start, end) {
  1688. var tmp
  1689. var output = []
  1690. for (var i = start; i < end; i += 3) {
  1691. tmp =
  1692. ((uint8[i] << 16) & 0xFF0000) +
  1693. ((uint8[i + 1] << 8) & 0xFF00) +
  1694. (uint8[i + 2] & 0xFF)
  1695. output.push(tripletToBase64(tmp))
  1696. }
  1697. return output.join('')
  1698. }
  1699. function fromByteArray (uint8) {
  1700. var tmp
  1701. var len = uint8.length
  1702. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  1703. var parts = []
  1704. var maxChunkLength = 16383 // must be multiple of 3
  1705. // go through the array every three bytes, we'll deal with trailing stuff later
  1706. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  1707. parts.push(encodeChunk(
  1708. uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)
  1709. ))
  1710. }
  1711. // pad the end with zeros, but make sure to not forget the extra bytes
  1712. if (extraBytes === 1) {
  1713. tmp = uint8[len - 1]
  1714. parts.push(
  1715. lookup[tmp >> 2] +
  1716. lookup[(tmp << 4) & 0x3F] +
  1717. '=='
  1718. )
  1719. } else if (extraBytes === 2) {
  1720. tmp = (uint8[len - 2] << 8) + uint8[len - 1]
  1721. parts.push(
  1722. lookup[tmp >> 10] +
  1723. lookup[(tmp >> 4) & 0x3F] +
  1724. lookup[(tmp << 2) & 0x3F] +
  1725. '='
  1726. )
  1727. }
  1728. return parts.join('')
  1729. }
  1730. },{}],15:[function(require,module,exports){
  1731. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  1732. var e, m
  1733. var eLen = (nBytes * 8) - mLen - 1
  1734. var eMax = (1 << eLen) - 1
  1735. var eBias = eMax >> 1
  1736. var nBits = -7
  1737. var i = isLE ? (nBytes - 1) : 0
  1738. var d = isLE ? -1 : 1
  1739. var s = buffer[offset + i]
  1740. i += d
  1741. e = s & ((1 << (-nBits)) - 1)
  1742. s >>= (-nBits)
  1743. nBits += eLen
  1744. for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  1745. m = e & ((1 << (-nBits)) - 1)
  1746. e >>= (-nBits)
  1747. nBits += mLen
  1748. for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  1749. if (e === 0) {
  1750. e = 1 - eBias
  1751. } else if (e === eMax) {
  1752. return m ? NaN : ((s ? -1 : 1) * Infinity)
  1753. } else {
  1754. m = m + Math.pow(2, mLen)
  1755. e = e - eBias
  1756. }
  1757. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  1758. }
  1759. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  1760. var e, m, c
  1761. var eLen = (nBytes * 8) - mLen - 1
  1762. var eMax = (1 << eLen) - 1
  1763. var eBias = eMax >> 1
  1764. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  1765. var i = isLE ? 0 : (nBytes - 1)
  1766. var d = isLE ? 1 : -1
  1767. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  1768. value = Math.abs(value)
  1769. if (isNaN(value) || value === Infinity) {
  1770. m = isNaN(value) ? 1 : 0
  1771. e = eMax
  1772. } else {
  1773. e = Math.floor(Math.log(value) / Math.LN2)
  1774. if (value * (c = Math.pow(2, -e)) < 1) {
  1775. e--
  1776. c *= 2
  1777. }
  1778. if (e + eBias >= 1) {
  1779. value += rt / c
  1780. } else {
  1781. value += rt * Math.pow(2, 1 - eBias)
  1782. }
  1783. if (value * c >= 2) {
  1784. e++
  1785. c /= 2
  1786. }
  1787. if (e + eBias >= eMax) {
  1788. m = 0
  1789. e = eMax
  1790. } else if (e + eBias >= 1) {
  1791. m = ((value * c) - 1) * Math.pow(2, mLen)
  1792. e = e + eBias
  1793. } else {
  1794. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  1795. e = 0
  1796. }
  1797. }
  1798. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  1799. e = (e << mLen) | m
  1800. eLen += mLen
  1801. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  1802. buffer[offset + i - d] |= s * 128
  1803. }
  1804. },{}],16:[function(require,module,exports){
  1805. if (typeof Object.create === 'function') {
  1806. // implementation from standard node.js 'util' module
  1807. module.exports = function inherits(ctor, superCtor) {
  1808. ctor.super_ = superCtor
  1809. ctor.prototype = Object.create(superCtor.prototype, {
  1810. constructor: {
  1811. value: ctor,
  1812. enumerable: false,
  1813. writable: true,
  1814. configurable: true
  1815. }
  1816. });
  1817. };
  1818. } else {
  1819. // old school shim for old browsers
  1820. module.exports = function inherits(ctor, superCtor) {
  1821. ctor.super_ = superCtor
  1822. var TempCtor = function () {}
  1823. TempCtor.prototype = superCtor.prototype
  1824. ctor.prototype = new TempCtor()
  1825. ctor.prototype.constructor = ctor
  1826. }
  1827. }
  1828. },{}],17:[function(require,module,exports){
  1829. /*!
  1830. * Determine if an object is a Buffer
  1831. *
  1832. * @author Feross Aboukhadijeh <https://feross.org>
  1833. * @license MIT
  1834. */
  1835. // The _isBuffer check is for Safari 5-7 support, because it's missing
  1836. // Object.prototype.constructor. Remove this eventually
  1837. module.exports = function (obj) {
  1838. return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer)
  1839. }
  1840. function isBuffer (obj) {
  1841. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  1842. }
  1843. // For Node v0.10 support. Remove this eventually.
  1844. function isSlowBuffer (obj) {
  1845. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0))
  1846. }
  1847. },{}],18:[function(require,module,exports){
  1848. // shim for using process in browser
  1849. var process = module.exports = {};
  1850. // cached from whatever global is present so that test runners that stub it
  1851. // don't break things. But we need to wrap it in a try catch in case it is
  1852. // wrapped in strict mode code which doesn't define any globals. It's inside a
  1853. // function because try/catches deoptimize in certain engines.
  1854. var cachedSetTimeout;
  1855. var cachedClearTimeout;
  1856. function defaultSetTimout() {
  1857. throw new Error('setTimeout has not been defined');
  1858. }
  1859. function defaultClearTimeout () {
  1860. throw new Error('clearTimeout has not been defined');
  1861. }
  1862. (function () {
  1863. try {
  1864. if (typeof setTimeout === 'function') {
  1865. cachedSetTimeout = setTimeout;
  1866. } else {
  1867. cachedSetTimeout = defaultSetTimout;
  1868. }
  1869. } catch (e) {
  1870. cachedSetTimeout = defaultSetTimout;
  1871. }
  1872. try {
  1873. if (typeof clearTimeout === 'function') {
  1874. cachedClearTimeout = clearTimeout;
  1875. } else {
  1876. cachedClearTimeout = defaultClearTimeout;
  1877. }
  1878. } catch (e) {
  1879. cachedClearTimeout = defaultClearTimeout;
  1880. }
  1881. } ())
  1882. function runTimeout(fun) {
  1883. if (cachedSetTimeout === setTimeout) {
  1884. //normal enviroments in sane situations
  1885. return setTimeout(fun, 0);
  1886. }
  1887. // if setTimeout wasn't available but was latter defined
  1888. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  1889. cachedSetTimeout = setTimeout;
  1890. return setTimeout(fun, 0);
  1891. }
  1892. try {
  1893. // when when somebody has screwed with setTimeout but no I.E. maddness
  1894. return cachedSetTimeout(fun, 0);
  1895. } catch(e){
  1896. try {
  1897. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1898. return cachedSetTimeout.call(null, fun, 0);
  1899. } catch(e){
  1900. // 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
  1901. return cachedSetTimeout.call(this, fun, 0);
  1902. }
  1903. }
  1904. }
  1905. function runClearTimeout(marker) {
  1906. if (cachedClearTimeout === clearTimeout) {
  1907. //normal enviroments in sane situations
  1908. return clearTimeout(marker);
  1909. }
  1910. // if clearTimeout wasn't available but was latter defined
  1911. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  1912. cachedClearTimeout = clearTimeout;
  1913. return clearTimeout(marker);
  1914. }
  1915. try {
  1916. // when when somebody has screwed with setTimeout but no I.E. maddness
  1917. return cachedClearTimeout(marker);
  1918. } catch (e){
  1919. try {
  1920. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1921. return cachedClearTimeout.call(null, marker);
  1922. } catch (e){
  1923. // 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.
  1924. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  1925. return cachedClearTimeout.call(this, marker);
  1926. }
  1927. }
  1928. }
  1929. var queue = [];
  1930. var draining = false;
  1931. var currentQueue;
  1932. var queueIndex = -1;
  1933. function cleanUpNextTick() {
  1934. if (!draining || !currentQueue) {
  1935. return;
  1936. }
  1937. draining = false;
  1938. if (currentQueue.length) {
  1939. queue = currentQueue.concat(queue);
  1940. } else {
  1941. queueIndex = -1;
  1942. }
  1943. if (queue.length) {
  1944. drainQueue();
  1945. }
  1946. }
  1947. function drainQueue() {
  1948. if (draining) {
  1949. return;
  1950. }
  1951. var timeout = runTimeout(cleanUpNextTick);
  1952. draining = true;
  1953. var len = queue.length;
  1954. while(len) {
  1955. currentQueue = queue;
  1956. queue = [];
  1957. while (++queueIndex < len) {
  1958. if (currentQueue) {
  1959. currentQueue[queueIndex].run();
  1960. }
  1961. }
  1962. queueIndex = -1;
  1963. len = queue.length;
  1964. }
  1965. currentQueue = null;
  1966. draining = false;
  1967. runClearTimeout(timeout);
  1968. }
  1969. process.nextTick = function (fun) {
  1970. var args = new Array(arguments.length - 1);
  1971. if (arguments.length > 1) {
  1972. for (var i = 1; i < arguments.length; i++) {
  1973. args[i - 1] = arguments[i];
  1974. }
  1975. }
  1976. queue.push(new Item(fun, args));
  1977. if (queue.length === 1 && !draining) {
  1978. runTimeout(drainQueue);
  1979. }
  1980. };
  1981. // v8 likes predictible objects
  1982. function Item(fun, array) {
  1983. this.fun = fun;
  1984. this.array = array;
  1985. }
  1986. Item.prototype.run = function () {
  1987. this.fun.apply(null, this.array);
  1988. };
  1989. process.title = 'browser';
  1990. process.browser = true;
  1991. process.env = {};
  1992. process.argv = [];
  1993. process.version = ''; // empty string to avoid regexp issues
  1994. process.versions = {};
  1995. function noop() {}
  1996. process.on = noop;
  1997. process.addListener = noop;
  1998. process.once = noop;
  1999. process.off = noop;
  2000. process.removeListener = noop;
  2001. process.removeAllListeners = noop;
  2002. process.emit = noop;
  2003. process.prependListener = noop;
  2004. process.prependOnceListener = noop;
  2005. process.listeners = function (name) { return [] }
  2006. process.binding = function (name) {
  2007. throw new Error('process.binding is not supported');
  2008. };
  2009. process.cwd = function () { return '/' };
  2010. process.chdir = function (dir) {
  2011. throw new Error('process.chdir is not supported');
  2012. };
  2013. process.umask = function() { return 0; };
  2014. },{}],19:[function(require,module,exports){
  2015. module.exports = function isBuffer(arg) {
  2016. return arg && typeof arg === 'object'
  2017. && typeof arg.copy === 'function'
  2018. && typeof arg.fill === 'function'
  2019. && typeof arg.readUInt8 === 'function';
  2020. }
  2021. },{}],20:[function(require,module,exports){
  2022. (function (process,global){
  2023. // Copyright Joyent, Inc. and other Node contributors.
  2024. //
  2025. // Permission is hereby granted, free of charge, to any person obtaining a
  2026. // copy of this software and associated documentation files (the
  2027. // "Software"), to deal in the Software without restriction, including
  2028. // without limitation the rights to use, copy, modify, merge, publish,
  2029. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2030. // persons to whom the Software is furnished to do so, subject to the
  2031. // following conditions:
  2032. //
  2033. // The above copyright notice and this permission notice shall be included
  2034. // in all copies or substantial portions of the Software.
  2035. //
  2036. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2037. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2038. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2039. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2040. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2041. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2042. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2043. var formatRegExp = /%[sdj%]/g;
  2044. exports.format = function(f) {
  2045. if (!isString(f)) {
  2046. var objects = [];
  2047. for (var i = 0; i < arguments.length; i++) {
  2048. objects.push(inspect(arguments[i]));
  2049. }
  2050. return objects.join(' ');
  2051. }
  2052. var i = 1;
  2053. var args = arguments;
  2054. var len = args.length;
  2055. var str = String(f).replace(formatRegExp, function(x) {
  2056. if (x === '%%') return '%';
  2057. if (i >= len) return x;
  2058. switch (x) {
  2059. case '%s': return String(args[i++]);
  2060. case '%d': return Number(args[i++]);
  2061. case '%j':
  2062. try {
  2063. return JSON.stringify(args[i++]);
  2064. } catch (_) {
  2065. return '[Circular]';
  2066. }
  2067. default:
  2068. return x;
  2069. }
  2070. });
  2071. for (var x = args[i]; i < len; x = args[++i]) {
  2072. if (isNull(x) || !isObject(x)) {
  2073. str += ' ' + x;
  2074. } else {
  2075. str += ' ' + inspect(x);
  2076. }
  2077. }
  2078. return str;
  2079. };
  2080. // Mark that a method should not be used.
  2081. // Returns a modified function which warns once by default.
  2082. // If --no-deprecation is set, then it is a no-op.
  2083. exports.deprecate = function(fn, msg) {
  2084. // Allow for deprecating things in the process of starting up.
  2085. if (isUndefined(global.process)) {
  2086. return function() {
  2087. return exports.deprecate(fn, msg).apply(this, arguments);
  2088. };
  2089. }
  2090. if (process.noDeprecation === true) {
  2091. return fn;
  2092. }
  2093. var warned = false;
  2094. function deprecated() {
  2095. if (!warned) {
  2096. if (process.throwDeprecation) {
  2097. throw new Error(msg);
  2098. } else if (process.traceDeprecation) {
  2099. console.trace(msg);
  2100. } else {
  2101. console.error(msg);
  2102. }
  2103. warned = true;
  2104. }
  2105. return fn.apply(this, arguments);
  2106. }
  2107. return deprecated;
  2108. };
  2109. var debugs = {};
  2110. var debugEnviron;
  2111. exports.debuglog = function(set) {
  2112. if (isUndefined(debugEnviron))
  2113. debugEnviron = process.env.NODE_DEBUG || '';
  2114. set = set.toUpperCase();
  2115. if (!debugs[set]) {
  2116. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  2117. var pid = process.pid;
  2118. debugs[set] = function() {
  2119. var msg = exports.format.apply(exports, arguments);
  2120. console.error('%s %d: %s', set, pid, msg);
  2121. };
  2122. } else {
  2123. debugs[set] = function() {};
  2124. }
  2125. }
  2126. return debugs[set];
  2127. };
  2128. /**
  2129. * Echos the value of a value. Trys to print the value out
  2130. * in the best way possible given the different types.
  2131. *
  2132. * @param {Object} obj The object to print out.
  2133. * @param {Object} opts Optional options object that alters the output.
  2134. */
  2135. /* legacy: obj, showHidden, depth, colors*/
  2136. function inspect(obj, opts) {
  2137. // default options
  2138. var ctx = {
  2139. seen: [],
  2140. stylize: stylizeNoColor
  2141. };
  2142. // legacy...
  2143. if (arguments.length >= 3) ctx.depth = arguments[2];
  2144. if (arguments.length >= 4) ctx.colors = arguments[3];
  2145. if (isBoolean(opts)) {
  2146. // legacy...
  2147. ctx.showHidden = opts;
  2148. } else if (opts) {
  2149. // got an "options" object
  2150. exports._extend(ctx, opts);
  2151. }
  2152. // set default options
  2153. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  2154. if (isUndefined(ctx.depth)) ctx.depth = 2;
  2155. if (isUndefined(ctx.colors)) ctx.colors = false;
  2156. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  2157. if (ctx.colors) ctx.stylize = stylizeWithColor;
  2158. return formatValue(ctx, obj, ctx.depth);
  2159. }
  2160. exports.inspect = inspect;
  2161. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  2162. inspect.colors = {
  2163. 'bold' : [1, 22],
  2164. 'italic' : [3, 23],
  2165. 'underline' : [4, 24],
  2166. 'inverse' : [7, 27],
  2167. 'white' : [37, 39],
  2168. 'grey' : [90, 39],
  2169. 'black' : [30, 39],
  2170. 'blue' : [34, 39],
  2171. 'cyan' : [36, 39],
  2172. 'green' : [32, 39],
  2173. 'magenta' : [35, 39],
  2174. 'red' : [31, 39],
  2175. 'yellow' : [33, 39]
  2176. };
  2177. // Don't use 'blue' not visible on cmd.exe
  2178. inspect.styles = {
  2179. 'special': 'cyan',
  2180. 'number': 'yellow',
  2181. 'boolean': 'yellow',
  2182. 'undefined': 'grey',
  2183. 'null': 'bold',
  2184. 'string': 'green',
  2185. 'date': 'magenta',
  2186. // "name": intentionally not styling
  2187. 'regexp': 'red'
  2188. };
  2189. function stylizeWithColor(str, styleType) {
  2190. var style = inspect.styles[styleType];
  2191. if (style) {
  2192. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  2193. '\u001b[' + inspect.colors[style][1] + 'm';
  2194. } else {
  2195. return str;
  2196. }
  2197. }
  2198. function stylizeNoColor(str, styleType) {
  2199. return str;
  2200. }
  2201. function arrayToHash(array) {
  2202. var hash = {};
  2203. array.forEach(function(val, idx) {
  2204. hash[val] = true;
  2205. });
  2206. return hash;
  2207. }
  2208. function formatValue(ctx, value, recurseTimes) {
  2209. // Provide a hook for user-specified inspect functions.
  2210. // Check that value is an object with an inspect function on it
  2211. if (ctx.customInspect &&
  2212. value &&
  2213. isFunction(value.inspect) &&
  2214. // Filter out the util module, it's inspect function is special
  2215. value.inspect !== exports.inspect &&
  2216. // Also filter out any prototype objects using the circular check.
  2217. !(value.constructor && value.constructor.prototype === value)) {
  2218. var ret = value.inspect(recurseTimes, ctx);
  2219. if (!isString(ret)) {
  2220. ret = formatValue(ctx, ret, recurseTimes);
  2221. }
  2222. return ret;
  2223. }
  2224. // Primitive types cannot have properties
  2225. var primitive = formatPrimitive(ctx, value);
  2226. if (primitive) {
  2227. return primitive;
  2228. }
  2229. // Look up the keys of the object.
  2230. var keys = Object.keys(value);
  2231. var visibleKeys = arrayToHash(keys);
  2232. if (ctx.showHidden) {
  2233. keys = Object.getOwnPropertyNames(value);
  2234. }
  2235. // IE doesn't make error fields non-enumerable
  2236. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  2237. if (isError(value)
  2238. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  2239. return formatError(value);
  2240. }
  2241. // Some type of object without properties can be shortcutted.
  2242. if (keys.length === 0) {
  2243. if (isFunction(value)) {
  2244. var name = value.name ? ': ' + value.name : '';
  2245. return ctx.stylize('[Function' + name + ']', 'special');
  2246. }
  2247. if (isRegExp(value)) {
  2248. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2249. }
  2250. if (isDate(value)) {
  2251. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  2252. }
  2253. if (isError(value)) {
  2254. return formatError(value);
  2255. }
  2256. }
  2257. var base = '', array = false, braces = ['{', '}'];
  2258. // Make Array say that they are Array
  2259. if (isArray(value)) {
  2260. array = true;
  2261. braces = ['[', ']'];
  2262. }
  2263. // Make functions say that they are functions
  2264. if (isFunction(value)) {
  2265. var n = value.name ? ': ' + value.name : '';
  2266. base = ' [Function' + n + ']';
  2267. }
  2268. // Make RegExps say that they are RegExps
  2269. if (isRegExp(value)) {
  2270. base = ' ' + RegExp.prototype.toString.call(value);
  2271. }
  2272. // Make dates with properties first say the date
  2273. if (isDate(value)) {
  2274. base = ' ' + Date.prototype.toUTCString.call(value);
  2275. }
  2276. // Make error with message first say the error
  2277. if (isError(value)) {
  2278. base = ' ' + formatError(value);
  2279. }
  2280. if (keys.length === 0 && (!array || value.length == 0)) {
  2281. return braces[0] + base + braces[1];
  2282. }
  2283. if (recurseTimes < 0) {
  2284. if (isRegExp(value)) {
  2285. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2286. } else {
  2287. return ctx.stylize('[Object]', 'special');
  2288. }
  2289. }
  2290. ctx.seen.push(value);
  2291. var output;
  2292. if (array) {
  2293. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  2294. } else {
  2295. output = keys.map(function(key) {
  2296. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  2297. });
  2298. }
  2299. ctx.seen.pop();
  2300. return reduceToSingleString(output, base, braces);
  2301. }
  2302. function formatPrimitive(ctx, value) {
  2303. if (isUndefined(value))
  2304. return ctx.stylize('undefined', 'undefined');
  2305. if (isString(value)) {
  2306. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  2307. .replace(/'/g, "\\'")
  2308. .replace(/\\"/g, '"') + '\'';
  2309. return ctx.stylize(simple, 'string');
  2310. }
  2311. if (isNumber(value))
  2312. return ctx.stylize('' + value, 'number');
  2313. if (isBoolean(value))
  2314. return ctx.stylize('' + value, 'boolean');
  2315. // For some reason typeof null is "object", so special case here.
  2316. if (isNull(value))
  2317. return ctx.stylize('null', 'null');
  2318. }
  2319. function formatError(value) {
  2320. return '[' + Error.prototype.toString.call(value) + ']';
  2321. }
  2322. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  2323. var output = [];
  2324. for (var i = 0, l = value.length; i < l; ++i) {
  2325. if (hasOwnProperty(value, String(i))) {
  2326. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2327. String(i), true));
  2328. } else {
  2329. output.push('');
  2330. }
  2331. }
  2332. keys.forEach(function(key) {
  2333. if (!key.match(/^\d+$/)) {
  2334. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2335. key, true));
  2336. }
  2337. });
  2338. return output;
  2339. }
  2340. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  2341. var name, str, desc;
  2342. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  2343. if (desc.get) {
  2344. if (desc.set) {
  2345. str = ctx.stylize('[Getter/Setter]', 'special');
  2346. } else {
  2347. str = ctx.stylize('[Getter]', 'special');
  2348. }
  2349. } else {
  2350. if (desc.set) {
  2351. str = ctx.stylize('[Setter]', 'special');
  2352. }
  2353. }
  2354. if (!hasOwnProperty(visibleKeys, key)) {
  2355. name = '[' + key + ']';
  2356. }
  2357. if (!str) {
  2358. if (ctx.seen.indexOf(desc.value) < 0) {
  2359. if (isNull(recurseTimes)) {
  2360. str = formatValue(ctx, desc.value, null);
  2361. } else {
  2362. str = formatValue(ctx, desc.value, recurseTimes - 1);
  2363. }
  2364. if (str.indexOf('\n') > -1) {
  2365. if (array) {
  2366. str = str.split('\n').map(function(line) {
  2367. return ' ' + line;
  2368. }).join('\n').substr(2);
  2369. } else {
  2370. str = '\n' + str.split('\n').map(function(line) {
  2371. return ' ' + line;
  2372. }).join('\n');
  2373. }
  2374. }
  2375. } else {
  2376. str = ctx.stylize('[Circular]', 'special');
  2377. }
  2378. }
  2379. if (isUndefined(name)) {
  2380. if (array && key.match(/^\d+$/)) {
  2381. return str;
  2382. }
  2383. name = JSON.stringify('' + key);
  2384. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  2385. name = name.substr(1, name.length - 2);
  2386. name = ctx.stylize(name, 'name');
  2387. } else {
  2388. name = name.replace(/'/g, "\\'")
  2389. .replace(/\\"/g, '"')
  2390. .replace(/(^"|"$)/g, "'");
  2391. name = ctx.stylize(name, 'string');
  2392. }
  2393. }
  2394. return name + ': ' + str;
  2395. }
  2396. function reduceToSingleString(output, base, braces) {
  2397. var numLinesEst = 0;
  2398. var length = output.reduce(function(prev, cur) {
  2399. numLinesEst++;
  2400. if (cur.indexOf('\n') >= 0) numLinesEst++;
  2401. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  2402. }, 0);
  2403. if (length > 60) {
  2404. return braces[0] +
  2405. (base === '' ? '' : base + '\n ') +
  2406. ' ' +
  2407. output.join(',\n ') +
  2408. ' ' +
  2409. braces[1];
  2410. }
  2411. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  2412. }
  2413. // NOTE: These type checking functions intentionally don't use `instanceof`
  2414. // because it is fragile and can be easily faked with `Object.create()`.
  2415. function isArray(ar) {
  2416. return Array.isArray(ar);
  2417. }
  2418. exports.isArray = isArray;
  2419. function isBoolean(arg) {
  2420. return typeof arg === 'boolean';
  2421. }
  2422. exports.isBoolean = isBoolean;
  2423. function isNull(arg) {
  2424. return arg === null;
  2425. }
  2426. exports.isNull = isNull;
  2427. function isNullOrUndefined(arg) {
  2428. return arg == null;
  2429. }
  2430. exports.isNullOrUndefined = isNullOrUndefined;
  2431. function isNumber(arg) {
  2432. return typeof arg === 'number';
  2433. }
  2434. exports.isNumber = isNumber;
  2435. function isString(arg) {
  2436. return typeof arg === 'string';
  2437. }
  2438. exports.isString = isString;
  2439. function isSymbol(arg) {
  2440. return typeof arg === 'symbol';
  2441. }
  2442. exports.isSymbol = isSymbol;
  2443. function isUndefined(arg) {
  2444. return arg === void 0;
  2445. }
  2446. exports.isUndefined = isUndefined;
  2447. function isRegExp(re) {
  2448. return isObject(re) && objectToString(re) === '[object RegExp]';
  2449. }
  2450. exports.isRegExp = isRegExp;
  2451. function isObject(arg) {
  2452. return typeof arg === 'object' && arg !== null;
  2453. }
  2454. exports.isObject = isObject;
  2455. function isDate(d) {
  2456. return isObject(d) && objectToString(d) === '[object Date]';
  2457. }
  2458. exports.isDate = isDate;
  2459. function isError(e) {
  2460. return isObject(e) &&
  2461. (objectToString(e) === '[object Error]' || e instanceof Error);
  2462. }
  2463. exports.isError = isError;
  2464. function isFunction(arg) {
  2465. return typeof arg === 'function';
  2466. }
  2467. exports.isFunction = isFunction;
  2468. function isPrimitive(arg) {
  2469. return arg === null ||
  2470. typeof arg === 'boolean' ||
  2471. typeof arg === 'number' ||
  2472. typeof arg === 'string' ||
  2473. typeof arg === 'symbol' || // ES6 symbol
  2474. typeof arg === 'undefined';
  2475. }
  2476. exports.isPrimitive = isPrimitive;
  2477. exports.isBuffer = require('./support/isBuffer');
  2478. function objectToString(o) {
  2479. return Object.prototype.toString.call(o);
  2480. }
  2481. function pad(n) {
  2482. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  2483. }
  2484. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  2485. 'Oct', 'Nov', 'Dec'];
  2486. // 26 Feb 16:19:34
  2487. function timestamp() {
  2488. var d = new Date();
  2489. var time = [pad(d.getHours()),
  2490. pad(d.getMinutes()),
  2491. pad(d.getSeconds())].join(':');
  2492. return [d.getDate(), months[d.getMonth()], time].join(' ');
  2493. }
  2494. // log is just a thin wrapper to console.log that prepends a timestamp
  2495. exports.log = function() {
  2496. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  2497. };
  2498. /**
  2499. * Inherit the prototype methods from one constructor into another.
  2500. *
  2501. * The Function.prototype.inherits from lang.js rewritten as a standalone
  2502. * function (not on Function.prototype). NOTE: If this file is to be loaded
  2503. * during bootstrapping this function needs to be rewritten using some native
  2504. * functions as prototype setup using normal JavaScript does not work as
  2505. * expected during bootstrapping (see mirror.js in r114903).
  2506. *
  2507. * @param {function} ctor Constructor function which needs to inherit the
  2508. * prototype.
  2509. * @param {function} superCtor Constructor function to inherit prototype from.
  2510. */
  2511. exports.inherits = require('inherits');
  2512. exports._extend = function(origin, add) {
  2513. // Don't do anything if add isn't an object
  2514. if (!add || !isObject(add)) return origin;
  2515. var keys = Object.keys(add);
  2516. var i = keys.length;
  2517. while (i--) {
  2518. origin[keys[i]] = add[keys[i]];
  2519. }
  2520. return origin;
  2521. };
  2522. function hasOwnProperty(obj, prop) {
  2523. return Object.prototype.hasOwnProperty.call(obj, prop);
  2524. }
  2525. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2526. },{"./support/isBuffer":19,"_process":18,"inherits":16}],"buffer":[function(require,module,exports){
  2527. (function (Buffer){
  2528. /*!
  2529. * The buffer module from node.js, for the browser.
  2530. *
  2531. * @author Feross Aboukhadijeh <https://feross.org>
  2532. * @license MIT
  2533. */
  2534. /* eslint-disable no-proto */
  2535. 'use strict'
  2536. var base64 = require('base64-js')
  2537. var ieee754 = require('ieee754')
  2538. exports.Buffer = Buffer
  2539. exports.SlowBuffer = SlowBuffer
  2540. exports.INSPECT_MAX_BYTES = 50
  2541. var K_MAX_LENGTH = 0x7fffffff
  2542. exports.kMaxLength = K_MAX_LENGTH
  2543. /**
  2544. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  2545. * === true Use Uint8Array implementation (fastest)
  2546. * === false Print warning and recommend using `buffer` v4.x which has an Object
  2547. * implementation (most compatible, even IE6)
  2548. *
  2549. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  2550. * Opera 11.6+, iOS 4.2+.
  2551. *
  2552. * We report that the browser does not support typed arrays if the are not subclassable
  2553. * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
  2554. * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
  2555. * for __proto__ and has a buggy typed array implementation.
  2556. */
  2557. Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
  2558. if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
  2559. typeof console.error === 'function') {
  2560. console.error(
  2561. 'This browser lacks typed array (Uint8Array) support which is required by ' +
  2562. '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
  2563. )
  2564. }
  2565. function typedArraySupport () {
  2566. // Can typed array instances can be augmented?
  2567. try {
  2568. var arr = new Uint8Array(1)
  2569. arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } }
  2570. return arr.foo() === 42
  2571. } catch (e) {
  2572. return false
  2573. }
  2574. }
  2575. Object.defineProperty(Buffer.prototype, 'parent', {
  2576. enumerable: true,
  2577. get: function () {
  2578. if (!Buffer.isBuffer(this)) return undefined
  2579. return this.buffer
  2580. }
  2581. })
  2582. Object.defineProperty(Buffer.prototype, 'offset', {
  2583. enumerable: true,
  2584. get: function () {
  2585. if (!Buffer.isBuffer(this)) return undefined
  2586. return this.byteOffset
  2587. }
  2588. })
  2589. function createBuffer (length) {
  2590. if (length > K_MAX_LENGTH) {
  2591. throw new RangeError('The value "' + length + '" is invalid for option "size"')
  2592. }
  2593. // Return an augmented `Uint8Array` instance
  2594. var buf = new Uint8Array(length)
  2595. buf.__proto__ = Buffer.prototype
  2596. return buf
  2597. }
  2598. /**
  2599. * The Buffer constructor returns instances of `Uint8Array` that have their
  2600. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  2601. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  2602. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  2603. * returns a single octet.
  2604. *
  2605. * The `Uint8Array` prototype remains unmodified.
  2606. */
  2607. function Buffer (arg, encodingOrOffset, length) {
  2608. // Common case.
  2609. if (typeof arg === 'number') {
  2610. if (typeof encodingOrOffset === 'string') {
  2611. throw new TypeError(
  2612. 'The "string" argument must be of type string. Received type number'
  2613. )
  2614. }
  2615. return allocUnsafe(arg)
  2616. }
  2617. return from(arg, encodingOrOffset, length)
  2618. }
  2619. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  2620. if (typeof Symbol !== 'undefined' && Symbol.species != null &&
  2621. Buffer[Symbol.species] === Buffer) {
  2622. Object.defineProperty(Buffer, Symbol.species, {
  2623. value: null,
  2624. configurable: true,
  2625. enumerable: false,
  2626. writable: false
  2627. })
  2628. }
  2629. Buffer.poolSize = 8192 // not used by this implementation
  2630. function from (value, encodingOrOffset, length) {
  2631. if (typeof value === 'string') {
  2632. return fromString(value, encodingOrOffset)
  2633. }
  2634. if (ArrayBuffer.isView(value)) {
  2635. return fromArrayLike(value)
  2636. }
  2637. if (value == null) {
  2638. throw TypeError(
  2639. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  2640. 'or Array-like Object. Received type ' + (typeof value)
  2641. )
  2642. }
  2643. if (isInstance(value, ArrayBuffer) ||
  2644. (value && isInstance(value.buffer, ArrayBuffer))) {
  2645. return fromArrayBuffer(value, encodingOrOffset, length)
  2646. }
  2647. if (typeof value === 'number') {
  2648. throw new TypeError(
  2649. 'The "value" argument must not be of type number. Received type number'
  2650. )
  2651. }
  2652. var valueOf = value.valueOf && value.valueOf()
  2653. if (valueOf != null && valueOf !== value) {
  2654. return Buffer.from(valueOf, encodingOrOffset, length)
  2655. }
  2656. var b = fromObject(value)
  2657. if (b) return b
  2658. if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
  2659. typeof value[Symbol.toPrimitive] === 'function') {
  2660. return Buffer.from(
  2661. value[Symbol.toPrimitive]('string'), encodingOrOffset, length
  2662. )
  2663. }
  2664. throw new TypeError(
  2665. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  2666. 'or Array-like Object. Received type ' + (typeof value)
  2667. )
  2668. }
  2669. /**
  2670. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  2671. * if value is a number.
  2672. * Buffer.from(str[, encoding])
  2673. * Buffer.from(array)
  2674. * Buffer.from(buffer)
  2675. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  2676. **/
  2677. Buffer.from = function (value, encodingOrOffset, length) {
  2678. return from(value, encodingOrOffset, length)
  2679. }
  2680. // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
  2681. // https://github.com/feross/buffer/pull/148
  2682. Buffer.prototype.__proto__ = Uint8Array.prototype
  2683. Buffer.__proto__ = Uint8Array
  2684. function assertSize (size) {
  2685. if (typeof size !== 'number') {
  2686. throw new TypeError('"size" argument must be of type number')
  2687. } else if (size < 0) {
  2688. throw new RangeError('The value "' + size + '" is invalid for option "size"')
  2689. }
  2690. }
  2691. function alloc (size, fill, encoding) {
  2692. assertSize(size)
  2693. if (size <= 0) {
  2694. return createBuffer(size)
  2695. }
  2696. if (fill !== undefined) {
  2697. // Only pay attention to encoding if it's a string. This
  2698. // prevents accidentally sending in a number that would
  2699. // be interpretted as a start offset.
  2700. return typeof encoding === 'string'
  2701. ? createBuffer(size).fill(fill, encoding)
  2702. : createBuffer(size).fill(fill)
  2703. }
  2704. return createBuffer(size)
  2705. }
  2706. /**
  2707. * Creates a new filled Buffer instance.
  2708. * alloc(size[, fill[, encoding]])
  2709. **/
  2710. Buffer.alloc = function (size, fill, encoding) {
  2711. return alloc(size, fill, encoding)
  2712. }
  2713. function allocUnsafe (size) {
  2714. assertSize(size)
  2715. return createBuffer(size < 0 ? 0 : checked(size) | 0)
  2716. }
  2717. /**
  2718. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  2719. * */
  2720. Buffer.allocUnsafe = function (size) {
  2721. return allocUnsafe(size)
  2722. }
  2723. /**
  2724. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  2725. */
  2726. Buffer.allocUnsafeSlow = function (size) {
  2727. return allocUnsafe(size)
  2728. }
  2729. function fromString (string, encoding) {
  2730. if (typeof encoding !== 'string' || encoding === '') {
  2731. encoding = 'utf8'
  2732. }
  2733. if (!Buffer.isEncoding(encoding)) {
  2734. throw new TypeError('Unknown encoding: ' + encoding)
  2735. }
  2736. var length = byteLength(string, encoding) | 0
  2737. var buf = createBuffer(length)
  2738. var actual = buf.write(string, encoding)
  2739. if (actual !== length) {
  2740. // Writing a hex string, for example, that contains invalid characters will
  2741. // cause everything after the first invalid character to be ignored. (e.g.
  2742. // 'abxxcd' will be treated as 'ab')
  2743. buf = buf.slice(0, actual)
  2744. }
  2745. return buf
  2746. }
  2747. function fromArrayLike (array) {
  2748. var length = array.length < 0 ? 0 : checked(array.length) | 0
  2749. var buf = createBuffer(length)
  2750. for (var i = 0; i < length; i += 1) {
  2751. buf[i] = array[i] & 255
  2752. }
  2753. return buf
  2754. }
  2755. function fromArrayBuffer (array, byteOffset, length) {
  2756. if (byteOffset < 0 || array.byteLength < byteOffset) {
  2757. throw new RangeError('"offset" is outside of buffer bounds')
  2758. }
  2759. if (array.byteLength < byteOffset + (length || 0)) {
  2760. throw new RangeError('"length" is outside of buffer bounds')
  2761. }
  2762. var buf
  2763. if (byteOffset === undefined && length === undefined) {
  2764. buf = new Uint8Array(array)
  2765. } else if (length === undefined) {
  2766. buf = new Uint8Array(array, byteOffset)
  2767. } else {
  2768. buf = new Uint8Array(array, byteOffset, length)
  2769. }
  2770. // Return an augmented `Uint8Array` instance
  2771. buf.__proto__ = Buffer.prototype
  2772. return buf
  2773. }
  2774. function fromObject (obj) {
  2775. if (Buffer.isBuffer(obj)) {
  2776. var len = checked(obj.length) | 0
  2777. var buf = createBuffer(len)
  2778. if (buf.length === 0) {
  2779. return buf
  2780. }
  2781. obj.copy(buf, 0, 0, len)
  2782. return buf
  2783. }
  2784. if (obj.length !== undefined) {
  2785. if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
  2786. return createBuffer(0)
  2787. }
  2788. return fromArrayLike(obj)
  2789. }
  2790. if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
  2791. return fromArrayLike(obj.data)
  2792. }
  2793. }
  2794. function checked (length) {
  2795. // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
  2796. // length is NaN (which is otherwise coerced to zero.)
  2797. if (length >= K_MAX_LENGTH) {
  2798. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  2799. 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
  2800. }
  2801. return length | 0
  2802. }
  2803. function SlowBuffer (length) {
  2804. if (+length != length) { // eslint-disable-line eqeqeq
  2805. length = 0
  2806. }
  2807. return Buffer.alloc(+length)
  2808. }
  2809. Buffer.isBuffer = function isBuffer (b) {
  2810. return b != null && b._isBuffer === true &&
  2811. b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
  2812. }
  2813. Buffer.compare = function compare (a, b) {
  2814. if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
  2815. if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
  2816. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  2817. throw new TypeError(
  2818. 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
  2819. )
  2820. }
  2821. if (a === b) return 0
  2822. var x = a.length
  2823. var y = b.length
  2824. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  2825. if (a[i] !== b[i]) {
  2826. x = a[i]
  2827. y = b[i]
  2828. break
  2829. }
  2830. }
  2831. if (x < y) return -1
  2832. if (y < x) return 1
  2833. return 0
  2834. }
  2835. Buffer.isEncoding = function isEncoding (encoding) {
  2836. switch (String(encoding).toLowerCase()) {
  2837. case 'hex':
  2838. case 'utf8':
  2839. case 'utf-8':
  2840. case 'ascii':
  2841. case 'latin1':
  2842. case 'binary':
  2843. case 'base64':
  2844. case 'ucs2':
  2845. case 'ucs-2':
  2846. case 'utf16le':
  2847. case 'utf-16le':
  2848. return true
  2849. default:
  2850. return false
  2851. }
  2852. }
  2853. Buffer.concat = function concat (list, length) {
  2854. if (!Array.isArray(list)) {
  2855. throw new TypeError('"list" argument must be an Array of Buffers')
  2856. }
  2857. if (list.length === 0) {
  2858. return Buffer.alloc(0)
  2859. }
  2860. var i
  2861. if (length === undefined) {
  2862. length = 0
  2863. for (i = 0; i < list.length; ++i) {
  2864. length += list[i].length
  2865. }
  2866. }
  2867. var buffer = Buffer.allocUnsafe(length)
  2868. var pos = 0
  2869. for (i = 0; i < list.length; ++i) {
  2870. var buf = list[i]
  2871. if (isInstance(buf, Uint8Array)) {
  2872. buf = Buffer.from(buf)
  2873. }
  2874. if (!Buffer.isBuffer(buf)) {
  2875. throw new TypeError('"list" argument must be an Array of Buffers')
  2876. }
  2877. buf.copy(buffer, pos)
  2878. pos += buf.length
  2879. }
  2880. return buffer
  2881. }
  2882. function byteLength (string, encoding) {
  2883. if (Buffer.isBuffer(string)) {
  2884. return string.length
  2885. }
  2886. if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
  2887. return string.byteLength
  2888. }
  2889. if (typeof string !== 'string') {
  2890. throw new TypeError(
  2891. 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
  2892. 'Received type ' + typeof string
  2893. )
  2894. }
  2895. var len = string.length
  2896. var mustMatch = (arguments.length > 2 && arguments[2] === true)
  2897. if (!mustMatch && len === 0) return 0
  2898. // Use a for loop to avoid recursion
  2899. var loweredCase = false
  2900. for (;;) {
  2901. switch (encoding) {
  2902. case 'ascii':
  2903. case 'latin1':
  2904. case 'binary':
  2905. return len
  2906. case 'utf8':
  2907. case 'utf-8':
  2908. return utf8ToBytes(string).length
  2909. case 'ucs2':
  2910. case 'ucs-2':
  2911. case 'utf16le':
  2912. case 'utf-16le':
  2913. return len * 2
  2914. case 'hex':
  2915. return len >>> 1
  2916. case 'base64':
  2917. return base64ToBytes(string).length
  2918. default:
  2919. if (loweredCase) {
  2920. return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
  2921. }
  2922. encoding = ('' + encoding).toLowerCase()
  2923. loweredCase = true
  2924. }
  2925. }
  2926. }
  2927. Buffer.byteLength = byteLength
  2928. function slowToString (encoding, start, end) {
  2929. var loweredCase = false
  2930. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  2931. // property of a typed array.
  2932. // This behaves neither like String nor Uint8Array in that we set start/end
  2933. // to their upper/lower bounds if the value passed is out of range.
  2934. // undefined is handled specially as per ECMA-262 6th Edition,
  2935. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  2936. if (start === undefined || start < 0) {
  2937. start = 0
  2938. }
  2939. // Return early if start > this.length. Done here to prevent potential uint32
  2940. // coercion fail below.
  2941. if (start > this.length) {
  2942. return ''
  2943. }
  2944. if (end === undefined || end > this.length) {
  2945. end = this.length
  2946. }
  2947. if (end <= 0) {
  2948. return ''
  2949. }
  2950. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  2951. end >>>= 0
  2952. start >>>= 0
  2953. if (end <= start) {
  2954. return ''
  2955. }
  2956. if (!encoding) encoding = 'utf8'
  2957. while (true) {
  2958. switch (encoding) {
  2959. case 'hex':
  2960. return hexSlice(this, start, end)
  2961. case 'utf8':
  2962. case 'utf-8':
  2963. return utf8Slice(this, start, end)
  2964. case 'ascii':
  2965. return asciiSlice(this, start, end)
  2966. case 'latin1':
  2967. case 'binary':
  2968. return latin1Slice(this, start, end)
  2969. case 'base64':
  2970. return base64Slice(this, start, end)
  2971. case 'ucs2':
  2972. case 'ucs-2':
  2973. case 'utf16le':
  2974. case 'utf-16le':
  2975. return utf16leSlice(this, start, end)
  2976. default:
  2977. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  2978. encoding = (encoding + '').toLowerCase()
  2979. loweredCase = true
  2980. }
  2981. }
  2982. }
  2983. // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
  2984. // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
  2985. // reliably in a browserify context because there could be multiple different
  2986. // copies of the 'buffer' package in use. This method works even for Buffer
  2987. // instances that were created from another copy of the `buffer` package.
  2988. // See: https://github.com/feross/buffer/issues/154
  2989. Buffer.prototype._isBuffer = true
  2990. function swap (b, n, m) {
  2991. var i = b[n]
  2992. b[n] = b[m]
  2993. b[m] = i
  2994. }
  2995. Buffer.prototype.swap16 = function swap16 () {
  2996. var len = this.length
  2997. if (len % 2 !== 0) {
  2998. throw new RangeError('Buffer size must be a multiple of 16-bits')
  2999. }
  3000. for (var i = 0; i < len; i += 2) {
  3001. swap(this, i, i + 1)
  3002. }
  3003. return this
  3004. }
  3005. Buffer.prototype.swap32 = function swap32 () {
  3006. var len = this.length
  3007. if (len % 4 !== 0) {
  3008. throw new RangeError('Buffer size must be a multiple of 32-bits')
  3009. }
  3010. for (var i = 0; i < len; i += 4) {
  3011. swap(this, i, i + 3)
  3012. swap(this, i + 1, i + 2)
  3013. }
  3014. return this
  3015. }
  3016. Buffer.prototype.swap64 = function swap64 () {
  3017. var len = this.length
  3018. if (len % 8 !== 0) {
  3019. throw new RangeError('Buffer size must be a multiple of 64-bits')
  3020. }
  3021. for (var i = 0; i < len; i += 8) {
  3022. swap(this, i, i + 7)
  3023. swap(this, i + 1, i + 6)
  3024. swap(this, i + 2, i + 5)
  3025. swap(this, i + 3, i + 4)
  3026. }
  3027. return this
  3028. }
  3029. Buffer.prototype.toString = function toString () {
  3030. var length = this.length
  3031. if (length === 0) return ''
  3032. if (arguments.length === 0) return utf8Slice(this, 0, length)
  3033. return slowToString.apply(this, arguments)
  3034. }
  3035. Buffer.prototype.toLocaleString = Buffer.prototype.toString
  3036. Buffer.prototype.equals = function equals (b) {
  3037. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  3038. if (this === b) return true
  3039. return Buffer.compare(this, b) === 0
  3040. }
  3041. Buffer.prototype.inspect = function inspect () {
  3042. var str = ''
  3043. var max = exports.INSPECT_MAX_BYTES
  3044. str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
  3045. if (this.length > max) str += ' ... '
  3046. return '<Buffer ' + str + '>'
  3047. }
  3048. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  3049. if (isInstance(target, Uint8Array)) {
  3050. target = Buffer.from(target, target.offset, target.byteLength)
  3051. }
  3052. if (!Buffer.isBuffer(target)) {
  3053. throw new TypeError(
  3054. 'The "target" argument must be one of type Buffer or Uint8Array. ' +
  3055. 'Received type ' + (typeof target)
  3056. )
  3057. }
  3058. if (start === undefined) {
  3059. start = 0
  3060. }
  3061. if (end === undefined) {
  3062. end = target ? target.length : 0
  3063. }
  3064. if (thisStart === undefined) {
  3065. thisStart = 0
  3066. }
  3067. if (thisEnd === undefined) {
  3068. thisEnd = this.length
  3069. }
  3070. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  3071. throw new RangeError('out of range index')
  3072. }
  3073. if (thisStart >= thisEnd && start >= end) {
  3074. return 0
  3075. }
  3076. if (thisStart >= thisEnd) {
  3077. return -1
  3078. }
  3079. if (start >= end) {
  3080. return 1
  3081. }
  3082. start >>>= 0
  3083. end >>>= 0
  3084. thisStart >>>= 0
  3085. thisEnd >>>= 0
  3086. if (this === target) return 0
  3087. var x = thisEnd - thisStart
  3088. var y = end - start
  3089. var len = Math.min(x, y)
  3090. var thisCopy = this.slice(thisStart, thisEnd)
  3091. var targetCopy = target.slice(start, end)
  3092. for (var i = 0; i < len; ++i) {
  3093. if (thisCopy[i] !== targetCopy[i]) {
  3094. x = thisCopy[i]
  3095. y = targetCopy[i]
  3096. break
  3097. }
  3098. }
  3099. if (x < y) return -1
  3100. if (y < x) return 1
  3101. return 0
  3102. }
  3103. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  3104. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  3105. //
  3106. // Arguments:
  3107. // - buffer - a Buffer to search
  3108. // - val - a string, Buffer, or number
  3109. // - byteOffset - an index into `buffer`; will be clamped to an int32
  3110. // - encoding - an optional encoding, relevant is val is a string
  3111. // - dir - true for indexOf, false for lastIndexOf
  3112. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  3113. // Empty buffer means no match
  3114. if (buffer.length === 0) return -1
  3115. // Normalize byteOffset
  3116. if (typeof byteOffset === 'string') {
  3117. encoding = byteOffset
  3118. byteOffset = 0
  3119. } else if (byteOffset > 0x7fffffff) {
  3120. byteOffset = 0x7fffffff
  3121. } else if (byteOffset < -0x80000000) {
  3122. byteOffset = -0x80000000
  3123. }
  3124. byteOffset = +byteOffset // Coerce to Number.
  3125. if (numberIsNaN(byteOffset)) {
  3126. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  3127. byteOffset = dir ? 0 : (buffer.length - 1)
  3128. }
  3129. // Normalize byteOffset: negative offsets start from the end of the buffer
  3130. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  3131. if (byteOffset >= buffer.length) {
  3132. if (dir) return -1
  3133. else byteOffset = buffer.length - 1
  3134. } else if (byteOffset < 0) {
  3135. if (dir) byteOffset = 0
  3136. else return -1
  3137. }
  3138. // Normalize val
  3139. if (typeof val === 'string') {
  3140. val = Buffer.from(val, encoding)
  3141. }
  3142. // Finally, search either indexOf (if dir is true) or lastIndexOf
  3143. if (Buffer.isBuffer(val)) {
  3144. // Special case: looking for empty string/buffer always fails
  3145. if (val.length === 0) {
  3146. return -1
  3147. }
  3148. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  3149. } else if (typeof val === 'number') {
  3150. val = val & 0xFF // Search for a byte value [0-255]
  3151. if (typeof Uint8Array.prototype.indexOf === 'function') {
  3152. if (dir) {
  3153. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  3154. } else {
  3155. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  3156. }
  3157. }
  3158. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  3159. }
  3160. throw new TypeError('val must be string, number or Buffer')
  3161. }
  3162. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  3163. var indexSize = 1
  3164. var arrLength = arr.length
  3165. var valLength = val.length
  3166. if (encoding !== undefined) {
  3167. encoding = String(encoding).toLowerCase()
  3168. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  3169. encoding === 'utf16le' || encoding === 'utf-16le') {
  3170. if (arr.length < 2 || val.length < 2) {
  3171. return -1
  3172. }
  3173. indexSize = 2
  3174. arrLength /= 2
  3175. valLength /= 2
  3176. byteOffset /= 2
  3177. }
  3178. }
  3179. function read (buf, i) {
  3180. if (indexSize === 1) {
  3181. return buf[i]
  3182. } else {
  3183. return buf.readUInt16BE(i * indexSize)
  3184. }
  3185. }
  3186. var i
  3187. if (dir) {
  3188. var foundIndex = -1
  3189. for (i = byteOffset; i < arrLength; i++) {
  3190. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  3191. if (foundIndex === -1) foundIndex = i
  3192. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  3193. } else {
  3194. if (foundIndex !== -1) i -= i - foundIndex
  3195. foundIndex = -1
  3196. }
  3197. }
  3198. } else {
  3199. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  3200. for (i = byteOffset; i >= 0; i--) {
  3201. var found = true
  3202. for (var j = 0; j < valLength; j++) {
  3203. if (read(arr, i + j) !== read(val, j)) {
  3204. found = false
  3205. break
  3206. }
  3207. }
  3208. if (found) return i
  3209. }
  3210. }
  3211. return -1
  3212. }
  3213. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  3214. return this.indexOf(val, byteOffset, encoding) !== -1
  3215. }
  3216. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  3217. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  3218. }
  3219. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  3220. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  3221. }
  3222. function hexWrite (buf, string, offset, length) {
  3223. offset = Number(offset) || 0
  3224. var remaining = buf.length - offset
  3225. if (!length) {
  3226. length = remaining
  3227. } else {
  3228. length = Number(length)
  3229. if (length > remaining) {
  3230. length = remaining
  3231. }
  3232. }
  3233. var strLen = string.length
  3234. if (length > strLen / 2) {
  3235. length = strLen / 2
  3236. }
  3237. for (var i = 0; i < length; ++i) {
  3238. var parsed = parseInt(string.substr(i * 2, 2), 16)
  3239. if (numberIsNaN(parsed)) return i
  3240. buf[offset + i] = parsed
  3241. }
  3242. return i
  3243. }
  3244. function utf8Write (buf, string, offset, length) {
  3245. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  3246. }
  3247. function asciiWrite (buf, string, offset, length) {
  3248. return blitBuffer(asciiToBytes(string), buf, offset, length)
  3249. }
  3250. function latin1Write (buf, string, offset, length) {
  3251. return asciiWrite(buf, string, offset, length)
  3252. }
  3253. function base64Write (buf, string, offset, length) {
  3254. return blitBuffer(base64ToBytes(string), buf, offset, length)
  3255. }
  3256. function ucs2Write (buf, string, offset, length) {
  3257. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  3258. }
  3259. Buffer.prototype.write = function write (string, offset, length, encoding) {
  3260. // Buffer#write(string)
  3261. if (offset === undefined) {
  3262. encoding = 'utf8'
  3263. length = this.length
  3264. offset = 0
  3265. // Buffer#write(string, encoding)
  3266. } else if (length === undefined && typeof offset === 'string') {
  3267. encoding = offset
  3268. length = this.length
  3269. offset = 0
  3270. // Buffer#write(string, offset[, length][, encoding])
  3271. } else if (isFinite(offset)) {
  3272. offset = offset >>> 0
  3273. if (isFinite(length)) {
  3274. length = length >>> 0
  3275. if (encoding === undefined) encoding = 'utf8'
  3276. } else {
  3277. encoding = length
  3278. length = undefined
  3279. }
  3280. } else {
  3281. throw new Error(
  3282. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  3283. )
  3284. }
  3285. var remaining = this.length - offset
  3286. if (length === undefined || length > remaining) length = remaining
  3287. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  3288. throw new RangeError('Attempt to write outside buffer bounds')
  3289. }
  3290. if (!encoding) encoding = 'utf8'
  3291. var loweredCase = false
  3292. for (;;) {
  3293. switch (encoding) {
  3294. case 'hex':
  3295. return hexWrite(this, string, offset, length)
  3296. case 'utf8':
  3297. case 'utf-8':
  3298. return utf8Write(this, string, offset, length)
  3299. case 'ascii':
  3300. return asciiWrite(this, string, offset, length)
  3301. case 'latin1':
  3302. case 'binary':
  3303. return latin1Write(this, string, offset, length)
  3304. case 'base64':
  3305. // Warning: maxLength not taken into account in base64Write
  3306. return base64Write(this, string, offset, length)
  3307. case 'ucs2':
  3308. case 'ucs-2':
  3309. case 'utf16le':
  3310. case 'utf-16le':
  3311. return ucs2Write(this, string, offset, length)
  3312. default:
  3313. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  3314. encoding = ('' + encoding).toLowerCase()
  3315. loweredCase = true
  3316. }
  3317. }
  3318. }
  3319. Buffer.prototype.toJSON = function toJSON () {
  3320. return {
  3321. type: 'Buffer',
  3322. data: Array.prototype.slice.call(this._arr || this, 0)
  3323. }
  3324. }
  3325. function base64Slice (buf, start, end) {
  3326. if (start === 0 && end === buf.length) {
  3327. return base64.fromByteArray(buf)
  3328. } else {
  3329. return base64.fromByteArray(buf.slice(start, end))
  3330. }
  3331. }
  3332. function utf8Slice (buf, start, end) {
  3333. end = Math.min(buf.length, end)
  3334. var res = []
  3335. var i = start
  3336. while (i < end) {
  3337. var firstByte = buf[i]
  3338. var codePoint = null
  3339. var bytesPerSequence = (firstByte > 0xEF) ? 4
  3340. : (firstByte > 0xDF) ? 3
  3341. : (firstByte > 0xBF) ? 2
  3342. : 1
  3343. if (i + bytesPerSequence <= end) {
  3344. var secondByte, thirdByte, fourthByte, tempCodePoint
  3345. switch (bytesPerSequence) {
  3346. case 1:
  3347. if (firstByte < 0x80) {
  3348. codePoint = firstByte
  3349. }
  3350. break
  3351. case 2:
  3352. secondByte = buf[i + 1]
  3353. if ((secondByte & 0xC0) === 0x80) {
  3354. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  3355. if (tempCodePoint > 0x7F) {
  3356. codePoint = tempCodePoint
  3357. }
  3358. }
  3359. break
  3360. case 3:
  3361. secondByte = buf[i + 1]
  3362. thirdByte = buf[i + 2]
  3363. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  3364. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  3365. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  3366. codePoint = tempCodePoint
  3367. }
  3368. }
  3369. break
  3370. case 4:
  3371. secondByte = buf[i + 1]
  3372. thirdByte = buf[i + 2]
  3373. fourthByte = buf[i + 3]
  3374. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  3375. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  3376. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  3377. codePoint = tempCodePoint
  3378. }
  3379. }
  3380. }
  3381. }
  3382. if (codePoint === null) {
  3383. // we did not generate a valid codePoint so insert a
  3384. // replacement char (U+FFFD) and advance only 1 byte
  3385. codePoint = 0xFFFD
  3386. bytesPerSequence = 1
  3387. } else if (codePoint > 0xFFFF) {
  3388. // encode to utf16 (surrogate pair dance)
  3389. codePoint -= 0x10000
  3390. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  3391. codePoint = 0xDC00 | codePoint & 0x3FF
  3392. }
  3393. res.push(codePoint)
  3394. i += bytesPerSequence
  3395. }
  3396. return decodeCodePointsArray(res)
  3397. }
  3398. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  3399. // the lowest limit is Chrome, with 0x10000 args.
  3400. // We go 1 magnitude less, for safety
  3401. var MAX_ARGUMENTS_LENGTH = 0x1000
  3402. function decodeCodePointsArray (codePoints) {
  3403. var len = codePoints.length
  3404. if (len <= MAX_ARGUMENTS_LENGTH) {
  3405. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  3406. }
  3407. // Decode in chunks to avoid "call stack size exceeded".
  3408. var res = ''
  3409. var i = 0
  3410. while (i < len) {
  3411. res += String.fromCharCode.apply(
  3412. String,
  3413. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  3414. )
  3415. }
  3416. return res
  3417. }
  3418. function asciiSlice (buf, start, end) {
  3419. var ret = ''
  3420. end = Math.min(buf.length, end)
  3421. for (var i = start; i < end; ++i) {
  3422. ret += String.fromCharCode(buf[i] & 0x7F)
  3423. }
  3424. return ret
  3425. }
  3426. function latin1Slice (buf, start, end) {
  3427. var ret = ''
  3428. end = Math.min(buf.length, end)
  3429. for (var i = start; i < end; ++i) {
  3430. ret += String.fromCharCode(buf[i])
  3431. }
  3432. return ret
  3433. }
  3434. function hexSlice (buf, start, end) {
  3435. var len = buf.length
  3436. if (!start || start < 0) start = 0
  3437. if (!end || end < 0 || end > len) end = len
  3438. var out = ''
  3439. for (var i = start; i < end; ++i) {
  3440. out += toHex(buf[i])
  3441. }
  3442. return out
  3443. }
  3444. function utf16leSlice (buf, start, end) {
  3445. var bytes = buf.slice(start, end)
  3446. var res = ''
  3447. for (var i = 0; i < bytes.length; i += 2) {
  3448. res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
  3449. }
  3450. return res
  3451. }
  3452. Buffer.prototype.slice = function slice (start, end) {
  3453. var len = this.length
  3454. start = ~~start
  3455. end = end === undefined ? len : ~~end
  3456. if (start < 0) {
  3457. start += len
  3458. if (start < 0) start = 0
  3459. } else if (start > len) {
  3460. start = len
  3461. }
  3462. if (end < 0) {
  3463. end += len
  3464. if (end < 0) end = 0
  3465. } else if (end > len) {
  3466. end = len
  3467. }
  3468. if (end < start) end = start
  3469. var newBuf = this.subarray(start, end)
  3470. // Return an augmented `Uint8Array` instance
  3471. newBuf.__proto__ = Buffer.prototype
  3472. return newBuf
  3473. }
  3474. /*
  3475. * Need to make sure that buffer isn't trying to write out of bounds.
  3476. */
  3477. function checkOffset (offset, ext, length) {
  3478. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  3479. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  3480. }
  3481. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  3482. offset = offset >>> 0
  3483. byteLength = byteLength >>> 0
  3484. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3485. var val = this[offset]
  3486. var mul = 1
  3487. var i = 0
  3488. while (++i < byteLength && (mul *= 0x100)) {
  3489. val += this[offset + i] * mul
  3490. }
  3491. return val
  3492. }
  3493. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  3494. offset = offset >>> 0
  3495. byteLength = byteLength >>> 0
  3496. if (!noAssert) {
  3497. checkOffset(offset, byteLength, this.length)
  3498. }
  3499. var val = this[offset + --byteLength]
  3500. var mul = 1
  3501. while (byteLength > 0 && (mul *= 0x100)) {
  3502. val += this[offset + --byteLength] * mul
  3503. }
  3504. return val
  3505. }
  3506. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  3507. offset = offset >>> 0
  3508. if (!noAssert) checkOffset(offset, 1, this.length)
  3509. return this[offset]
  3510. }
  3511. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  3512. offset = offset >>> 0
  3513. if (!noAssert) checkOffset(offset, 2, this.length)
  3514. return this[offset] | (this[offset + 1] << 8)
  3515. }
  3516. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  3517. offset = offset >>> 0
  3518. if (!noAssert) checkOffset(offset, 2, this.length)
  3519. return (this[offset] << 8) | this[offset + 1]
  3520. }
  3521. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  3522. offset = offset >>> 0
  3523. if (!noAssert) checkOffset(offset, 4, this.length)
  3524. return ((this[offset]) |
  3525. (this[offset + 1] << 8) |
  3526. (this[offset + 2] << 16)) +
  3527. (this[offset + 3] * 0x1000000)
  3528. }
  3529. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  3530. offset = offset >>> 0
  3531. if (!noAssert) checkOffset(offset, 4, this.length)
  3532. return (this[offset] * 0x1000000) +
  3533. ((this[offset + 1] << 16) |
  3534. (this[offset + 2] << 8) |
  3535. this[offset + 3])
  3536. }
  3537. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  3538. offset = offset >>> 0
  3539. byteLength = byteLength >>> 0
  3540. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3541. var val = this[offset]
  3542. var mul = 1
  3543. var i = 0
  3544. while (++i < byteLength && (mul *= 0x100)) {
  3545. val += this[offset + i] * mul
  3546. }
  3547. mul *= 0x80
  3548. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  3549. return val
  3550. }
  3551. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  3552. offset = offset >>> 0
  3553. byteLength = byteLength >>> 0
  3554. if (!noAssert) checkOffset(offset, byteLength, this.length)
  3555. var i = byteLength
  3556. var mul = 1
  3557. var val = this[offset + --i]
  3558. while (i > 0 && (mul *= 0x100)) {
  3559. val += this[offset + --i] * mul
  3560. }
  3561. mul *= 0x80
  3562. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  3563. return val
  3564. }
  3565. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  3566. offset = offset >>> 0
  3567. if (!noAssert) checkOffset(offset, 1, this.length)
  3568. if (!(this[offset] & 0x80)) return (this[offset])
  3569. return ((0xff - this[offset] + 1) * -1)
  3570. }
  3571. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  3572. offset = offset >>> 0
  3573. if (!noAssert) checkOffset(offset, 2, this.length)
  3574. var val = this[offset] | (this[offset + 1] << 8)
  3575. return (val & 0x8000) ? val | 0xFFFF0000 : val
  3576. }
  3577. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  3578. offset = offset >>> 0
  3579. if (!noAssert) checkOffset(offset, 2, this.length)
  3580. var val = this[offset + 1] | (this[offset] << 8)
  3581. return (val & 0x8000) ? val | 0xFFFF0000 : val
  3582. }
  3583. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  3584. offset = offset >>> 0
  3585. if (!noAssert) checkOffset(offset, 4, this.length)
  3586. return (this[offset]) |
  3587. (this[offset + 1] << 8) |
  3588. (this[offset + 2] << 16) |
  3589. (this[offset + 3] << 24)
  3590. }
  3591. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  3592. offset = offset >>> 0
  3593. if (!noAssert) checkOffset(offset, 4, this.length)
  3594. return (this[offset] << 24) |
  3595. (this[offset + 1] << 16) |
  3596. (this[offset + 2] << 8) |
  3597. (this[offset + 3])
  3598. }
  3599. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  3600. offset = offset >>> 0
  3601. if (!noAssert) checkOffset(offset, 4, this.length)
  3602. return ieee754.read(this, offset, true, 23, 4)
  3603. }
  3604. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  3605. offset = offset >>> 0
  3606. if (!noAssert) checkOffset(offset, 4, this.length)
  3607. return ieee754.read(this, offset, false, 23, 4)
  3608. }
  3609. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  3610. offset = offset >>> 0
  3611. if (!noAssert) checkOffset(offset, 8, this.length)
  3612. return ieee754.read(this, offset, true, 52, 8)
  3613. }
  3614. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  3615. offset = offset >>> 0
  3616. if (!noAssert) checkOffset(offset, 8, this.length)
  3617. return ieee754.read(this, offset, false, 52, 8)
  3618. }
  3619. function checkInt (buf, value, offset, ext, max, min) {
  3620. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  3621. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  3622. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  3623. }
  3624. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  3625. value = +value
  3626. offset = offset >>> 0
  3627. byteLength = byteLength >>> 0
  3628. if (!noAssert) {
  3629. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  3630. checkInt(this, value, offset, byteLength, maxBytes, 0)
  3631. }
  3632. var mul = 1
  3633. var i = 0
  3634. this[offset] = value & 0xFF
  3635. while (++i < byteLength && (mul *= 0x100)) {
  3636. this[offset + i] = (value / mul) & 0xFF
  3637. }
  3638. return offset + byteLength
  3639. }
  3640. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  3641. value = +value
  3642. offset = offset >>> 0
  3643. byteLength = byteLength >>> 0
  3644. if (!noAssert) {
  3645. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  3646. checkInt(this, value, offset, byteLength, maxBytes, 0)
  3647. }
  3648. var i = byteLength - 1
  3649. var mul = 1
  3650. this[offset + i] = value & 0xFF
  3651. while (--i >= 0 && (mul *= 0x100)) {
  3652. this[offset + i] = (value / mul) & 0xFF
  3653. }
  3654. return offset + byteLength
  3655. }
  3656. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  3657. value = +value
  3658. offset = offset >>> 0
  3659. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  3660. this[offset] = (value & 0xff)
  3661. return offset + 1
  3662. }
  3663. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  3664. value = +value
  3665. offset = offset >>> 0
  3666. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  3667. this[offset] = (value & 0xff)
  3668. this[offset + 1] = (value >>> 8)
  3669. return offset + 2
  3670. }
  3671. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  3672. value = +value
  3673. offset = offset >>> 0
  3674. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  3675. this[offset] = (value >>> 8)
  3676. this[offset + 1] = (value & 0xff)
  3677. return offset + 2
  3678. }
  3679. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  3680. value = +value
  3681. offset = offset >>> 0
  3682. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  3683. this[offset + 3] = (value >>> 24)
  3684. this[offset + 2] = (value >>> 16)
  3685. this[offset + 1] = (value >>> 8)
  3686. this[offset] = (value & 0xff)
  3687. return offset + 4
  3688. }
  3689. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  3690. value = +value
  3691. offset = offset >>> 0
  3692. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  3693. this[offset] = (value >>> 24)
  3694. this[offset + 1] = (value >>> 16)
  3695. this[offset + 2] = (value >>> 8)
  3696. this[offset + 3] = (value & 0xff)
  3697. return offset + 4
  3698. }
  3699. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  3700. value = +value
  3701. offset = offset >>> 0
  3702. if (!noAssert) {
  3703. var limit = Math.pow(2, (8 * byteLength) - 1)
  3704. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  3705. }
  3706. var i = 0
  3707. var mul = 1
  3708. var sub = 0
  3709. this[offset] = value & 0xFF
  3710. while (++i < byteLength && (mul *= 0x100)) {
  3711. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  3712. sub = 1
  3713. }
  3714. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  3715. }
  3716. return offset + byteLength
  3717. }
  3718. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  3719. value = +value
  3720. offset = offset >>> 0
  3721. if (!noAssert) {
  3722. var limit = Math.pow(2, (8 * byteLength) - 1)
  3723. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  3724. }
  3725. var i = byteLength - 1
  3726. var mul = 1
  3727. var sub = 0
  3728. this[offset + i] = value & 0xFF
  3729. while (--i >= 0 && (mul *= 0x100)) {
  3730. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  3731. sub = 1
  3732. }
  3733. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  3734. }
  3735. return offset + byteLength
  3736. }
  3737. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  3738. value = +value
  3739. offset = offset >>> 0
  3740. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  3741. if (value < 0) value = 0xff + value + 1
  3742. this[offset] = (value & 0xff)
  3743. return offset + 1
  3744. }
  3745. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  3746. value = +value
  3747. offset = offset >>> 0
  3748. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3749. this[offset] = (value & 0xff)
  3750. this[offset + 1] = (value >>> 8)
  3751. return offset + 2
  3752. }
  3753. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  3754. value = +value
  3755. offset = offset >>> 0
  3756. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3757. this[offset] = (value >>> 8)
  3758. this[offset + 1] = (value & 0xff)
  3759. return offset + 2
  3760. }
  3761. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  3762. value = +value
  3763. offset = offset >>> 0
  3764. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3765. this[offset] = (value & 0xff)
  3766. this[offset + 1] = (value >>> 8)
  3767. this[offset + 2] = (value >>> 16)
  3768. this[offset + 3] = (value >>> 24)
  3769. return offset + 4
  3770. }
  3771. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  3772. value = +value
  3773. offset = offset >>> 0
  3774. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3775. if (value < 0) value = 0xffffffff + value + 1
  3776. this[offset] = (value >>> 24)
  3777. this[offset + 1] = (value >>> 16)
  3778. this[offset + 2] = (value >>> 8)
  3779. this[offset + 3] = (value & 0xff)
  3780. return offset + 4
  3781. }
  3782. function checkIEEE754 (buf, value, offset, ext, max, min) {
  3783. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  3784. if (offset < 0) throw new RangeError('Index out of range')
  3785. }
  3786. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  3787. value = +value
  3788. offset = offset >>> 0
  3789. if (!noAssert) {
  3790. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  3791. }
  3792. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  3793. return offset + 4
  3794. }
  3795. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  3796. return writeFloat(this, value, offset, true, noAssert)
  3797. }
  3798. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  3799. return writeFloat(this, value, offset, false, noAssert)
  3800. }
  3801. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  3802. value = +value
  3803. offset = offset >>> 0
  3804. if (!noAssert) {
  3805. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  3806. }
  3807. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  3808. return offset + 8
  3809. }
  3810. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  3811. return writeDouble(this, value, offset, true, noAssert)
  3812. }
  3813. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  3814. return writeDouble(this, value, offset, false, noAssert)
  3815. }
  3816. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  3817. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  3818. if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
  3819. if (!start) start = 0
  3820. if (!end && end !== 0) end = this.length
  3821. if (targetStart >= target.length) targetStart = target.length
  3822. if (!targetStart) targetStart = 0
  3823. if (end > 0 && end < start) end = start
  3824. // Copy 0 bytes; we're done
  3825. if (end === start) return 0
  3826. if (target.length === 0 || this.length === 0) return 0
  3827. // Fatal error conditions
  3828. if (targetStart < 0) {
  3829. throw new RangeError('targetStart out of bounds')
  3830. }
  3831. if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
  3832. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  3833. // Are we oob?
  3834. if (end > this.length) end = this.length
  3835. if (target.length - targetStart < end - start) {
  3836. end = target.length - targetStart + start
  3837. }
  3838. var len = end - start
  3839. if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
  3840. // Use built-in when available, missing from IE11
  3841. this.copyWithin(targetStart, start, end)
  3842. } else if (this === target && start < targetStart && targetStart < end) {
  3843. // descending copy from end
  3844. for (var i = len - 1; i >= 0; --i) {
  3845. target[i + targetStart] = this[i + start]
  3846. }
  3847. } else {
  3848. Uint8Array.prototype.set.call(
  3849. target,
  3850. this.subarray(start, end),
  3851. targetStart
  3852. )
  3853. }
  3854. return len
  3855. }
  3856. // Usage:
  3857. // buffer.fill(number[, offset[, end]])
  3858. // buffer.fill(buffer[, offset[, end]])
  3859. // buffer.fill(string[, offset[, end]][, encoding])
  3860. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  3861. // Handle string cases:
  3862. if (typeof val === 'string') {
  3863. if (typeof start === 'string') {
  3864. encoding = start
  3865. start = 0
  3866. end = this.length
  3867. } else if (typeof end === 'string') {
  3868. encoding = end
  3869. end = this.length
  3870. }
  3871. if (encoding !== undefined && typeof encoding !== 'string') {
  3872. throw new TypeError('encoding must be a string')
  3873. }
  3874. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  3875. throw new TypeError('Unknown encoding: ' + encoding)
  3876. }
  3877. if (val.length === 1) {
  3878. var code = val.charCodeAt(0)
  3879. if ((encoding === 'utf8' && code < 128) ||
  3880. encoding === 'latin1') {
  3881. // Fast path: If `val` fits into a single byte, use that numeric value.
  3882. val = code
  3883. }
  3884. }
  3885. } else if (typeof val === 'number') {
  3886. val = val & 255
  3887. }
  3888. // Invalid ranges are not set to a default, so can range check early.
  3889. if (start < 0 || this.length < start || this.length < end) {
  3890. throw new RangeError('Out of range index')
  3891. }
  3892. if (end <= start) {
  3893. return this
  3894. }
  3895. start = start >>> 0
  3896. end = end === undefined ? this.length : end >>> 0
  3897. if (!val) val = 0
  3898. var i
  3899. if (typeof val === 'number') {
  3900. for (i = start; i < end; ++i) {
  3901. this[i] = val
  3902. }
  3903. } else {
  3904. var bytes = Buffer.isBuffer(val)
  3905. ? val
  3906. : Buffer.from(val, encoding)
  3907. var len = bytes.length
  3908. if (len === 0) {
  3909. throw new TypeError('The value "' + val +
  3910. '" is invalid for argument "value"')
  3911. }
  3912. for (i = 0; i < end - start; ++i) {
  3913. this[i + start] = bytes[i % len]
  3914. }
  3915. }
  3916. return this
  3917. }
  3918. // HELPER FUNCTIONS
  3919. // ================
  3920. var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
  3921. function base64clean (str) {
  3922. // Node takes equal signs as end of the Base64 encoding
  3923. str = str.split('=')[0]
  3924. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  3925. str = str.trim().replace(INVALID_BASE64_RE, '')
  3926. // Node converts strings with length < 2 to ''
  3927. if (str.length < 2) return ''
  3928. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  3929. while (str.length % 4 !== 0) {
  3930. str = str + '='
  3931. }
  3932. return str
  3933. }
  3934. function toHex (n) {
  3935. if (n < 16) return '0' + n.toString(16)
  3936. return n.toString(16)
  3937. }
  3938. function utf8ToBytes (string, units) {
  3939. units = units || Infinity
  3940. var codePoint
  3941. var length = string.length
  3942. var leadSurrogate = null
  3943. var bytes = []
  3944. for (var i = 0; i < length; ++i) {
  3945. codePoint = string.charCodeAt(i)
  3946. // is surrogate component
  3947. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  3948. // last char was a lead
  3949. if (!leadSurrogate) {
  3950. // no lead yet
  3951. if (codePoint > 0xDBFF) {
  3952. // unexpected trail
  3953. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3954. continue
  3955. } else if (i + 1 === length) {
  3956. // unpaired lead
  3957. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3958. continue
  3959. }
  3960. // valid lead
  3961. leadSurrogate = codePoint
  3962. continue
  3963. }
  3964. // 2 leads in a row
  3965. if (codePoint < 0xDC00) {
  3966. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3967. leadSurrogate = codePoint
  3968. continue
  3969. }
  3970. // valid surrogate pair
  3971. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  3972. } else if (leadSurrogate) {
  3973. // valid bmp char, but last char was a lead
  3974. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  3975. }
  3976. leadSurrogate = null
  3977. // encode utf8
  3978. if (codePoint < 0x80) {
  3979. if ((units -= 1) < 0) break
  3980. bytes.push(codePoint)
  3981. } else if (codePoint < 0x800) {
  3982. if ((units -= 2) < 0) break
  3983. bytes.push(
  3984. codePoint >> 0x6 | 0xC0,
  3985. codePoint & 0x3F | 0x80
  3986. )
  3987. } else if (codePoint < 0x10000) {
  3988. if ((units -= 3) < 0) break
  3989. bytes.push(
  3990. codePoint >> 0xC | 0xE0,
  3991. codePoint >> 0x6 & 0x3F | 0x80,
  3992. codePoint & 0x3F | 0x80
  3993. )
  3994. } else if (codePoint < 0x110000) {
  3995. if ((units -= 4) < 0) break
  3996. bytes.push(
  3997. codePoint >> 0x12 | 0xF0,
  3998. codePoint >> 0xC & 0x3F | 0x80,
  3999. codePoint >> 0x6 & 0x3F | 0x80,
  4000. codePoint & 0x3F | 0x80
  4001. )
  4002. } else {
  4003. throw new Error('Invalid code point')
  4004. }
  4005. }
  4006. return bytes
  4007. }
  4008. function asciiToBytes (str) {
  4009. var byteArray = []
  4010. for (var i = 0; i < str.length; ++i) {
  4011. // Node's code seems to be doing this and not & 0x7F..
  4012. byteArray.push(str.charCodeAt(i) & 0xFF)
  4013. }
  4014. return byteArray
  4015. }
  4016. function utf16leToBytes (str, units) {
  4017. var c, hi, lo
  4018. var byteArray = []
  4019. for (var i = 0; i < str.length; ++i) {
  4020. if ((units -= 2) < 0) break
  4021. c = str.charCodeAt(i)
  4022. hi = c >> 8
  4023. lo = c % 256
  4024. byteArray.push(lo)
  4025. byteArray.push(hi)
  4026. }
  4027. return byteArray
  4028. }
  4029. function base64ToBytes (str) {
  4030. return base64.toByteArray(base64clean(str))
  4031. }
  4032. function blitBuffer (src, dst, offset, length) {
  4033. for (var i = 0; i < length; ++i) {
  4034. if ((i + offset >= dst.length) || (i >= src.length)) break
  4035. dst[i + offset] = src[i]
  4036. }
  4037. return i
  4038. }
  4039. // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
  4040. // the `instanceof` check but they should be treated as of that type.
  4041. // See: https://github.com/feross/buffer/issues/166
  4042. function isInstance (obj, type) {
  4043. return obj instanceof type ||
  4044. (obj != null && obj.constructor != null && obj.constructor.name != null &&
  4045. obj.constructor.name === type.name)
  4046. }
  4047. function numberIsNaN (obj) {
  4048. // For IE11 support
  4049. return obj !== obj // eslint-disable-line no-self-compare
  4050. }
  4051. }).call(this,require("buffer").Buffer)
  4052. },{"base64-js":14,"buffer":"buffer","ieee754":15}],"wkx":[function(require,module,exports){
  4053. exports.Types = require('./types');
  4054. exports.Geometry = require('./geometry');
  4055. exports.Point = require('./point');
  4056. exports.LineString = require('./linestring');
  4057. exports.Polygon = require('./polygon');
  4058. exports.MultiPoint = require('./multipoint');
  4059. exports.MultiLineString = require('./multilinestring');
  4060. exports.MultiPolygon = require('./multipolygon');
  4061. exports.GeometryCollection = require('./geometrycollection');
  4062. },{"./geometry":3,"./geometrycollection":4,"./linestring":5,"./multilinestring":6,"./multipoint":7,"./multipolygon":8,"./point":9,"./polygon":10,"./types":11}]},{},["wkx"]);