bundle.js 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. (function (Buffer){
  3. var ws = require('websocket-stream');
  4. var through = require('through2');
  5. // create connection to websocket server
  6. var stream = ws('ws://localhost:8001');
  7. stream.on('error', function(err){
  8. // console.log(err);
  9. });
  10. var domstream = require('domnode-dom');
  11. var hello = {
  12. "source": "browser",
  13. "id": new Date().getTime(),
  14. "url": window.location.href
  15. };
  16. stream.write(new Buffer(JSON.stringify(hello)))
  17. stream.on('data', function(x) {
  18. // console.log('data from server', x.toString());
  19. var data = JSON.parse(x.toString());
  20. var selector = data.selector;
  21. // if there is no data, assume its a request to bind a new event listerer from bpipe
  22. if (typeof data.data === "undefined") {
  23. var inputElement = document.querySelectorAll(selector)[0];
  24. var inputStream = domstream.createEventStream(inputElement, data.event);
  25. // console.log('mapping new element', inputElement, selector)
  26. inputStream.pipe(through(function(chunk, enc, cb){
  27. var data = {
  28. "selector": selector,
  29. "data": chunk.toString(),
  30. "source": "browser",
  31. "url": window.location.href
  32. };
  33. cb(null, JSON.stringify(data));
  34. }))
  35. .pipe(stream);
  36. } else {
  37. // data incoming from bpipe, send it to the browser
  38. var outputElement = document.querySelectorAll(selector)[0];
  39. var outputStream = domstream.createWriteStream(outputElement, 'text/plain');
  40. //console.log('incoming data from bpipe', data, outputElement);
  41. outputStream.write(data.data);
  42. }
  43. });
  44. }).call(this,require("buffer").Buffer)
  45. },{"buffer":44,"domnode-dom":2,"through2":16,"websocket-stream":17}],2:[function(require,module,exports){
  46. module.exports = require('./lib/index')
  47. },{"./lib/index":3}],3:[function(require,module,exports){
  48. var WriteStream = require('./writable')
  49. , ReadStream = require('./readable')
  50. , DOMStream = {}
  51. DOMStream.WriteStream = WriteStream
  52. DOMStream.ReadStream = ReadStream
  53. DOMStream.createAppendStream = function(el, mimetype) {
  54. return new DOMStream.WriteStream(
  55. el
  56. , DOMStream.WriteStream.APPEND
  57. , mimetype
  58. )
  59. }
  60. DOMStream.createWriteStream = function(el, mimetype) {
  61. return new DOMStream.WriteStream(
  62. el
  63. , DOMStream.WriteStream.WRITE
  64. , mimetype
  65. )
  66. }
  67. DOMStream.createReadStream =
  68. DOMStream.createEventStream = function(el, type, preventDefault) {
  69. preventDefault = preventDefault === undefined ? true : preventDefault
  70. return new DOMStream.ReadStream(
  71. el
  72. , type
  73. , preventDefault
  74. )
  75. }
  76. module.exports = DOMStream
  77. },{"./readable":4,"./writable":5}],4:[function(require,module,exports){
  78. module.exports = DOMStream
  79. var Stream = require('stream').Stream
  80. var listener = function(el, type, onmsg) {
  81. return el.addEventListener(type, onmsg, false)
  82. }
  83. if(typeof $ !== 'undefined')
  84. listener = function(el, type, onmsg) {
  85. return el = $(el)[type](onmsg)
  86. }
  87. if(typeof document !== 'undefined' && !document.createElement('div').addEventListener)
  88. listener = function(el, type, onmsg) {
  89. return el.attachEvent('on'+type, onmsg)
  90. }
  91. function DOMStream(el, eventType, shouldPreventDefault) {
  92. this.el = el
  93. this.eventType = eventType
  94. this.shouldPreventDefault = shouldPreventDefault
  95. var self = this
  96. if(el && this.eventType)
  97. listener(
  98. this.el
  99. , this.eventType
  100. , function() { return self.listen.apply(self, arguments) }
  101. )
  102. Stream.call(this)
  103. }
  104. var cons = DOMStream
  105. , proto = cons.prototype = Object.create(Stream.prototype)
  106. proto.constructor = cons
  107. proto.listen = function(ev) {
  108. if(this.shouldPreventDefault)
  109. ev.preventDefault ? ev.preventDefault() : (ev.returnValue = false)
  110. var collectData =
  111. this.eventType === 'submit' ||
  112. this.eventType === 'change' ||
  113. this.eventType === 'keydown' ||
  114. this.eventType === 'keyup' ||
  115. this.eventType === 'input'
  116. if(collectData) {
  117. if(this.el.tagName.toUpperCase() === 'FORM')
  118. return this.handleFormSubmit(ev)
  119. return this.emit('data', valueFromElement(this.el))
  120. }
  121. this.emit('data', ev)
  122. }
  123. proto.handleFormSubmit = function(ev) {
  124. var elements = []
  125. if(this.el.querySelectorAll) {
  126. elements = this.el.querySelectorAll('input,textarea,select')
  127. } else {
  128. var inputs = {'INPUT':true, 'TEXTAREA':true, 'SELECT':true}
  129. var recurse = function(el) {
  130. for(var i = 0, len = el.childNodes.length; i < len; ++i) {
  131. if(el.childNodes[i].tagName) {
  132. if(inputs[el.childNodes[i].tagName.toUpperCase()]) {
  133. elements.push(el)
  134. } else {
  135. recurse(el.childNodes[i])
  136. }
  137. }
  138. }
  139. }
  140. recurse(this.el)
  141. }
  142. var output = {}
  143. , attr
  144. , val
  145. for(var i = 0, len = elements.length; i < len; ++i) {
  146. attr = elements[i].getAttribute('name')
  147. val = valueFromElement(elements[i])
  148. if(val !== null) {
  149. output[attr] = val
  150. }
  151. }
  152. return this.emit('data', output)
  153. }
  154. function valueFromElement(el) {
  155. switch(el.getAttribute('type')) {
  156. case 'radio':
  157. return el.checked ? el.value : null
  158. case 'checkbox':
  159. return 'data', el.checked
  160. }
  161. return el.value
  162. }
  163. },{"stream":63}],5:[function(require,module,exports){
  164. module.exports = DOMStream
  165. var Stream = require('stream').Stream
  166. function DOMStream(el, mode, mimetype) {
  167. this.el = el
  168. this.mode = mode
  169. this.mimetype = mimetype || 'text/html'
  170. Stream.call(this)
  171. }
  172. var cons = DOMStream
  173. , proto = cons.prototype = Object.create(Stream.prototype)
  174. proto.constructor = cons
  175. cons.APPEND = 0
  176. cons.WRITE = 1
  177. proto.writable = true
  178. proto.setMimetype = function(mime) {
  179. this.mimetype = mime
  180. }
  181. proto.write = function(data) {
  182. var result = (this.mode === cons.APPEND) ? this.append(data) : this.insert(data)
  183. this.emit('data', this.el.childNodes)
  184. return result
  185. }
  186. proto.end = function() {
  187. }
  188. proto.insert = function(data) {
  189. this.el.innerHTML = ''
  190. return this.append(data)
  191. }
  192. proto.append = function(data) {
  193. var result = this[this.resolveMimetypeHandler()](data)
  194. for(var i = 0, len = result.length; i < len; ++i) {
  195. this.el.appendChild(result[i])
  196. }
  197. return true
  198. }
  199. proto.resolveMimetypeHandler = function() {
  200. var type = this.mimetype.replace(/(\/\w)/, function(x) {
  201. return x.slice(1).toUpperCase()
  202. })
  203. type = type.charAt(0).toUpperCase() + type.slice(1)
  204. return 'construct'+type
  205. }
  206. proto.constructTextHtml = function(data) {
  207. var isTableFragment = /(tr|td|th)/.test(data) && !/table/.test(data)
  208. , div
  209. if(isTableFragment) {
  210. // wuh-oh.
  211. div = document.createElement('table')
  212. }
  213. div = div || document.createElement('div')
  214. div.innerHTML = data
  215. return [].slice.call(div.childNodes)
  216. }
  217. proto.constructTextPlain = function(data) {
  218. var textNode = document.createTextNode(data)
  219. return [textNode]
  220. }
  221. },{"stream":63}],6:[function(require,module,exports){
  222. (function (process){
  223. // Copyright Joyent, Inc. and other Node contributors.
  224. //
  225. // Permission is hereby granted, free of charge, to any person obtaining a
  226. // copy of this software and associated documentation files (the
  227. // "Software"), to deal in the Software without restriction, including
  228. // without limitation the rights to use, copy, modify, merge, publish,
  229. // distribute, sublicense, and/or sell copies of the Software, and to permit
  230. // persons to whom the Software is furnished to do so, subject to the
  231. // following conditions:
  232. //
  233. // The above copyright notice and this permission notice shall be included
  234. // in all copies or substantial portions of the Software.
  235. //
  236. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  237. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  238. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  239. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  240. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  241. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  242. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  243. // a duplex stream is just a stream that is both readable and writable.
  244. // Since JS doesn't have multiple prototypal inheritance, this class
  245. // prototypally inherits from Readable, and then parasitically from
  246. // Writable.
  247. module.exports = Duplex;
  248. /*<replacement>*/
  249. var objectKeys = Object.keys || function (obj) {
  250. var keys = [];
  251. for (var key in obj) keys.push(key);
  252. return keys;
  253. }
  254. /*</replacement>*/
  255. /*<replacement>*/
  256. var util = require('core-util-is');
  257. util.inherits = require('inherits');
  258. /*</replacement>*/
  259. var Readable = require('./_stream_readable');
  260. var Writable = require('./_stream_writable');
  261. util.inherits(Duplex, Readable);
  262. forEach(objectKeys(Writable.prototype), function(method) {
  263. if (!Duplex.prototype[method])
  264. Duplex.prototype[method] = Writable.prototype[method];
  265. });
  266. function Duplex(options) {
  267. if (!(this instanceof Duplex))
  268. return new Duplex(options);
  269. Readable.call(this, options);
  270. Writable.call(this, options);
  271. if (options && options.readable === false)
  272. this.readable = false;
  273. if (options && options.writable === false)
  274. this.writable = false;
  275. this.allowHalfOpen = true;
  276. if (options && options.allowHalfOpen === false)
  277. this.allowHalfOpen = false;
  278. this.once('end', onend);
  279. }
  280. // the no-half-open enforcer
  281. function onend() {
  282. // if we allow half-open state, or if the writable side ended,
  283. // then we're ok.
  284. if (this.allowHalfOpen || this._writableState.ended)
  285. return;
  286. // no more data can be written.
  287. // But allow more writes to happen in this tick.
  288. process.nextTick(this.end.bind(this));
  289. }
  290. function forEach (xs, f) {
  291. for (var i = 0, l = xs.length; i < l; i++) {
  292. f(xs[i], i);
  293. }
  294. }
  295. }).call(this,require('_process'))
  296. },{"./_stream_readable":7,"./_stream_writable":9,"_process":50,"core-util-is":10,"inherits":11}],7:[function(require,module,exports){
  297. (function (process){
  298. // Copyright Joyent, Inc. and other Node contributors.
  299. //
  300. // Permission is hereby granted, free of charge, to any person obtaining a
  301. // copy of this software and associated documentation files (the
  302. // "Software"), to deal in the Software without restriction, including
  303. // without limitation the rights to use, copy, modify, merge, publish,
  304. // distribute, sublicense, and/or sell copies of the Software, and to permit
  305. // persons to whom the Software is furnished to do so, subject to the
  306. // following conditions:
  307. //
  308. // The above copyright notice and this permission notice shall be included
  309. // in all copies or substantial portions of the Software.
  310. //
  311. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  312. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  313. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  314. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  315. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  316. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  317. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  318. module.exports = Readable;
  319. /*<replacement>*/
  320. var isArray = require('isarray');
  321. /*</replacement>*/
  322. /*<replacement>*/
  323. var Buffer = require('buffer').Buffer;
  324. /*</replacement>*/
  325. Readable.ReadableState = ReadableState;
  326. var EE = require('events').EventEmitter;
  327. /*<replacement>*/
  328. if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
  329. return emitter.listeners(type).length;
  330. };
  331. /*</replacement>*/
  332. var Stream = require('stream');
  333. /*<replacement>*/
  334. var util = require('core-util-is');
  335. util.inherits = require('inherits');
  336. /*</replacement>*/
  337. var StringDecoder;
  338. util.inherits(Readable, Stream);
  339. function ReadableState(options, stream) {
  340. options = options || {};
  341. // the point at which it stops calling _read() to fill the buffer
  342. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  343. var hwm = options.highWaterMark;
  344. this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024;
  345. // cast to ints.
  346. this.highWaterMark = ~~this.highWaterMark;
  347. this.buffer = [];
  348. this.length = 0;
  349. this.pipes = null;
  350. this.pipesCount = 0;
  351. this.flowing = false;
  352. this.ended = false;
  353. this.endEmitted = false;
  354. this.reading = false;
  355. // In streams that never have any data, and do push(null) right away,
  356. // the consumer can miss the 'end' event if they do some I/O before
  357. // consuming the stream. So, we don't emit('end') until some reading
  358. // happens.
  359. this.calledRead = false;
  360. // a flag to be able to tell if the onwrite cb is called immediately,
  361. // or on a later tick. We set this to true at first, becuase any
  362. // actions that shouldn't happen until "later" should generally also
  363. // not happen before the first write call.
  364. this.sync = true;
  365. // whenever we return null, then we set a flag to say
  366. // that we're awaiting a 'readable' event emission.
  367. this.needReadable = false;
  368. this.emittedReadable = false;
  369. this.readableListening = false;
  370. // object stream flag. Used to make read(n) ignore n and to
  371. // make all the buffer merging and length checks go away
  372. this.objectMode = !!options.objectMode;
  373. // Crypto is kind of old and crusty. Historically, its default string
  374. // encoding is 'binary' so we have to make this configurable.
  375. // Everything else in the universe uses 'utf8', though.
  376. this.defaultEncoding = options.defaultEncoding || 'utf8';
  377. // when piping, we only care about 'readable' events that happen
  378. // after read()ing all the bytes and not getting any pushback.
  379. this.ranOut = false;
  380. // the number of writers that are awaiting a drain event in .pipe()s
  381. this.awaitDrain = 0;
  382. // if true, a maybeReadMore has been scheduled
  383. this.readingMore = false;
  384. this.decoder = null;
  385. this.encoding = null;
  386. if (options.encoding) {
  387. if (!StringDecoder)
  388. StringDecoder = require('string_decoder/').StringDecoder;
  389. this.decoder = new StringDecoder(options.encoding);
  390. this.encoding = options.encoding;
  391. }
  392. }
  393. function Readable(options) {
  394. if (!(this instanceof Readable))
  395. return new Readable(options);
  396. this._readableState = new ReadableState(options, this);
  397. // legacy
  398. this.readable = true;
  399. Stream.call(this);
  400. }
  401. // Manually shove something into the read() buffer.
  402. // This returns true if the highWaterMark has not been hit yet,
  403. // similar to how Writable.write() returns true if you should
  404. // write() some more.
  405. Readable.prototype.push = function(chunk, encoding) {
  406. var state = this._readableState;
  407. if (typeof chunk === 'string' && !state.objectMode) {
  408. encoding = encoding || state.defaultEncoding;
  409. if (encoding !== state.encoding) {
  410. chunk = new Buffer(chunk, encoding);
  411. encoding = '';
  412. }
  413. }
  414. return readableAddChunk(this, state, chunk, encoding, false);
  415. };
  416. // Unshift should *always* be something directly out of read()
  417. Readable.prototype.unshift = function(chunk) {
  418. var state = this._readableState;
  419. return readableAddChunk(this, state, chunk, '', true);
  420. };
  421. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  422. var er = chunkInvalid(state, chunk);
  423. if (er) {
  424. stream.emit('error', er);
  425. } else if (chunk === null || chunk === undefined) {
  426. state.reading = false;
  427. if (!state.ended)
  428. onEofChunk(stream, state);
  429. } else if (state.objectMode || chunk && chunk.length > 0) {
  430. if (state.ended && !addToFront) {
  431. var e = new Error('stream.push() after EOF');
  432. stream.emit('error', e);
  433. } else if (state.endEmitted && addToFront) {
  434. var e = new Error('stream.unshift() after end event');
  435. stream.emit('error', e);
  436. } else {
  437. if (state.decoder && !addToFront && !encoding)
  438. chunk = state.decoder.write(chunk);
  439. // update the buffer info.
  440. state.length += state.objectMode ? 1 : chunk.length;
  441. if (addToFront) {
  442. state.buffer.unshift(chunk);
  443. } else {
  444. state.reading = false;
  445. state.buffer.push(chunk);
  446. }
  447. if (state.needReadable)
  448. emitReadable(stream);
  449. maybeReadMore(stream, state);
  450. }
  451. } else if (!addToFront) {
  452. state.reading = false;
  453. }
  454. return needMoreData(state);
  455. }
  456. // if it's past the high water mark, we can push in some more.
  457. // Also, if we have no data yet, we can stand some
  458. // more bytes. This is to work around cases where hwm=0,
  459. // such as the repl. Also, if the push() triggered a
  460. // readable event, and the user called read(largeNumber) such that
  461. // needReadable was set, then we ought to push more, so that another
  462. // 'readable' event will be triggered.
  463. function needMoreData(state) {
  464. return !state.ended &&
  465. (state.needReadable ||
  466. state.length < state.highWaterMark ||
  467. state.length === 0);
  468. }
  469. // backwards compatibility.
  470. Readable.prototype.setEncoding = function(enc) {
  471. if (!StringDecoder)
  472. StringDecoder = require('string_decoder/').StringDecoder;
  473. this._readableState.decoder = new StringDecoder(enc);
  474. this._readableState.encoding = enc;
  475. };
  476. // Don't raise the hwm > 128MB
  477. var MAX_HWM = 0x800000;
  478. function roundUpToNextPowerOf2(n) {
  479. if (n >= MAX_HWM) {
  480. n = MAX_HWM;
  481. } else {
  482. // Get the next highest power of 2
  483. n--;
  484. for (var p = 1; p < 32; p <<= 1) n |= n >> p;
  485. n++;
  486. }
  487. return n;
  488. }
  489. function howMuchToRead(n, state) {
  490. if (state.length === 0 && state.ended)
  491. return 0;
  492. if (state.objectMode)
  493. return n === 0 ? 0 : 1;
  494. if (n === null || isNaN(n)) {
  495. // only flow one buffer at a time
  496. if (state.flowing && state.buffer.length)
  497. return state.buffer[0].length;
  498. else
  499. return state.length;
  500. }
  501. if (n <= 0)
  502. return 0;
  503. // If we're asking for more than the target buffer level,
  504. // then raise the water mark. Bump up to the next highest
  505. // power of 2, to prevent increasing it excessively in tiny
  506. // amounts.
  507. if (n > state.highWaterMark)
  508. state.highWaterMark = roundUpToNextPowerOf2(n);
  509. // don't have that much. return null, unless we've ended.
  510. if (n > state.length) {
  511. if (!state.ended) {
  512. state.needReadable = true;
  513. return 0;
  514. } else
  515. return state.length;
  516. }
  517. return n;
  518. }
  519. // you can override either this method, or the async _read(n) below.
  520. Readable.prototype.read = function(n) {
  521. var state = this._readableState;
  522. state.calledRead = true;
  523. var nOrig = n;
  524. var ret;
  525. if (typeof n !== 'number' || n > 0)
  526. state.emittedReadable = false;
  527. // if we're doing read(0) to trigger a readable event, but we
  528. // already have a bunch of data in the buffer, then just trigger
  529. // the 'readable' event and move on.
  530. if (n === 0 &&
  531. state.needReadable &&
  532. (state.length >= state.highWaterMark || state.ended)) {
  533. emitReadable(this);
  534. return null;
  535. }
  536. n = howMuchToRead(n, state);
  537. // if we've ended, and we're now clear, then finish it up.
  538. if (n === 0 && state.ended) {
  539. ret = null;
  540. // In cases where the decoder did not receive enough data
  541. // to produce a full chunk, then immediately received an
  542. // EOF, state.buffer will contain [<Buffer >, <Buffer 00 ...>].
  543. // howMuchToRead will see this and coerce the amount to
  544. // read to zero (because it's looking at the length of the
  545. // first <Buffer > in state.buffer), and we'll end up here.
  546. //
  547. // This can only happen via state.decoder -- no other venue
  548. // exists for pushing a zero-length chunk into state.buffer
  549. // and triggering this behavior. In this case, we return our
  550. // remaining data and end the stream, if appropriate.
  551. if (state.length > 0 && state.decoder) {
  552. ret = fromList(n, state);
  553. state.length -= ret.length;
  554. }
  555. if (state.length === 0)
  556. endReadable(this);
  557. return ret;
  558. }
  559. // All the actual chunk generation logic needs to be
  560. // *below* the call to _read. The reason is that in certain
  561. // synthetic stream cases, such as passthrough streams, _read
  562. // may be a completely synchronous operation which may change
  563. // the state of the read buffer, providing enough data when
  564. // before there was *not* enough.
  565. //
  566. // So, the steps are:
  567. // 1. Figure out what the state of things will be after we do
  568. // a read from the buffer.
  569. //
  570. // 2. If that resulting state will trigger a _read, then call _read.
  571. // Note that this may be asynchronous, or synchronous. Yes, it is
  572. // deeply ugly to write APIs this way, but that still doesn't mean
  573. // that the Readable class should behave improperly, as streams are
  574. // designed to be sync/async agnostic.
  575. // Take note if the _read call is sync or async (ie, if the read call
  576. // has returned yet), so that we know whether or not it's safe to emit
  577. // 'readable' etc.
  578. //
  579. // 3. Actually pull the requested chunks out of the buffer and return.
  580. // if we need a readable event, then we need to do some reading.
  581. var doRead = state.needReadable;
  582. // if we currently have less than the highWaterMark, then also read some
  583. if (state.length - n <= state.highWaterMark)
  584. doRead = true;
  585. // however, if we've ended, then there's no point, and if we're already
  586. // reading, then it's unnecessary.
  587. if (state.ended || state.reading)
  588. doRead = false;
  589. if (doRead) {
  590. state.reading = true;
  591. state.sync = true;
  592. // if the length is currently zero, then we *need* a readable event.
  593. if (state.length === 0)
  594. state.needReadable = true;
  595. // call internal read method
  596. this._read(state.highWaterMark);
  597. state.sync = false;
  598. }
  599. // If _read called its callback synchronously, then `reading`
  600. // will be false, and we need to re-evaluate how much data we
  601. // can return to the user.
  602. if (doRead && !state.reading)
  603. n = howMuchToRead(nOrig, state);
  604. if (n > 0)
  605. ret = fromList(n, state);
  606. else
  607. ret = null;
  608. if (ret === null) {
  609. state.needReadable = true;
  610. n = 0;
  611. }
  612. state.length -= n;
  613. // If we have nothing in the buffer, then we want to know
  614. // as soon as we *do* get something into the buffer.
  615. if (state.length === 0 && !state.ended)
  616. state.needReadable = true;
  617. // If we happened to read() exactly the remaining amount in the
  618. // buffer, and the EOF has been seen at this point, then make sure
  619. // that we emit 'end' on the very next tick.
  620. if (state.ended && !state.endEmitted && state.length === 0)
  621. endReadable(this);
  622. return ret;
  623. };
  624. function chunkInvalid(state, chunk) {
  625. var er = null;
  626. if (!Buffer.isBuffer(chunk) &&
  627. 'string' !== typeof chunk &&
  628. chunk !== null &&
  629. chunk !== undefined &&
  630. !state.objectMode) {
  631. er = new TypeError('Invalid non-string/buffer chunk');
  632. }
  633. return er;
  634. }
  635. function onEofChunk(stream, state) {
  636. if (state.decoder && !state.ended) {
  637. var chunk = state.decoder.end();
  638. if (chunk && chunk.length) {
  639. state.buffer.push(chunk);
  640. state.length += state.objectMode ? 1 : chunk.length;
  641. }
  642. }
  643. state.ended = true;
  644. // if we've ended and we have some data left, then emit
  645. // 'readable' now to make sure it gets picked up.
  646. if (state.length > 0)
  647. emitReadable(stream);
  648. else
  649. endReadable(stream);
  650. }
  651. // Don't emit readable right away in sync mode, because this can trigger
  652. // another read() call => stack overflow. This way, it might trigger
  653. // a nextTick recursion warning, but that's not so bad.
  654. function emitReadable(stream) {
  655. var state = stream._readableState;
  656. state.needReadable = false;
  657. if (state.emittedReadable)
  658. return;
  659. state.emittedReadable = true;
  660. if (state.sync)
  661. process.nextTick(function() {
  662. emitReadable_(stream);
  663. });
  664. else
  665. emitReadable_(stream);
  666. }
  667. function emitReadable_(stream) {
  668. stream.emit('readable');
  669. }
  670. // at this point, the user has presumably seen the 'readable' event,
  671. // and called read() to consume some data. that may have triggered
  672. // in turn another _read(n) call, in which case reading = true if
  673. // it's in progress.
  674. // However, if we're not ended, or reading, and the length < hwm,
  675. // then go ahead and try to read some more preemptively.
  676. function maybeReadMore(stream, state) {
  677. if (!state.readingMore) {
  678. state.readingMore = true;
  679. process.nextTick(function() {
  680. maybeReadMore_(stream, state);
  681. });
  682. }
  683. }
  684. function maybeReadMore_(stream, state) {
  685. var len = state.length;
  686. while (!state.reading && !state.flowing && !state.ended &&
  687. state.length < state.highWaterMark) {
  688. stream.read(0);
  689. if (len === state.length)
  690. // didn't get any data, stop spinning.
  691. break;
  692. else
  693. len = state.length;
  694. }
  695. state.readingMore = false;
  696. }
  697. // abstract method. to be overridden in specific implementation classes.
  698. // call cb(er, data) where data is <= n in length.
  699. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  700. // arbitrary, and perhaps not very meaningful.
  701. Readable.prototype._read = function(n) {
  702. this.emit('error', new Error('not implemented'));
  703. };
  704. Readable.prototype.pipe = function(dest, pipeOpts) {
  705. var src = this;
  706. var state = this._readableState;
  707. switch (state.pipesCount) {
  708. case 0:
  709. state.pipes = dest;
  710. break;
  711. case 1:
  712. state.pipes = [state.pipes, dest];
  713. break;
  714. default:
  715. state.pipes.push(dest);
  716. break;
  717. }
  718. state.pipesCount += 1;
  719. var doEnd = (!pipeOpts || pipeOpts.end !== false) &&
  720. dest !== process.stdout &&
  721. dest !== process.stderr;
  722. var endFn = doEnd ? onend : cleanup;
  723. if (state.endEmitted)
  724. process.nextTick(endFn);
  725. else
  726. src.once('end', endFn);
  727. dest.on('unpipe', onunpipe);
  728. function onunpipe(readable) {
  729. if (readable !== src) return;
  730. cleanup();
  731. }
  732. function onend() {
  733. dest.end();
  734. }
  735. // when the dest drains, it reduces the awaitDrain counter
  736. // on the source. This would be more elegant with a .once()
  737. // handler in flow(), but adding and removing repeatedly is
  738. // too slow.
  739. var ondrain = pipeOnDrain(src);
  740. dest.on('drain', ondrain);
  741. function cleanup() {
  742. // cleanup event handlers once the pipe is broken
  743. dest.removeListener('close', onclose);
  744. dest.removeListener('finish', onfinish);
  745. dest.removeListener('drain', ondrain);
  746. dest.removeListener('error', onerror);
  747. dest.removeListener('unpipe', onunpipe);
  748. src.removeListener('end', onend);
  749. src.removeListener('end', cleanup);
  750. // if the reader is waiting for a drain event from this
  751. // specific writer, then it would cause it to never start
  752. // flowing again.
  753. // So, if this is awaiting a drain, then we just call it now.
  754. // If we don't know, then assume that we are waiting for one.
  755. if (!dest._writableState || dest._writableState.needDrain)
  756. ondrain();
  757. }
  758. // if the dest has an error, then stop piping into it.
  759. // however, don't suppress the throwing behavior for this.
  760. function onerror(er) {
  761. unpipe();
  762. dest.removeListener('error', onerror);
  763. if (EE.listenerCount(dest, 'error') === 0)
  764. dest.emit('error', er);
  765. }
  766. // This is a brutally ugly hack to make sure that our error handler
  767. // is attached before any userland ones. NEVER DO THIS.
  768. if (!dest._events || !dest._events.error)
  769. dest.on('error', onerror);
  770. else if (isArray(dest._events.error))
  771. dest._events.error.unshift(onerror);
  772. else
  773. dest._events.error = [onerror, dest._events.error];
  774. // Both close and finish should trigger unpipe, but only once.
  775. function onclose() {
  776. dest.removeListener('finish', onfinish);
  777. unpipe();
  778. }
  779. dest.once('close', onclose);
  780. function onfinish() {
  781. dest.removeListener('close', onclose);
  782. unpipe();
  783. }
  784. dest.once('finish', onfinish);
  785. function unpipe() {
  786. src.unpipe(dest);
  787. }
  788. // tell the dest that it's being piped to
  789. dest.emit('pipe', src);
  790. // start the flow if it hasn't been started already.
  791. if (!state.flowing) {
  792. // the handler that waits for readable events after all
  793. // the data gets sucked out in flow.
  794. // This would be easier to follow with a .once() handler
  795. // in flow(), but that is too slow.
  796. this.on('readable', pipeOnReadable);
  797. state.flowing = true;
  798. process.nextTick(function() {
  799. flow(src);
  800. });
  801. }
  802. return dest;
  803. };
  804. function pipeOnDrain(src) {
  805. return function() {
  806. var dest = this;
  807. var state = src._readableState;
  808. state.awaitDrain--;
  809. if (state.awaitDrain === 0)
  810. flow(src);
  811. };
  812. }
  813. function flow(src) {
  814. var state = src._readableState;
  815. var chunk;
  816. state.awaitDrain = 0;
  817. function write(dest, i, list) {
  818. var written = dest.write(chunk);
  819. if (false === written) {
  820. state.awaitDrain++;
  821. }
  822. }
  823. while (state.pipesCount && null !== (chunk = src.read())) {
  824. if (state.pipesCount === 1)
  825. write(state.pipes, 0, null);
  826. else
  827. forEach(state.pipes, write);
  828. src.emit('data', chunk);
  829. // if anyone needs a drain, then we have to wait for that.
  830. if (state.awaitDrain > 0)
  831. return;
  832. }
  833. // if every destination was unpiped, either before entering this
  834. // function, or in the while loop, then stop flowing.
  835. //
  836. // NB: This is a pretty rare edge case.
  837. if (state.pipesCount === 0) {
  838. state.flowing = false;
  839. // if there were data event listeners added, then switch to old mode.
  840. if (EE.listenerCount(src, 'data') > 0)
  841. emitDataEvents(src);
  842. return;
  843. }
  844. // at this point, no one needed a drain, so we just ran out of data
  845. // on the next readable event, start it over again.
  846. state.ranOut = true;
  847. }
  848. function pipeOnReadable() {
  849. if (this._readableState.ranOut) {
  850. this._readableState.ranOut = false;
  851. flow(this);
  852. }
  853. }
  854. Readable.prototype.unpipe = function(dest) {
  855. var state = this._readableState;
  856. // if we're not piping anywhere, then do nothing.
  857. if (state.pipesCount === 0)
  858. return this;
  859. // just one destination. most common case.
  860. if (state.pipesCount === 1) {
  861. // passed in one, but it's not the right one.
  862. if (dest && dest !== state.pipes)
  863. return this;
  864. if (!dest)
  865. dest = state.pipes;
  866. // got a match.
  867. state.pipes = null;
  868. state.pipesCount = 0;
  869. this.removeListener('readable', pipeOnReadable);
  870. state.flowing = false;
  871. if (dest)
  872. dest.emit('unpipe', this);
  873. return this;
  874. }
  875. // slow case. multiple pipe destinations.
  876. if (!dest) {
  877. // remove all.
  878. var dests = state.pipes;
  879. var len = state.pipesCount;
  880. state.pipes = null;
  881. state.pipesCount = 0;
  882. this.removeListener('readable', pipeOnReadable);
  883. state.flowing = false;
  884. for (var i = 0; i < len; i++)
  885. dests[i].emit('unpipe', this);
  886. return this;
  887. }
  888. // try to find the right one.
  889. var i = indexOf(state.pipes, dest);
  890. if (i === -1)
  891. return this;
  892. state.pipes.splice(i, 1);
  893. state.pipesCount -= 1;
  894. if (state.pipesCount === 1)
  895. state.pipes = state.pipes[0];
  896. dest.emit('unpipe', this);
  897. return this;
  898. };
  899. // set up data events if they are asked for
  900. // Ensure readable listeners eventually get something
  901. Readable.prototype.on = function(ev, fn) {
  902. var res = Stream.prototype.on.call(this, ev, fn);
  903. if (ev === 'data' && !this._readableState.flowing)
  904. emitDataEvents(this);
  905. if (ev === 'readable' && this.readable) {
  906. var state = this._readableState;
  907. if (!state.readableListening) {
  908. state.readableListening = true;
  909. state.emittedReadable = false;
  910. state.needReadable = true;
  911. if (!state.reading) {
  912. this.read(0);
  913. } else if (state.length) {
  914. emitReadable(this, state);
  915. }
  916. }
  917. }
  918. return res;
  919. };
  920. Readable.prototype.addListener = Readable.prototype.on;
  921. // pause() and resume() are remnants of the legacy readable stream API
  922. // If the user uses them, then switch into old mode.
  923. Readable.prototype.resume = function() {
  924. emitDataEvents(this);
  925. this.read(0);
  926. this.emit('resume');
  927. };
  928. Readable.prototype.pause = function() {
  929. emitDataEvents(this, true);
  930. this.emit('pause');
  931. };
  932. function emitDataEvents(stream, startPaused) {
  933. var state = stream._readableState;
  934. if (state.flowing) {
  935. // https://github.com/isaacs/readable-stream/issues/16
  936. throw new Error('Cannot switch to old mode now.');
  937. }
  938. var paused = startPaused || false;
  939. var readable = false;
  940. // convert to an old-style stream.
  941. stream.readable = true;
  942. stream.pipe = Stream.prototype.pipe;
  943. stream.on = stream.addListener = Stream.prototype.on;
  944. stream.on('readable', function() {
  945. readable = true;
  946. var c;
  947. while (!paused && (null !== (c = stream.read())))
  948. stream.emit('data', c);
  949. if (c === null) {
  950. readable = false;
  951. stream._readableState.needReadable = true;
  952. }
  953. });
  954. stream.pause = function() {
  955. paused = true;
  956. this.emit('pause');
  957. };
  958. stream.resume = function() {
  959. paused = false;
  960. if (readable)
  961. process.nextTick(function() {
  962. stream.emit('readable');
  963. });
  964. else
  965. this.read(0);
  966. this.emit('resume');
  967. };
  968. // now make it start, just in case it hadn't already.
  969. stream.emit('readable');
  970. }
  971. // wrap an old-style stream as the async data source.
  972. // This is *not* part of the readable stream interface.
  973. // It is an ugly unfortunate mess of history.
  974. Readable.prototype.wrap = function(stream) {
  975. var state = this._readableState;
  976. var paused = false;
  977. var self = this;
  978. stream.on('end', function() {
  979. if (state.decoder && !state.ended) {
  980. var chunk = state.decoder.end();
  981. if (chunk && chunk.length)
  982. self.push(chunk);
  983. }
  984. self.push(null);
  985. });
  986. stream.on('data', function(chunk) {
  987. if (state.decoder)
  988. chunk = state.decoder.write(chunk);
  989. // don't skip over falsy values in objectMode
  990. //if (state.objectMode && util.isNullOrUndefined(chunk))
  991. if (state.objectMode && (chunk === null || chunk === undefined))
  992. return;
  993. else if (!state.objectMode && (!chunk || !chunk.length))
  994. return;
  995. var ret = self.push(chunk);
  996. if (!ret) {
  997. paused = true;
  998. stream.pause();
  999. }
  1000. });
  1001. // proxy all the other methods.
  1002. // important when wrapping filters and duplexes.
  1003. for (var i in stream) {
  1004. if (typeof stream[i] === 'function' &&
  1005. typeof this[i] === 'undefined') {
  1006. this[i] = function(method) { return function() {
  1007. return stream[method].apply(stream, arguments);
  1008. }}(i);
  1009. }
  1010. }
  1011. // proxy certain important events.
  1012. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  1013. forEach(events, function(ev) {
  1014. stream.on(ev, self.emit.bind(self, ev));
  1015. });
  1016. // when we try to consume some more bytes, simply unpause the
  1017. // underlying stream.
  1018. self._read = function(n) {
  1019. if (paused) {
  1020. paused = false;
  1021. stream.resume();
  1022. }
  1023. };
  1024. return self;
  1025. };
  1026. // exposed for testing purposes only.
  1027. Readable._fromList = fromList;
  1028. // Pluck off n bytes from an array of buffers.
  1029. // Length is the combined lengths of all the buffers in the list.
  1030. function fromList(n, state) {
  1031. var list = state.buffer;
  1032. var length = state.length;
  1033. var stringMode = !!state.decoder;
  1034. var objectMode = !!state.objectMode;
  1035. var ret;
  1036. // nothing in the list, definitely empty.
  1037. if (list.length === 0)
  1038. return null;
  1039. if (length === 0)
  1040. ret = null;
  1041. else if (objectMode)
  1042. ret = list.shift();
  1043. else if (!n || n >= length) {
  1044. // read it all, truncate the array.
  1045. if (stringMode)
  1046. ret = list.join('');
  1047. else
  1048. ret = Buffer.concat(list, length);
  1049. list.length = 0;
  1050. } else {
  1051. // read just some of it.
  1052. if (n < list[0].length) {
  1053. // just take a part of the first list item.
  1054. // slice is the same for buffers and strings.
  1055. var buf = list[0];
  1056. ret = buf.slice(0, n);
  1057. list[0] = buf.slice(n);
  1058. } else if (n === list[0].length) {
  1059. // first list is a perfect match
  1060. ret = list.shift();
  1061. } else {
  1062. // complex case.
  1063. // we have enough to cover it, but it spans past the first buffer.
  1064. if (stringMode)
  1065. ret = '';
  1066. else
  1067. ret = new Buffer(n);
  1068. var c = 0;
  1069. for (var i = 0, l = list.length; i < l && c < n; i++) {
  1070. var buf = list[0];
  1071. var cpy = Math.min(n - c, buf.length);
  1072. if (stringMode)
  1073. ret += buf.slice(0, cpy);
  1074. else
  1075. buf.copy(ret, c, 0, cpy);
  1076. if (cpy < buf.length)
  1077. list[0] = buf.slice(cpy);
  1078. else
  1079. list.shift();
  1080. c += cpy;
  1081. }
  1082. }
  1083. }
  1084. return ret;
  1085. }
  1086. function endReadable(stream) {
  1087. var state = stream._readableState;
  1088. // If we get here before consuming all the bytes, then that is a
  1089. // bug in node. Should never happen.
  1090. if (state.length > 0)
  1091. throw new Error('endReadable called on non-empty stream');
  1092. if (!state.endEmitted && state.calledRead) {
  1093. state.ended = true;
  1094. process.nextTick(function() {
  1095. // Check that we didn't get one last unshift.
  1096. if (!state.endEmitted && state.length === 0) {
  1097. state.endEmitted = true;
  1098. stream.readable = false;
  1099. stream.emit('end');
  1100. }
  1101. });
  1102. }
  1103. }
  1104. function forEach (xs, f) {
  1105. for (var i = 0, l = xs.length; i < l; i++) {
  1106. f(xs[i], i);
  1107. }
  1108. }
  1109. function indexOf (xs, x) {
  1110. for (var i = 0, l = xs.length; i < l; i++) {
  1111. if (xs[i] === x) return i;
  1112. }
  1113. return -1;
  1114. }
  1115. }).call(this,require('_process'))
  1116. },{"_process":50,"buffer":44,"core-util-is":10,"events":47,"inherits":11,"isarray":12,"stream":63,"string_decoder/":13}],8:[function(require,module,exports){
  1117. // Copyright Joyent, Inc. and other Node contributors.
  1118. //
  1119. // Permission is hereby granted, free of charge, to any person obtaining a
  1120. // copy of this software and associated documentation files (the
  1121. // "Software"), to deal in the Software without restriction, including
  1122. // without limitation the rights to use, copy, modify, merge, publish,
  1123. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1124. // persons to whom the Software is furnished to do so, subject to the
  1125. // following conditions:
  1126. //
  1127. // The above copyright notice and this permission notice shall be included
  1128. // in all copies or substantial portions of the Software.
  1129. //
  1130. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1131. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1132. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1133. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1134. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1135. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1136. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1137. // a transform stream is a readable/writable stream where you do
  1138. // something with the data. Sometimes it's called a "filter",
  1139. // but that's not a great name for it, since that implies a thing where
  1140. // some bits pass through, and others are simply ignored. (That would
  1141. // be a valid example of a transform, of course.)
  1142. //
  1143. // While the output is causally related to the input, it's not a
  1144. // necessarily symmetric or synchronous transformation. For example,
  1145. // a zlib stream might take multiple plain-text writes(), and then
  1146. // emit a single compressed chunk some time in the future.
  1147. //
  1148. // Here's how this works:
  1149. //
  1150. // The Transform stream has all the aspects of the readable and writable
  1151. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  1152. // internally, and returns false if there's a lot of pending writes
  1153. // buffered up. When you call read(), that calls _read(n) until
  1154. // there's enough pending readable data buffered up.
  1155. //
  1156. // In a transform stream, the written data is placed in a buffer. When
  1157. // _read(n) is called, it transforms the queued up data, calling the
  1158. // buffered _write cb's as it consumes chunks. If consuming a single
  1159. // written chunk would result in multiple output chunks, then the first
  1160. // outputted bit calls the readcb, and subsequent chunks just go into
  1161. // the read buffer, and will cause it to emit 'readable' if necessary.
  1162. //
  1163. // This way, back-pressure is actually determined by the reading side,
  1164. // since _read has to be called to start processing a new chunk. However,
  1165. // a pathological inflate type of transform can cause excessive buffering
  1166. // here. For example, imagine a stream where every byte of input is
  1167. // interpreted as an integer from 0-255, and then results in that many
  1168. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  1169. // 1kb of data being output. In this case, you could write a very small
  1170. // amount of input, and end up with a very large amount of output. In
  1171. // such a pathological inflating mechanism, there'd be no way to tell
  1172. // the system to stop doing the transform. A single 4MB write could
  1173. // cause the system to run out of memory.
  1174. //
  1175. // However, even in such a pathological case, only a single written chunk
  1176. // would be consumed, and then the rest would wait (un-transformed) until
  1177. // the results of the previous transformed chunk were consumed.
  1178. module.exports = Transform;
  1179. var Duplex = require('./_stream_duplex');
  1180. /*<replacement>*/
  1181. var util = require('core-util-is');
  1182. util.inherits = require('inherits');
  1183. /*</replacement>*/
  1184. util.inherits(Transform, Duplex);
  1185. function TransformState(options, stream) {
  1186. this.afterTransform = function(er, data) {
  1187. return afterTransform(stream, er, data);
  1188. };
  1189. this.needTransform = false;
  1190. this.transforming = false;
  1191. this.writecb = null;
  1192. this.writechunk = null;
  1193. }
  1194. function afterTransform(stream, er, data) {
  1195. var ts = stream._transformState;
  1196. ts.transforming = false;
  1197. var cb = ts.writecb;
  1198. if (!cb)
  1199. return stream.emit('error', new Error('no writecb in Transform class'));
  1200. ts.writechunk = null;
  1201. ts.writecb = null;
  1202. if (data !== null && data !== undefined)
  1203. stream.push(data);
  1204. if (cb)
  1205. cb(er);
  1206. var rs = stream._readableState;
  1207. rs.reading = false;
  1208. if (rs.needReadable || rs.length < rs.highWaterMark) {
  1209. stream._read(rs.highWaterMark);
  1210. }
  1211. }
  1212. function Transform(options) {
  1213. if (!(this instanceof Transform))
  1214. return new Transform(options);
  1215. Duplex.call(this, options);
  1216. var ts = this._transformState = new TransformState(options, this);
  1217. // when the writable side finishes, then flush out anything remaining.
  1218. var stream = this;
  1219. // start out asking for a readable event once data is transformed.
  1220. this._readableState.needReadable = true;
  1221. // we have implemented the _read method, and done the other things
  1222. // that Readable wants before the first _read call, so unset the
  1223. // sync guard flag.
  1224. this._readableState.sync = false;
  1225. this.once('finish', function() {
  1226. if ('function' === typeof this._flush)
  1227. this._flush(function(er) {
  1228. done(stream, er);
  1229. });
  1230. else
  1231. done(stream);
  1232. });
  1233. }
  1234. Transform.prototype.push = function(chunk, encoding) {
  1235. this._transformState.needTransform = false;
  1236. return Duplex.prototype.push.call(this, chunk, encoding);
  1237. };
  1238. // This is the part where you do stuff!
  1239. // override this function in implementation classes.
  1240. // 'chunk' is an input chunk.
  1241. //
  1242. // Call `push(newChunk)` to pass along transformed output
  1243. // to the readable side. You may call 'push' zero or more times.
  1244. //
  1245. // Call `cb(err)` when you are done with this chunk. If you pass
  1246. // an error, then that'll put the hurt on the whole operation. If you
  1247. // never call cb(), then you'll never get another chunk.
  1248. Transform.prototype._transform = function(chunk, encoding, cb) {
  1249. throw new Error('not implemented');
  1250. };
  1251. Transform.prototype._write = function(chunk, encoding, cb) {
  1252. var ts = this._transformState;
  1253. ts.writecb = cb;
  1254. ts.writechunk = chunk;
  1255. ts.writeencoding = encoding;
  1256. if (!ts.transforming) {
  1257. var rs = this._readableState;
  1258. if (ts.needTransform ||
  1259. rs.needReadable ||
  1260. rs.length < rs.highWaterMark)
  1261. this._read(rs.highWaterMark);
  1262. }
  1263. };
  1264. // Doesn't matter what the args are here.
  1265. // _transform does all the work.
  1266. // That we got here means that the readable side wants more data.
  1267. Transform.prototype._read = function(n) {
  1268. var ts = this._transformState;
  1269. if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
  1270. ts.transforming = true;
  1271. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  1272. } else {
  1273. // mark that we need a transform, so that any data that comes in
  1274. // will get processed, now that we've asked for it.
  1275. ts.needTransform = true;
  1276. }
  1277. };
  1278. function done(stream, er) {
  1279. if (er)
  1280. return stream.emit('error', er);
  1281. // if there's nothing in the write buffer, then that means
  1282. // that nothing more will ever be provided
  1283. var ws = stream._writableState;
  1284. var rs = stream._readableState;
  1285. var ts = stream._transformState;
  1286. if (ws.length)
  1287. throw new Error('calling transform done when ws.length != 0');
  1288. if (ts.transforming)
  1289. throw new Error('calling transform done when still transforming');
  1290. return stream.push(null);
  1291. }
  1292. },{"./_stream_duplex":6,"core-util-is":10,"inherits":11}],9:[function(require,module,exports){
  1293. (function (process){
  1294. // Copyright Joyent, Inc. and other Node contributors.
  1295. //
  1296. // Permission is hereby granted, free of charge, to any person obtaining a
  1297. // copy of this software and associated documentation files (the
  1298. // "Software"), to deal in the Software without restriction, including
  1299. // without limitation the rights to use, copy, modify, merge, publish,
  1300. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1301. // persons to whom the Software is furnished to do so, subject to the
  1302. // following conditions:
  1303. //
  1304. // The above copyright notice and this permission notice shall be included
  1305. // in all copies or substantial portions of the Software.
  1306. //
  1307. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1308. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1309. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1310. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1311. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1312. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1313. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1314. // A bit simpler than readable streams.
  1315. // Implement an async ._write(chunk, cb), and it'll handle all
  1316. // the drain event emission and buffering.
  1317. module.exports = Writable;
  1318. /*<replacement>*/
  1319. var Buffer = require('buffer').Buffer;
  1320. /*</replacement>*/
  1321. Writable.WritableState = WritableState;
  1322. /*<replacement>*/
  1323. var util = require('core-util-is');
  1324. util.inherits = require('inherits');
  1325. /*</replacement>*/
  1326. var Stream = require('stream');
  1327. util.inherits(Writable, Stream);
  1328. function WriteReq(chunk, encoding, cb) {
  1329. this.chunk = chunk;
  1330. this.encoding = encoding;
  1331. this.callback = cb;
  1332. }
  1333. function WritableState(options, stream) {
  1334. options = options || {};
  1335. // the point at which write() starts returning false
  1336. // Note: 0 is a valid value, means that we always return false if
  1337. // the entire buffer is not flushed immediately on write()
  1338. var hwm = options.highWaterMark;
  1339. this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024;
  1340. // object stream flag to indicate whether or not this stream
  1341. // contains buffers or objects.
  1342. this.objectMode = !!options.objectMode;
  1343. // cast to ints.
  1344. this.highWaterMark = ~~this.highWaterMark;
  1345. this.needDrain = false;
  1346. // at the start of calling end()
  1347. this.ending = false;
  1348. // when end() has been called, and returned
  1349. this.ended = false;
  1350. // when 'finish' is emitted
  1351. this.finished = false;
  1352. // should we decode strings into buffers before passing to _write?
  1353. // this is here so that some node-core streams can optimize string
  1354. // handling at a lower level.
  1355. var noDecode = options.decodeStrings === false;
  1356. this.decodeStrings = !noDecode;
  1357. // Crypto is kind of old and crusty. Historically, its default string
  1358. // encoding is 'binary' so we have to make this configurable.
  1359. // Everything else in the universe uses 'utf8', though.
  1360. this.defaultEncoding = options.defaultEncoding || 'utf8';
  1361. // not an actual buffer we keep track of, but a measurement
  1362. // of how much we're waiting to get pushed to some underlying
  1363. // socket or file.
  1364. this.length = 0;
  1365. // a flag to see when we're in the middle of a write.
  1366. this.writing = false;
  1367. // a flag to be able to tell if the onwrite cb is called immediately,
  1368. // or on a later tick. We set this to true at first, becuase any
  1369. // actions that shouldn't happen until "later" should generally also
  1370. // not happen before the first write call.
  1371. this.sync = true;
  1372. // a flag to know if we're processing previously buffered items, which
  1373. // may call the _write() callback in the same tick, so that we don't
  1374. // end up in an overlapped onwrite situation.
  1375. this.bufferProcessing = false;
  1376. // the callback that's passed to _write(chunk,cb)
  1377. this.onwrite = function(er) {
  1378. onwrite(stream, er);
  1379. };
  1380. // the callback that the user supplies to write(chunk,encoding,cb)
  1381. this.writecb = null;
  1382. // the amount that is being written when _write is called.
  1383. this.writelen = 0;
  1384. this.buffer = [];
  1385. // True if the error was already emitted and should not be thrown again
  1386. this.errorEmitted = false;
  1387. }
  1388. function Writable(options) {
  1389. var Duplex = require('./_stream_duplex');
  1390. // Writable ctor is applied to Duplexes, though they're not
  1391. // instanceof Writable, they're instanceof Readable.
  1392. if (!(this instanceof Writable) && !(this instanceof Duplex))
  1393. return new Writable(options);
  1394. this._writableState = new WritableState(options, this);
  1395. // legacy.
  1396. this.writable = true;
  1397. Stream.call(this);
  1398. }
  1399. // Otherwise people can pipe Writable streams, which is just wrong.
  1400. Writable.prototype.pipe = function() {
  1401. this.emit('error', new Error('Cannot pipe. Not readable.'));
  1402. };
  1403. function writeAfterEnd(stream, state, cb) {
  1404. var er = new Error('write after end');
  1405. // TODO: defer error events consistently everywhere, not just the cb
  1406. stream.emit('error', er);
  1407. process.nextTick(function() {
  1408. cb(er);
  1409. });
  1410. }
  1411. // If we get something that is not a buffer, string, null, or undefined,
  1412. // and we're not in objectMode, then that's an error.
  1413. // Otherwise stream chunks are all considered to be of length=1, and the
  1414. // watermarks determine how many objects to keep in the buffer, rather than
  1415. // how many bytes or characters.
  1416. function validChunk(stream, state, chunk, cb) {
  1417. var valid = true;
  1418. if (!Buffer.isBuffer(chunk) &&
  1419. 'string' !== typeof chunk &&
  1420. chunk !== null &&
  1421. chunk !== undefined &&
  1422. !state.objectMode) {
  1423. var er = new TypeError('Invalid non-string/buffer chunk');
  1424. stream.emit('error', er);
  1425. process.nextTick(function() {
  1426. cb(er);
  1427. });
  1428. valid = false;
  1429. }
  1430. return valid;
  1431. }
  1432. Writable.prototype.write = function(chunk, encoding, cb) {
  1433. var state = this._writableState;
  1434. var ret = false;
  1435. if (typeof encoding === 'function') {
  1436. cb = encoding;
  1437. encoding = null;
  1438. }
  1439. if (Buffer.isBuffer(chunk))
  1440. encoding = 'buffer';
  1441. else if (!encoding)
  1442. encoding = state.defaultEncoding;
  1443. if (typeof cb !== 'function')
  1444. cb = function() {};
  1445. if (state.ended)
  1446. writeAfterEnd(this, state, cb);
  1447. else if (validChunk(this, state, chunk, cb))
  1448. ret = writeOrBuffer(this, state, chunk, encoding, cb);
  1449. return ret;
  1450. };
  1451. function decodeChunk(state, chunk, encoding) {
  1452. if (!state.objectMode &&
  1453. state.decodeStrings !== false &&
  1454. typeof chunk === 'string') {
  1455. chunk = new Buffer(chunk, encoding);
  1456. }
  1457. return chunk;
  1458. }
  1459. // if we're already writing something, then just put this
  1460. // in the queue, and wait our turn. Otherwise, call _write
  1461. // If we return false, then we need a drain event, so set that flag.
  1462. function writeOrBuffer(stream, state, chunk, encoding, cb) {
  1463. chunk = decodeChunk(state, chunk, encoding);
  1464. if (Buffer.isBuffer(chunk))
  1465. encoding = 'buffer';
  1466. var len = state.objectMode ? 1 : chunk.length;
  1467. state.length += len;
  1468. var ret = state.length < state.highWaterMark;
  1469. // we must ensure that previous needDrain will not be reset to false.
  1470. if (!ret)
  1471. state.needDrain = true;
  1472. if (state.writing)
  1473. state.buffer.push(new WriteReq(chunk, encoding, cb));
  1474. else
  1475. doWrite(stream, state, len, chunk, encoding, cb);
  1476. return ret;
  1477. }
  1478. function doWrite(stream, state, len, chunk, encoding, cb) {
  1479. state.writelen = len;
  1480. state.writecb = cb;
  1481. state.writing = true;
  1482. state.sync = true;
  1483. stream._write(chunk, encoding, state.onwrite);
  1484. state.sync = false;
  1485. }
  1486. function onwriteError(stream, state, sync, er, cb) {
  1487. if (sync)
  1488. process.nextTick(function() {
  1489. cb(er);
  1490. });
  1491. else
  1492. cb(er);
  1493. stream._writableState.errorEmitted = true;
  1494. stream.emit('error', er);
  1495. }
  1496. function onwriteStateUpdate(state) {
  1497. state.writing = false;
  1498. state.writecb = null;
  1499. state.length -= state.writelen;
  1500. state.writelen = 0;
  1501. }
  1502. function onwrite(stream, er) {
  1503. var state = stream._writableState;
  1504. var sync = state.sync;
  1505. var cb = state.writecb;
  1506. onwriteStateUpdate(state);
  1507. if (er)
  1508. onwriteError(stream, state, sync, er, cb);
  1509. else {
  1510. // Check if we're actually ready to finish, but don't emit yet
  1511. var finished = needFinish(stream, state);
  1512. if (!finished && !state.bufferProcessing && state.buffer.length)
  1513. clearBuffer(stream, state);
  1514. if (sync) {
  1515. process.nextTick(function() {
  1516. afterWrite(stream, state, finished, cb);
  1517. });
  1518. } else {
  1519. afterWrite(stream, state, finished, cb);
  1520. }
  1521. }
  1522. }
  1523. function afterWrite(stream, state, finished, cb) {
  1524. if (!finished)
  1525. onwriteDrain(stream, state);
  1526. cb();
  1527. if (finished)
  1528. finishMaybe(stream, state);
  1529. }
  1530. // Must force callback to be called on nextTick, so that we don't
  1531. // emit 'drain' before the write() consumer gets the 'false' return
  1532. // value, and has a chance to attach a 'drain' listener.
  1533. function onwriteDrain(stream, state) {
  1534. if (state.length === 0 && state.needDrain) {
  1535. state.needDrain = false;
  1536. stream.emit('drain');
  1537. }
  1538. }
  1539. // if there's something in the buffer waiting, then process it
  1540. function clearBuffer(stream, state) {
  1541. state.bufferProcessing = true;
  1542. for (var c = 0; c < state.buffer.length; c++) {
  1543. var entry = state.buffer[c];
  1544. var chunk = entry.chunk;
  1545. var encoding = entry.encoding;
  1546. var cb = entry.callback;
  1547. var len = state.objectMode ? 1 : chunk.length;
  1548. doWrite(stream, state, len, chunk, encoding, cb);
  1549. // if we didn't call the onwrite immediately, then
  1550. // it means that we need to wait until it does.
  1551. // also, that means that the chunk and cb are currently
  1552. // being processed, so move the buffer counter past them.
  1553. if (state.writing) {
  1554. c++;
  1555. break;
  1556. }
  1557. }
  1558. state.bufferProcessing = false;
  1559. if (c < state.buffer.length)
  1560. state.buffer = state.buffer.slice(c);
  1561. else
  1562. state.buffer.length = 0;
  1563. }
  1564. Writable.prototype._write = function(chunk, encoding, cb) {
  1565. cb(new Error('not implemented'));
  1566. };
  1567. Writable.prototype.end = function(chunk, encoding, cb) {
  1568. var state = this._writableState;
  1569. if (typeof chunk === 'function') {
  1570. cb = chunk;
  1571. chunk = null;
  1572. encoding = null;
  1573. } else if (typeof encoding === 'function') {
  1574. cb = encoding;
  1575. encoding = null;
  1576. }
  1577. if (typeof chunk !== 'undefined' && chunk !== null)
  1578. this.write(chunk, encoding);
  1579. // ignore unnecessary end() calls.
  1580. if (!state.ending && !state.finished)
  1581. endWritable(this, state, cb);
  1582. };
  1583. function needFinish(stream, state) {
  1584. return (state.ending &&
  1585. state.length === 0 &&
  1586. !state.finished &&
  1587. !state.writing);
  1588. }
  1589. function finishMaybe(stream, state) {
  1590. var need = needFinish(stream, state);
  1591. if (need) {
  1592. state.finished = true;
  1593. stream.emit('finish');
  1594. }
  1595. return need;
  1596. }
  1597. function endWritable(stream, state, cb) {
  1598. state.ending = true;
  1599. finishMaybe(stream, state);
  1600. if (cb) {
  1601. if (state.finished)
  1602. process.nextTick(cb);
  1603. else
  1604. stream.once('finish', cb);
  1605. }
  1606. state.ended = true;
  1607. }
  1608. }).call(this,require('_process'))
  1609. },{"./_stream_duplex":6,"_process":50,"buffer":44,"core-util-is":10,"inherits":11,"stream":63}],10:[function(require,module,exports){
  1610. (function (Buffer){
  1611. // Copyright Joyent, Inc. and other Node contributors.
  1612. //
  1613. // Permission is hereby granted, free of charge, to any person obtaining a
  1614. // copy of this software and associated documentation files (the
  1615. // "Software"), to deal in the Software without restriction, including
  1616. // without limitation the rights to use, copy, modify, merge, publish,
  1617. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1618. // persons to whom the Software is furnished to do so, subject to the
  1619. // following conditions:
  1620. //
  1621. // The above copyright notice and this permission notice shall be included
  1622. // in all copies or substantial portions of the Software.
  1623. //
  1624. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1625. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1626. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1627. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1628. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1629. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1630. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1631. // NOTE: These type checking functions intentionally don't use `instanceof`
  1632. // because it is fragile and can be easily faked with `Object.create()`.
  1633. function isArray(ar) {
  1634. return Array.isArray(ar);
  1635. }
  1636. exports.isArray = isArray;
  1637. function isBoolean(arg) {
  1638. return typeof arg === 'boolean';
  1639. }
  1640. exports.isBoolean = isBoolean;
  1641. function isNull(arg) {
  1642. return arg === null;
  1643. }
  1644. exports.isNull = isNull;
  1645. function isNullOrUndefined(arg) {
  1646. return arg == null;
  1647. }
  1648. exports.isNullOrUndefined = isNullOrUndefined;
  1649. function isNumber(arg) {
  1650. return typeof arg === 'number';
  1651. }
  1652. exports.isNumber = isNumber;
  1653. function isString(arg) {
  1654. return typeof arg === 'string';
  1655. }
  1656. exports.isString = isString;
  1657. function isSymbol(arg) {
  1658. return typeof arg === 'symbol';
  1659. }
  1660. exports.isSymbol = isSymbol;
  1661. function isUndefined(arg) {
  1662. return arg === void 0;
  1663. }
  1664. exports.isUndefined = isUndefined;
  1665. function isRegExp(re) {
  1666. return isObject(re) && objectToString(re) === '[object RegExp]';
  1667. }
  1668. exports.isRegExp = isRegExp;
  1669. function isObject(arg) {
  1670. return typeof arg === 'object' && arg !== null;
  1671. }
  1672. exports.isObject = isObject;
  1673. function isDate(d) {
  1674. return isObject(d) && objectToString(d) === '[object Date]';
  1675. }
  1676. exports.isDate = isDate;
  1677. function isError(e) {
  1678. return isObject(e) &&
  1679. (objectToString(e) === '[object Error]' || e instanceof Error);
  1680. }
  1681. exports.isError = isError;
  1682. function isFunction(arg) {
  1683. return typeof arg === 'function';
  1684. }
  1685. exports.isFunction = isFunction;
  1686. function isPrimitive(arg) {
  1687. return arg === null ||
  1688. typeof arg === 'boolean' ||
  1689. typeof arg === 'number' ||
  1690. typeof arg === 'string' ||
  1691. typeof arg === 'symbol' || // ES6 symbol
  1692. typeof arg === 'undefined';
  1693. }
  1694. exports.isPrimitive = isPrimitive;
  1695. function isBuffer(arg) {
  1696. return Buffer.isBuffer(arg);
  1697. }
  1698. exports.isBuffer = isBuffer;
  1699. function objectToString(o) {
  1700. return Object.prototype.toString.call(o);
  1701. }
  1702. }).call(this,require("buffer").Buffer)
  1703. },{"buffer":44}],11:[function(require,module,exports){
  1704. if (typeof Object.create === 'function') {
  1705. // implementation from standard node.js 'util' module
  1706. module.exports = function inherits(ctor, superCtor) {
  1707. ctor.super_ = superCtor
  1708. ctor.prototype = Object.create(superCtor.prototype, {
  1709. constructor: {
  1710. value: ctor,
  1711. enumerable: false,
  1712. writable: true,
  1713. configurable: true
  1714. }
  1715. });
  1716. };
  1717. } else {
  1718. // old school shim for old browsers
  1719. module.exports = function inherits(ctor, superCtor) {
  1720. ctor.super_ = superCtor
  1721. var TempCtor = function () {}
  1722. TempCtor.prototype = superCtor.prototype
  1723. ctor.prototype = new TempCtor()
  1724. ctor.prototype.constructor = ctor
  1725. }
  1726. }
  1727. },{}],12:[function(require,module,exports){
  1728. module.exports = Array.isArray || function (arr) {
  1729. return Object.prototype.toString.call(arr) == '[object Array]';
  1730. };
  1731. },{}],13:[function(require,module,exports){
  1732. // Copyright Joyent, Inc. and other Node contributors.
  1733. //
  1734. // Permission is hereby granted, free of charge, to any person obtaining a
  1735. // copy of this software and associated documentation files (the
  1736. // "Software"), to deal in the Software without restriction, including
  1737. // without limitation the rights to use, copy, modify, merge, publish,
  1738. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1739. // persons to whom the Software is furnished to do so, subject to the
  1740. // following conditions:
  1741. //
  1742. // The above copyright notice and this permission notice shall be included
  1743. // in all copies or substantial portions of the Software.
  1744. //
  1745. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1746. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1747. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1748. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1749. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1750. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1751. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1752. var Buffer = require('buffer').Buffer;
  1753. var isBufferEncoding = Buffer.isEncoding
  1754. || function(encoding) {
  1755. switch (encoding && encoding.toLowerCase()) {
  1756. case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': case 'raw': return true;
  1757. default: return false;
  1758. }
  1759. }
  1760. function assertEncoding(encoding) {
  1761. if (encoding && !isBufferEncoding(encoding)) {
  1762. throw new Error('Unknown encoding: ' + encoding);
  1763. }
  1764. }
  1765. // StringDecoder provides an interface for efficiently splitting a series of
  1766. // buffers into a series of JS strings without breaking apart multi-byte
  1767. // characters. CESU-8 is handled as part of the UTF-8 encoding.
  1768. //
  1769. // @TODO Handling all encodings inside a single object makes it very difficult
  1770. // to reason about this code, so it should be split up in the future.
  1771. // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
  1772. // points as used by CESU-8.
  1773. var StringDecoder = exports.StringDecoder = function(encoding) {
  1774. this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
  1775. assertEncoding(encoding);
  1776. switch (this.encoding) {
  1777. case 'utf8':
  1778. // CESU-8 represents each of Surrogate Pair by 3-bytes
  1779. this.surrogateSize = 3;
  1780. break;
  1781. case 'ucs2':
  1782. case 'utf16le':
  1783. // UTF-16 represents each of Surrogate Pair by 2-bytes
  1784. this.surrogateSize = 2;
  1785. this.detectIncompleteChar = utf16DetectIncompleteChar;
  1786. break;
  1787. case 'base64':
  1788. // Base-64 stores 3 bytes in 4 chars, and pads the remainder.
  1789. this.surrogateSize = 3;
  1790. this.detectIncompleteChar = base64DetectIncompleteChar;
  1791. break;
  1792. default:
  1793. this.write = passThroughWrite;
  1794. return;
  1795. }
  1796. // Enough space to store all bytes of a single character. UTF-8 needs 4
  1797. // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
  1798. this.charBuffer = new Buffer(6);
  1799. // Number of bytes received for the current incomplete multi-byte character.
  1800. this.charReceived = 0;
  1801. // Number of bytes expected for the current incomplete multi-byte character.
  1802. this.charLength = 0;
  1803. };
  1804. // write decodes the given buffer and returns it as JS string that is
  1805. // guaranteed to not contain any partial multi-byte characters. Any partial
  1806. // character found at the end of the buffer is buffered up, and will be
  1807. // returned when calling write again with the remaining bytes.
  1808. //
  1809. // Note: Converting a Buffer containing an orphan surrogate to a String
  1810. // currently works, but converting a String to a Buffer (via `new Buffer`, or
  1811. // Buffer#write) will replace incomplete surrogates with the unicode
  1812. // replacement character. See https://codereview.chromium.org/121173009/ .
  1813. StringDecoder.prototype.write = function(buffer) {
  1814. var charStr = '';
  1815. // if our last write ended with an incomplete multibyte character
  1816. while (this.charLength) {
  1817. // determine how many remaining bytes this buffer has to offer for this char
  1818. var available = (buffer.length >= this.charLength - this.charReceived) ?
  1819. this.charLength - this.charReceived :
  1820. buffer.length;
  1821. // add the new bytes to the char buffer
  1822. buffer.copy(this.charBuffer, this.charReceived, 0, available);
  1823. this.charReceived += available;
  1824. if (this.charReceived < this.charLength) {
  1825. // still not enough chars in this buffer? wait for more ...
  1826. return '';
  1827. }
  1828. // remove bytes belonging to the current character from the buffer
  1829. buffer = buffer.slice(available, buffer.length);
  1830. // get the character that was split
  1831. charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
  1832. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  1833. var charCode = charStr.charCodeAt(charStr.length - 1);
  1834. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  1835. this.charLength += this.surrogateSize;
  1836. charStr = '';
  1837. continue;
  1838. }
  1839. this.charReceived = this.charLength = 0;
  1840. // if there are no more bytes in this buffer, just emit our char
  1841. if (buffer.length === 0) {
  1842. return charStr;
  1843. }
  1844. break;
  1845. }
  1846. // determine and set charLength / charReceived
  1847. this.detectIncompleteChar(buffer);
  1848. var end = buffer.length;
  1849. if (this.charLength) {
  1850. // buffer the incomplete character bytes we got
  1851. buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
  1852. end -= this.charReceived;
  1853. }
  1854. charStr += buffer.toString(this.encoding, 0, end);
  1855. var end = charStr.length - 1;
  1856. var charCode = charStr.charCodeAt(end);
  1857. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  1858. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  1859. var size = this.surrogateSize;
  1860. this.charLength += size;
  1861. this.charReceived += size;
  1862. this.charBuffer.copy(this.charBuffer, size, 0, size);
  1863. buffer.copy(this.charBuffer, 0, 0, size);
  1864. return charStr.substring(0, end);
  1865. }
  1866. // or just emit the charStr
  1867. return charStr;
  1868. };
  1869. // detectIncompleteChar determines if there is an incomplete UTF-8 character at
  1870. // the end of the given buffer. If so, it sets this.charLength to the byte
  1871. // length that character, and sets this.charReceived to the number of bytes
  1872. // that are available for this character.
  1873. StringDecoder.prototype.detectIncompleteChar = function(buffer) {
  1874. // determine how many bytes we have to check at the end of this buffer
  1875. var i = (buffer.length >= 3) ? 3 : buffer.length;
  1876. // Figure out if one of the last i bytes of our buffer announces an
  1877. // incomplete char.
  1878. for (; i > 0; i--) {
  1879. var c = buffer[buffer.length - i];
  1880. // See http://en.wikipedia.org/wiki/UTF-8#Description
  1881. // 110XXXXX
  1882. if (i == 1 && c >> 5 == 0x06) {
  1883. this.charLength = 2;
  1884. break;
  1885. }
  1886. // 1110XXXX
  1887. if (i <= 2 && c >> 4 == 0x0E) {
  1888. this.charLength = 3;
  1889. break;
  1890. }
  1891. // 11110XXX
  1892. if (i <= 3 && c >> 3 == 0x1E) {
  1893. this.charLength = 4;
  1894. break;
  1895. }
  1896. }
  1897. this.charReceived = i;
  1898. };
  1899. StringDecoder.prototype.end = function(buffer) {
  1900. var res = '';
  1901. if (buffer && buffer.length)
  1902. res = this.write(buffer);
  1903. if (this.charReceived) {
  1904. var cr = this.charReceived;
  1905. var buf = this.charBuffer;
  1906. var enc = this.encoding;
  1907. res += buf.slice(0, cr).toString(enc);
  1908. }
  1909. return res;
  1910. };
  1911. function passThroughWrite(buffer) {
  1912. return buffer.toString(this.encoding);
  1913. }
  1914. function utf16DetectIncompleteChar(buffer) {
  1915. this.charReceived = buffer.length % 2;
  1916. this.charLength = this.charReceived ? 2 : 0;
  1917. }
  1918. function base64DetectIncompleteChar(buffer) {
  1919. this.charReceived = buffer.length % 3;
  1920. this.charLength = this.charReceived ? 3 : 0;
  1921. }
  1922. },{"buffer":44}],14:[function(require,module,exports){
  1923. module.exports = require("./lib/_stream_transform.js")
  1924. },{"./lib/_stream_transform.js":8}],15:[function(require,module,exports){
  1925. module.exports = extend
  1926. function extend() {
  1927. var target = {}
  1928. for (var i = 0; i < arguments.length; i++) {
  1929. var source = arguments[i]
  1930. for (var key in source) {
  1931. if (source.hasOwnProperty(key)) {
  1932. target[key] = source[key]
  1933. }
  1934. }
  1935. }
  1936. return target
  1937. }
  1938. },{}],16:[function(require,module,exports){
  1939. (function (process){
  1940. var Transform = require('readable-stream/transform')
  1941. , inherits = require('util').inherits
  1942. , xtend = require('xtend')
  1943. function DestroyableTransform(opts) {
  1944. Transform.call(this, opts)
  1945. this._destroyed = false
  1946. }
  1947. inherits(DestroyableTransform, Transform)
  1948. DestroyableTransform.prototype.destroy = function(err) {
  1949. if (this._destroyed) return
  1950. this._destroyed = true
  1951. var self = this
  1952. process.nextTick(function() {
  1953. if (err)
  1954. self.emit('error', err)
  1955. self.emit('close')
  1956. })
  1957. }
  1958. // a noop _transform function
  1959. function noop (chunk, enc, callback) {
  1960. callback(null, chunk)
  1961. }
  1962. // create a new export function, used by both the main export and
  1963. // the .ctor export, contains common logic for dealing with arguments
  1964. function through2 (construct) {
  1965. return function (options, transform, flush) {
  1966. if (typeof options == 'function') {
  1967. flush = transform
  1968. transform = options
  1969. options = {}
  1970. }
  1971. if (typeof transform != 'function')
  1972. transform = noop
  1973. if (typeof flush != 'function')
  1974. flush = null
  1975. return construct(options, transform, flush)
  1976. }
  1977. }
  1978. // main export, just make me a transform stream!
  1979. module.exports = through2(function (options, transform, flush) {
  1980. var t2 = new DestroyableTransform(options)
  1981. t2._transform = transform
  1982. if (flush)
  1983. t2._flush = flush
  1984. return t2
  1985. })
  1986. // make me a reusable prototype that I can `new`, or implicitly `new`
  1987. // with a constructor call
  1988. module.exports.ctor = through2(function (options, transform, flush) {
  1989. function Through2 (override) {
  1990. if (!(this instanceof Through2))
  1991. return new Through2(override)
  1992. this.options = xtend(options, override)
  1993. DestroyableTransform.call(this, this.options)
  1994. }
  1995. inherits(Through2, DestroyableTransform)
  1996. Through2.prototype._transform = transform
  1997. if (flush)
  1998. Through2.prototype._flush = flush
  1999. return Through2
  2000. })
  2001. module.exports.obj = through2(function (options, transform, flush) {
  2002. var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
  2003. t2._transform = transform
  2004. if (flush)
  2005. t2._flush = flush
  2006. return t2
  2007. })
  2008. }).call(this,require('_process'))
  2009. },{"_process":50,"readable-stream/transform":14,"util":65,"xtend":15}],17:[function(require,module,exports){
  2010. (function (Buffer){
  2011. var through = require('through2')
  2012. var duplexify = require('duplexify')
  2013. var WS = require('ws')
  2014. module.exports = WebSocketStream
  2015. function WebSocketStream(target, protocols) {
  2016. var stream, socket
  2017. var proxy = through(socketWrite, socketEnd)
  2018. // use existing WebSocket object that was passed in
  2019. if (typeof target === 'object') {
  2020. socket = target
  2021. // otherwise make a new one
  2022. } else {
  2023. socket = new WS(target, protocols)
  2024. socket.binaryType = 'arraybuffer'
  2025. }
  2026. // was already open when passed in
  2027. if (socket.readyState === 1) {
  2028. stream = proxy
  2029. } else {
  2030. stream = duplexify()
  2031. socket.addEventListener("open", onready)
  2032. }
  2033. stream.socket = socket
  2034. socket.addEventListener("close", onclose)
  2035. socket.addEventListener("error", onerror)
  2036. socket.addEventListener("message", onmessage)
  2037. function socketWrite(chunk, enc, next) {
  2038. socket.send(chunk)
  2039. next()
  2040. }
  2041. function socketEnd(done) {
  2042. socket.close()
  2043. done()
  2044. }
  2045. function onready() {
  2046. stream.setReadable(proxy)
  2047. stream.setWritable(proxy)
  2048. }
  2049. function onclose() {
  2050. stream.destroy()
  2051. }
  2052. function onerror(err) {
  2053. stream.destroy(err)
  2054. }
  2055. function onmessage(event) {
  2056. var data = event.data
  2057. if (data instanceof ArrayBuffer) data = new Buffer(new Uint8Array(data))
  2058. proxy.push(data)
  2059. }
  2060. return stream
  2061. }
  2062. }).call(this,require("buffer").Buffer)
  2063. },{"buffer":44,"duplexify":18,"through2":41,"ws":43}],18:[function(require,module,exports){
  2064. (function (process,Buffer){
  2065. var stream = require('readable-stream')
  2066. var eos = require('end-of-stream')
  2067. var util = require('util')
  2068. var SIGNAL_FLUSH = new Buffer([0])
  2069. var onuncork = function(self, fn) {
  2070. if (self._corked) self.once('uncork', fn)
  2071. else fn()
  2072. }
  2073. var destroyer = function(self, end) {
  2074. return function(err) {
  2075. if (err) self.destroy(err.message === 'premature close' ? null : err)
  2076. else if (end && !self._ended) self.end()
  2077. }
  2078. }
  2079. var end = function(ws, fn) {
  2080. if (!ws) return fn()
  2081. if (ws._writableState && ws._writableState.finished) return fn()
  2082. if (ws._writableState) return ws.end(fn)
  2083. ws.end()
  2084. fn()
  2085. }
  2086. var toStreams2 = function(rs) {
  2087. return new (stream.Readable)({objectMode:true, highWaterMark:16}).wrap(rs)
  2088. }
  2089. var Duplexify = function(writable, readable, opts) {
  2090. if (!(this instanceof Duplexify)) return new Duplexify(writable, readable, opts)
  2091. stream.Duplex.call(this, opts)
  2092. this._writable = null
  2093. this._readable = null
  2094. this._readable2 = null
  2095. this._forwardDestroy = !opts || opts.destroy !== false
  2096. this._corked = 1 // start corked
  2097. this._ondrain = null
  2098. this._drained = false
  2099. this._forwarding = false
  2100. this._unwrite = null
  2101. this._unread = null
  2102. this._ended = false
  2103. this.destroyed = false
  2104. if (writable) this.setWritable(writable)
  2105. if (readable) this.setReadable(readable)
  2106. }
  2107. util.inherits(Duplexify, stream.Duplex)
  2108. Duplexify.obj = function(writable, readable, opts) {
  2109. if (!opts) opts = {}
  2110. opts.objectMode = true
  2111. opts.highWaterMark = 16
  2112. return new Duplexify(writable, readable, opts)
  2113. }
  2114. Duplexify.prototype.cork = function() {
  2115. if (++this._corked === 1) this.emit('cork')
  2116. }
  2117. Duplexify.prototype.uncork = function() {
  2118. if (this._corked && --this._corked === 0) this.emit('uncork')
  2119. }
  2120. Duplexify.prototype.setWritable = function(writable) {
  2121. if (this._unwrite) this._unwrite()
  2122. if (this.destroyed) {
  2123. if (writable && writable.destroy) writable.destroy()
  2124. return
  2125. }
  2126. if (writable === null || writable === false) {
  2127. this.end()
  2128. return
  2129. }
  2130. var self = this
  2131. var unend = eos(writable, {writable:true, readable:false}, destroyer(this, true))
  2132. var ondrain = function() {
  2133. var ondrain = self._ondrain
  2134. self._ondrain = null
  2135. if (ondrain) ondrain()
  2136. }
  2137. var clear = function() {
  2138. self._writable.removeListener('drain', ondrain)
  2139. unend()
  2140. }
  2141. if (this._unwrite) process.nextTick(ondrain) // force a drain on stream reset to avoid livelocks
  2142. this._writable = writable
  2143. this._writable.on('drain', ondrain)
  2144. this._unwrite = clear
  2145. this.uncork() // always uncork setWritable
  2146. }
  2147. Duplexify.prototype.setReadable = function(readable) {
  2148. if (this._unread) this._unread()
  2149. if (this.destroyed) {
  2150. if (readable && readable.destroy) readable.destroy()
  2151. return
  2152. }
  2153. if (readable === null || readable === false) {
  2154. this.push(null)
  2155. this.resume()
  2156. return
  2157. }
  2158. var self = this
  2159. var unend = eos(readable, {writable:false, readable:true}, destroyer(this))
  2160. var onreadable = function() {
  2161. self._forward()
  2162. }
  2163. var onend = function() {
  2164. self.push(null)
  2165. }
  2166. var clear = function() {
  2167. self._readable2.removeListener('readable', onreadable)
  2168. self._readable2.removeListener('end', onend)
  2169. unend()
  2170. }
  2171. this._drained = true
  2172. this._readable = readable
  2173. this._readable2 = readable._readableState ? readable : toStreams2(readable)
  2174. this._readable2.on('readable', onreadable)
  2175. this._readable2.on('end', onend)
  2176. this._unread = clear
  2177. this._forward()
  2178. }
  2179. Duplexify.prototype._read = function() {
  2180. this._drained = true
  2181. this._forward()
  2182. }
  2183. Duplexify.prototype._forward = function() {
  2184. if (this._forwarding || !this._readable2 || !this._drained) return
  2185. this._forwarding = true
  2186. var data
  2187. while ((data = this._readable2.read()) !== null) {
  2188. this._drained = this.push(data)
  2189. }
  2190. this._forwarding = false
  2191. }
  2192. Duplexify.prototype.destroy = function(err) {
  2193. if (this.destroyed) return
  2194. this.destroyed = true
  2195. var self = this
  2196. process.nextTick(function() {
  2197. self._destroy(err)
  2198. })
  2199. }
  2200. Duplexify.prototype._destroy = function(err) {
  2201. if (err) {
  2202. var ondrain = this._ondrain
  2203. this._ondrain = null
  2204. if (ondrain) ondrain(err)
  2205. else this.emit('error', err)
  2206. }
  2207. if (this._forwardDestroy) {
  2208. if (this._readable && this._readable.destroy) this._readable.destroy()
  2209. if (this._writable && this._writable.destroy) this._writable.destroy()
  2210. }
  2211. this.emit('close')
  2212. }
  2213. Duplexify.prototype._write = function(data, enc, cb) {
  2214. if (this.destroyed) return cb()
  2215. if (this._corked) return onuncork(this, this._write.bind(this, data, enc, cb))
  2216. if (data === SIGNAL_FLUSH) return this._finish(cb)
  2217. if (!this._writable) return cb()
  2218. if (this._writable.write(data) === false) this._ondrain = cb
  2219. else cb()
  2220. }
  2221. Duplexify.prototype._finish = function(cb) {
  2222. var self = this
  2223. this.emit('preend')
  2224. onuncork(this, function() {
  2225. end(self._writable, function() {
  2226. self.emit('prefinish')
  2227. onuncork(self, cb)
  2228. })
  2229. })
  2230. }
  2231. Duplexify.prototype.end = function(data, enc, cb) {
  2232. if (typeof data === 'function') return this.end(null, null, data)
  2233. if (typeof enc === 'function') return this.end(data, null, enc)
  2234. this._ended = true
  2235. if (data) this.write(data)
  2236. if (!this._writableState.ending) this.write(SIGNAL_FLUSH)
  2237. return stream.Writable.prototype.end.call(this, cb)
  2238. }
  2239. module.exports = Duplexify
  2240. }).call(this,require('_process'),require("buffer").Buffer)
  2241. },{"_process":50,"buffer":44,"end-of-stream":19,"readable-stream":31,"util":65}],19:[function(require,module,exports){
  2242. var once = require('once');
  2243. var noop = function() {};
  2244. var isRequest = function(stream) {
  2245. return stream.setHeader && typeof stream.abort === 'function';
  2246. };
  2247. var eos = function(stream, opts, callback) {
  2248. if (typeof opts === 'function') return eos(stream, null, opts);
  2249. if (!opts) opts = {};
  2250. callback = once(callback || noop);
  2251. var ws = stream._writableState;
  2252. var rs = stream._readableState;
  2253. var readable = opts.readable || (opts.readable !== false && stream.readable);
  2254. var writable = opts.writable || (opts.writable !== false && stream.writable);
  2255. var onlegacyfinish = function() {
  2256. if (!stream.writable) onfinish();
  2257. };
  2258. var onfinish = function() {
  2259. writable = false;
  2260. if (!readable) callback();
  2261. };
  2262. var onend = function() {
  2263. readable = false;
  2264. if (!writable) callback();
  2265. };
  2266. var onclose = function() {
  2267. if (readable && !(rs && rs.ended)) return callback(new Error('premature close'));
  2268. if (writable && !(ws && ws.ended)) return callback(new Error('premature close'));
  2269. };
  2270. var onrequest = function() {
  2271. stream.req.on('finish', onfinish);
  2272. };
  2273. if (isRequest(stream)) {
  2274. stream.on('complete', onfinish);
  2275. stream.on('abort', onclose);
  2276. if (stream.req) onrequest();
  2277. else stream.on('request', onrequest);
  2278. } else if (writable && !ws) { // legacy streams
  2279. stream.on('end', onlegacyfinish);
  2280. stream.on('close', onlegacyfinish);
  2281. }
  2282. stream.on('end', onend);
  2283. stream.on('finish', onfinish);
  2284. if (opts.error !== false) stream.on('error', callback);
  2285. stream.on('close', onclose);
  2286. return function() {
  2287. stream.removeListener('complete', onfinish);
  2288. stream.removeListener('abort', onclose);
  2289. stream.removeListener('request', onrequest);
  2290. if (stream.req) stream.req.removeListener('finish', onfinish);
  2291. stream.removeListener('end', onlegacyfinish);
  2292. stream.removeListener('close', onlegacyfinish);
  2293. stream.removeListener('finish', onfinish);
  2294. stream.removeListener('end', onend);
  2295. stream.removeListener('error', callback);
  2296. stream.removeListener('close', onclose);
  2297. };
  2298. };
  2299. module.exports = eos;
  2300. },{"once":21}],20:[function(require,module,exports){
  2301. // Returns a wrapper function that returns a wrapped callback
  2302. // The wrapper function should do some stuff, and return a
  2303. // presumably different callback function.
  2304. // This makes sure that own properties are retained, so that
  2305. // decorations and such are not lost along the way.
  2306. module.exports = wrappy
  2307. function wrappy (fn, cb) {
  2308. if (fn && cb) return wrappy(fn)(cb)
  2309. if (typeof fn !== 'function')
  2310. throw new TypeError('need wrapper function')
  2311. Object.keys(fn).forEach(function (k) {
  2312. wrapper[k] = fn[k]
  2313. })
  2314. return wrapper
  2315. function wrapper() {
  2316. var args = new Array(arguments.length)
  2317. for (var i = 0; i < args.length; i++) {
  2318. args[i] = arguments[i]
  2319. }
  2320. var ret = fn.apply(this, args)
  2321. var cb = args[args.length-1]
  2322. if (typeof ret === 'function' && ret !== cb) {
  2323. Object.keys(cb).forEach(function (k) {
  2324. ret[k] = cb[k]
  2325. })
  2326. }
  2327. return ret
  2328. }
  2329. }
  2330. },{}],21:[function(require,module,exports){
  2331. var wrappy = require('wrappy')
  2332. module.exports = wrappy(once)
  2333. once.proto = once(function () {
  2334. Object.defineProperty(Function.prototype, 'once', {
  2335. value: function () {
  2336. return once(this)
  2337. },
  2338. configurable: true
  2339. })
  2340. })
  2341. function once (fn) {
  2342. var f = function () {
  2343. if (f.called) return f.value
  2344. f.called = true
  2345. return f.value = fn.apply(this, arguments)
  2346. }
  2347. f.called = false
  2348. return f
  2349. }
  2350. },{"wrappy":20}],22:[function(require,module,exports){
  2351. module.exports=require(6)
  2352. },{"./_stream_readable":24,"./_stream_writable":26,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_duplex.js":6,"_process":50,"core-util-is":27,"inherits":28}],23:[function(require,module,exports){
  2353. // Copyright Joyent, Inc. and other Node contributors.
  2354. //
  2355. // Permission is hereby granted, free of charge, to any person obtaining a
  2356. // copy of this software and associated documentation files (the
  2357. // "Software"), to deal in the Software without restriction, including
  2358. // without limitation the rights to use, copy, modify, merge, publish,
  2359. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2360. // persons to whom the Software is furnished to do so, subject to the
  2361. // following conditions:
  2362. //
  2363. // The above copyright notice and this permission notice shall be included
  2364. // in all copies or substantial portions of the Software.
  2365. //
  2366. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2367. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2368. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2369. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2370. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2371. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2372. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2373. // a passthrough stream.
  2374. // basically just the most minimal sort of Transform stream.
  2375. // Every written chunk gets output as-is.
  2376. module.exports = PassThrough;
  2377. var Transform = require('./_stream_transform');
  2378. /*<replacement>*/
  2379. var util = require('core-util-is');
  2380. util.inherits = require('inherits');
  2381. /*</replacement>*/
  2382. util.inherits(PassThrough, Transform);
  2383. function PassThrough(options) {
  2384. if (!(this instanceof PassThrough))
  2385. return new PassThrough(options);
  2386. Transform.call(this, options);
  2387. }
  2388. PassThrough.prototype._transform = function(chunk, encoding, cb) {
  2389. cb(null, chunk);
  2390. };
  2391. },{"./_stream_transform":25,"core-util-is":27,"inherits":28}],24:[function(require,module,exports){
  2392. module.exports=require(7)
  2393. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js":7,"_process":50,"buffer":44,"core-util-is":27,"events":47,"inherits":28,"isarray":29,"stream":63,"string_decoder/":30}],25:[function(require,module,exports){
  2394. module.exports=require(8)
  2395. },{"./_stream_duplex":22,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js":8,"core-util-is":27,"inherits":28}],26:[function(require,module,exports){
  2396. module.exports=require(9)
  2397. },{"./_stream_duplex":22,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js":9,"_process":50,"buffer":44,"core-util-is":27,"inherits":28,"stream":63}],27:[function(require,module,exports){
  2398. module.exports=require(10)
  2399. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/core-util-is/lib/util.js":10,"buffer":44}],28:[function(require,module,exports){
  2400. module.exports=require(11)
  2401. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/inherits/inherits_browser.js":11}],29:[function(require,module,exports){
  2402. module.exports=require(12)
  2403. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/isarray/index.js":12}],30:[function(require,module,exports){
  2404. module.exports=require(13)
  2405. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/string_decoder/index.js":13,"buffer":44}],31:[function(require,module,exports){
  2406. exports = module.exports = require('./lib/_stream_readable.js');
  2407. exports.Readable = exports;
  2408. exports.Writable = require('./lib/_stream_writable.js');
  2409. exports.Duplex = require('./lib/_stream_duplex.js');
  2410. exports.Transform = require('./lib/_stream_transform.js');
  2411. exports.PassThrough = require('./lib/_stream_passthrough.js');
  2412. },{"./lib/_stream_duplex.js":22,"./lib/_stream_passthrough.js":23,"./lib/_stream_readable.js":24,"./lib/_stream_transform.js":25,"./lib/_stream_writable.js":26}],32:[function(require,module,exports){
  2413. module.exports=require(6)
  2414. },{"./_stream_readable":33,"./_stream_writable":35,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_duplex.js":6,"_process":50,"core-util-is":36,"inherits":37}],33:[function(require,module,exports){
  2415. module.exports=require(7)
  2416. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js":7,"_process":50,"buffer":44,"core-util-is":36,"events":47,"inherits":37,"isarray":38,"stream":63,"string_decoder/":39}],34:[function(require,module,exports){
  2417. module.exports=require(8)
  2418. },{"./_stream_duplex":32,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js":8,"core-util-is":36,"inherits":37}],35:[function(require,module,exports){
  2419. module.exports=require(9)
  2420. },{"./_stream_duplex":32,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js":9,"_process":50,"buffer":44,"core-util-is":36,"inherits":37,"stream":63}],36:[function(require,module,exports){
  2421. module.exports=require(10)
  2422. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/core-util-is/lib/util.js":10,"buffer":44}],37:[function(require,module,exports){
  2423. module.exports=require(11)
  2424. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/inherits/inherits_browser.js":11}],38:[function(require,module,exports){
  2425. module.exports=require(12)
  2426. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/isarray/index.js":12}],39:[function(require,module,exports){
  2427. module.exports=require(13)
  2428. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/string_decoder/index.js":13,"buffer":44}],40:[function(require,module,exports){
  2429. module.exports=require(14)
  2430. },{"./lib/_stream_transform.js":34,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/transform.js":14}],41:[function(require,module,exports){
  2431. module.exports=require(16)
  2432. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/through2.js":16,"_process":50,"readable-stream/transform":40,"util":65,"xtend":42}],42:[function(require,module,exports){
  2433. module.exports=require(15)
  2434. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/xtend/immutable.js":15}],43:[function(require,module,exports){
  2435. /**
  2436. * Module dependencies.
  2437. */
  2438. var global = (function() { return this; })();
  2439. /**
  2440. * WebSocket constructor.
  2441. */
  2442. var WebSocket = global.WebSocket || global.MozWebSocket;
  2443. /**
  2444. * Module exports.
  2445. */
  2446. module.exports = WebSocket ? ws : null;
  2447. /**
  2448. * WebSocket constructor.
  2449. *
  2450. * The third `opts` options object gets ignored in web browsers, since it's
  2451. * non-standard, and throws a TypeError if passed to the constructor.
  2452. * See: https://github.com/einaros/ws/issues/227
  2453. *
  2454. * @param {String} uri
  2455. * @param {Array} protocols (optional)
  2456. * @param {Object) opts (optional)
  2457. * @api public
  2458. */
  2459. function ws(uri, protocols, opts) {
  2460. var instance;
  2461. if (protocols) {
  2462. instance = new WebSocket(uri, protocols);
  2463. } else {
  2464. instance = new WebSocket(uri);
  2465. }
  2466. return instance;
  2467. }
  2468. if (WebSocket) ws.prototype = WebSocket.prototype;
  2469. },{}],44:[function(require,module,exports){
  2470. /*!
  2471. * The buffer module from node.js, for the browser.
  2472. *
  2473. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  2474. * @license MIT
  2475. */
  2476. var base64 = require('base64-js')
  2477. var ieee754 = require('ieee754')
  2478. exports.Buffer = Buffer
  2479. exports.SlowBuffer = Buffer
  2480. exports.INSPECT_MAX_BYTES = 50
  2481. Buffer.poolSize = 8192
  2482. /**
  2483. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  2484. * === true Use Uint8Array implementation (fastest)
  2485. * === false Use Object implementation (most compatible, even IE6)
  2486. *
  2487. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  2488. * Opera 11.6+, iOS 4.2+.
  2489. *
  2490. * Note:
  2491. *
  2492. * - Implementation must support adding new properties to `Uint8Array` instances.
  2493. * Firefox 4-29 lacked support, fixed in Firefox 30+.
  2494. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  2495. *
  2496. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  2497. *
  2498. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  2499. * incorrect length in some situations.
  2500. *
  2501. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will
  2502. * get the Object implementation, which is slower but will work correctly.
  2503. */
  2504. Buffer.TYPED_ARRAY_SUPPORT = (function () {
  2505. try {
  2506. var buf = new ArrayBuffer(0)
  2507. var arr = new Uint8Array(buf)
  2508. arr.foo = function () { return 42 }
  2509. return 42 === arr.foo() && // typed array instances can be augmented
  2510. typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
  2511. new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
  2512. } catch (e) {
  2513. return false
  2514. }
  2515. })()
  2516. /**
  2517. * Class: Buffer
  2518. * =============
  2519. *
  2520. * The Buffer constructor returns instances of `Uint8Array` that are augmented
  2521. * with function properties for all the node `Buffer` API functions. We use
  2522. * `Uint8Array` so that square bracket notation works as expected -- it returns
  2523. * a single octet.
  2524. *
  2525. * By augmenting the instances, we can avoid modifying the `Uint8Array`
  2526. * prototype.
  2527. */
  2528. function Buffer (subject, encoding, noZero) {
  2529. if (!(this instanceof Buffer))
  2530. return new Buffer(subject, encoding, noZero)
  2531. var type = typeof subject
  2532. // Find the length
  2533. var length
  2534. if (type === 'number')
  2535. length = subject > 0 ? subject >>> 0 : 0
  2536. else if (type === 'string') {
  2537. if (encoding === 'base64')
  2538. subject = base64clean(subject)
  2539. length = Buffer.byteLength(subject, encoding)
  2540. } else if (type === 'object' && subject !== null) { // assume object is array-like
  2541. if (subject.type === 'Buffer' && isArray(subject.data))
  2542. subject = subject.data
  2543. length = +subject.length > 0 ? Math.floor(+subject.length) : 0
  2544. } else
  2545. throw new Error('First argument needs to be a number, array or string.')
  2546. var buf
  2547. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2548. // Preferred: Return an augmented `Uint8Array` instance for best performance
  2549. buf = Buffer._augment(new Uint8Array(length))
  2550. } else {
  2551. // Fallback: Return THIS instance of Buffer (created by `new`)
  2552. buf = this
  2553. buf.length = length
  2554. buf._isBuffer = true
  2555. }
  2556. var i
  2557. if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
  2558. // Speed optimization -- use set if we're copying from a typed array
  2559. buf._set(subject)
  2560. } else if (isArrayish(subject)) {
  2561. // Treat array-ish objects as a byte array
  2562. if (Buffer.isBuffer(subject)) {
  2563. for (i = 0; i < length; i++)
  2564. buf[i] = subject.readUInt8(i)
  2565. } else {
  2566. for (i = 0; i < length; i++)
  2567. buf[i] = ((subject[i] % 256) + 256) % 256
  2568. }
  2569. } else if (type === 'string') {
  2570. buf.write(subject, 0, encoding)
  2571. } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {
  2572. for (i = 0; i < length; i++) {
  2573. buf[i] = 0
  2574. }
  2575. }
  2576. return buf
  2577. }
  2578. // STATIC METHODS
  2579. // ==============
  2580. Buffer.isEncoding = function (encoding) {
  2581. switch (String(encoding).toLowerCase()) {
  2582. case 'hex':
  2583. case 'utf8':
  2584. case 'utf-8':
  2585. case 'ascii':
  2586. case 'binary':
  2587. case 'base64':
  2588. case 'raw':
  2589. case 'ucs2':
  2590. case 'ucs-2':
  2591. case 'utf16le':
  2592. case 'utf-16le':
  2593. return true
  2594. default:
  2595. return false
  2596. }
  2597. }
  2598. Buffer.isBuffer = function (b) {
  2599. return !!(b != null && b._isBuffer)
  2600. }
  2601. Buffer.byteLength = function (str, encoding) {
  2602. var ret
  2603. str = str.toString()
  2604. switch (encoding || 'utf8') {
  2605. case 'hex':
  2606. ret = str.length / 2
  2607. break
  2608. case 'utf8':
  2609. case 'utf-8':
  2610. ret = utf8ToBytes(str).length
  2611. break
  2612. case 'ascii':
  2613. case 'binary':
  2614. case 'raw':
  2615. ret = str.length
  2616. break
  2617. case 'base64':
  2618. ret = base64ToBytes(str).length
  2619. break
  2620. case 'ucs2':
  2621. case 'ucs-2':
  2622. case 'utf16le':
  2623. case 'utf-16le':
  2624. ret = str.length * 2
  2625. break
  2626. default:
  2627. throw new Error('Unknown encoding')
  2628. }
  2629. return ret
  2630. }
  2631. Buffer.concat = function (list, totalLength) {
  2632. assert(isArray(list), 'Usage: Buffer.concat(list[, length])')
  2633. if (list.length === 0) {
  2634. return new Buffer(0)
  2635. } else if (list.length === 1) {
  2636. return list[0]
  2637. }
  2638. var i
  2639. if (totalLength === undefined) {
  2640. totalLength = 0
  2641. for (i = 0; i < list.length; i++) {
  2642. totalLength += list[i].length
  2643. }
  2644. }
  2645. var buf = new Buffer(totalLength)
  2646. var pos = 0
  2647. for (i = 0; i < list.length; i++) {
  2648. var item = list[i]
  2649. item.copy(buf, pos)
  2650. pos += item.length
  2651. }
  2652. return buf
  2653. }
  2654. Buffer.compare = function (a, b) {
  2655. assert(Buffer.isBuffer(a) && Buffer.isBuffer(b), 'Arguments must be Buffers')
  2656. var x = a.length
  2657. var y = b.length
  2658. for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {}
  2659. if (i !== len) {
  2660. x = a[i]
  2661. y = b[i]
  2662. }
  2663. if (x < y) {
  2664. return -1
  2665. }
  2666. if (y < x) {
  2667. return 1
  2668. }
  2669. return 0
  2670. }
  2671. // BUFFER INSTANCE METHODS
  2672. // =======================
  2673. function hexWrite (buf, string, offset, length) {
  2674. offset = Number(offset) || 0
  2675. var remaining = buf.length - offset
  2676. if (!length) {
  2677. length = remaining
  2678. } else {
  2679. length = Number(length)
  2680. if (length > remaining) {
  2681. length = remaining
  2682. }
  2683. }
  2684. // must be an even number of digits
  2685. var strLen = string.length
  2686. assert(strLen % 2 === 0, 'Invalid hex string')
  2687. if (length > strLen / 2) {
  2688. length = strLen / 2
  2689. }
  2690. for (var i = 0; i < length; i++) {
  2691. var byte = parseInt(string.substr(i * 2, 2), 16)
  2692. assert(!isNaN(byte), 'Invalid hex string')
  2693. buf[offset + i] = byte
  2694. }
  2695. return i
  2696. }
  2697. function utf8Write (buf, string, offset, length) {
  2698. var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length)
  2699. return charsWritten
  2700. }
  2701. function asciiWrite (buf, string, offset, length) {
  2702. var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length)
  2703. return charsWritten
  2704. }
  2705. function binaryWrite (buf, string, offset, length) {
  2706. return asciiWrite(buf, string, offset, length)
  2707. }
  2708. function base64Write (buf, string, offset, length) {
  2709. var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)
  2710. return charsWritten
  2711. }
  2712. function utf16leWrite (buf, string, offset, length) {
  2713. var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length)
  2714. return charsWritten
  2715. }
  2716. Buffer.prototype.write = function (string, offset, length, encoding) {
  2717. // Support both (string, offset, length, encoding)
  2718. // and the legacy (string, encoding, offset, length)
  2719. if (isFinite(offset)) {
  2720. if (!isFinite(length)) {
  2721. encoding = length
  2722. length = undefined
  2723. }
  2724. } else { // legacy
  2725. var swap = encoding
  2726. encoding = offset
  2727. offset = length
  2728. length = swap
  2729. }
  2730. offset = Number(offset) || 0
  2731. var remaining = this.length - offset
  2732. if (!length) {
  2733. length = remaining
  2734. } else {
  2735. length = Number(length)
  2736. if (length > remaining) {
  2737. length = remaining
  2738. }
  2739. }
  2740. encoding = String(encoding || 'utf8').toLowerCase()
  2741. var ret
  2742. switch (encoding) {
  2743. case 'hex':
  2744. ret = hexWrite(this, string, offset, length)
  2745. break
  2746. case 'utf8':
  2747. case 'utf-8':
  2748. ret = utf8Write(this, string, offset, length)
  2749. break
  2750. case 'ascii':
  2751. ret = asciiWrite(this, string, offset, length)
  2752. break
  2753. case 'binary':
  2754. ret = binaryWrite(this, string, offset, length)
  2755. break
  2756. case 'base64':
  2757. ret = base64Write(this, string, offset, length)
  2758. break
  2759. case 'ucs2':
  2760. case 'ucs-2':
  2761. case 'utf16le':
  2762. case 'utf-16le':
  2763. ret = utf16leWrite(this, string, offset, length)
  2764. break
  2765. default:
  2766. throw new Error('Unknown encoding')
  2767. }
  2768. return ret
  2769. }
  2770. Buffer.prototype.toString = function (encoding, start, end) {
  2771. var self = this
  2772. encoding = String(encoding || 'utf8').toLowerCase()
  2773. start = Number(start) || 0
  2774. end = (end === undefined) ? self.length : Number(end)
  2775. // Fastpath empty strings
  2776. if (end === start)
  2777. return ''
  2778. var ret
  2779. switch (encoding) {
  2780. case 'hex':
  2781. ret = hexSlice(self, start, end)
  2782. break
  2783. case 'utf8':
  2784. case 'utf-8':
  2785. ret = utf8Slice(self, start, end)
  2786. break
  2787. case 'ascii':
  2788. ret = asciiSlice(self, start, end)
  2789. break
  2790. case 'binary':
  2791. ret = binarySlice(self, start, end)
  2792. break
  2793. case 'base64':
  2794. ret = base64Slice(self, start, end)
  2795. break
  2796. case 'ucs2':
  2797. case 'ucs-2':
  2798. case 'utf16le':
  2799. case 'utf-16le':
  2800. ret = utf16leSlice(self, start, end)
  2801. break
  2802. default:
  2803. throw new Error('Unknown encoding')
  2804. }
  2805. return ret
  2806. }
  2807. Buffer.prototype.toJSON = function () {
  2808. return {
  2809. type: 'Buffer',
  2810. data: Array.prototype.slice.call(this._arr || this, 0)
  2811. }
  2812. }
  2813. Buffer.prototype.equals = function (b) {
  2814. assert(Buffer.isBuffer(b), 'Argument must be a Buffer')
  2815. return Buffer.compare(this, b) === 0
  2816. }
  2817. Buffer.prototype.compare = function (b) {
  2818. assert(Buffer.isBuffer(b), 'Argument must be a Buffer')
  2819. return Buffer.compare(this, b)
  2820. }
  2821. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  2822. Buffer.prototype.copy = function (target, target_start, start, end) {
  2823. var source = this
  2824. if (!start) start = 0
  2825. if (!end && end !== 0) end = this.length
  2826. if (!target_start) target_start = 0
  2827. // Copy 0 bytes; we're done
  2828. if (end === start) return
  2829. if (target.length === 0 || source.length === 0) return
  2830. // Fatal error conditions
  2831. assert(end >= start, 'sourceEnd < sourceStart')
  2832. assert(target_start >= 0 && target_start < target.length,
  2833. 'targetStart out of bounds')
  2834. assert(start >= 0 && start < source.length, 'sourceStart out of bounds')
  2835. assert(end >= 0 && end <= source.length, 'sourceEnd out of bounds')
  2836. // Are we oob?
  2837. if (end > this.length)
  2838. end = this.length
  2839. if (target.length - target_start < end - start)
  2840. end = target.length - target_start + start
  2841. var len = end - start
  2842. if (len < 100 || !Buffer.TYPED_ARRAY_SUPPORT) {
  2843. for (var i = 0; i < len; i++) {
  2844. target[i + target_start] = this[i + start]
  2845. }
  2846. } else {
  2847. target._set(this.subarray(start, start + len), target_start)
  2848. }
  2849. }
  2850. function base64Slice (buf, start, end) {
  2851. if (start === 0 && end === buf.length) {
  2852. return base64.fromByteArray(buf)
  2853. } else {
  2854. return base64.fromByteArray(buf.slice(start, end))
  2855. }
  2856. }
  2857. function utf8Slice (buf, start, end) {
  2858. var res = ''
  2859. var tmp = ''
  2860. end = Math.min(buf.length, end)
  2861. for (var i = start; i < end; i++) {
  2862. if (buf[i] <= 0x7F) {
  2863. res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
  2864. tmp = ''
  2865. } else {
  2866. tmp += '%' + buf[i].toString(16)
  2867. }
  2868. }
  2869. return res + decodeUtf8Char(tmp)
  2870. }
  2871. function asciiSlice (buf, start, end) {
  2872. var ret = ''
  2873. end = Math.min(buf.length, end)
  2874. for (var i = start; i < end; i++) {
  2875. ret += String.fromCharCode(buf[i])
  2876. }
  2877. return ret
  2878. }
  2879. function binarySlice (buf, start, end) {
  2880. return asciiSlice(buf, start, end)
  2881. }
  2882. function hexSlice (buf, start, end) {
  2883. var len = buf.length
  2884. if (!start || start < 0) start = 0
  2885. if (!end || end < 0 || end > len) end = len
  2886. var out = ''
  2887. for (var i = start; i < end; i++) {
  2888. out += toHex(buf[i])
  2889. }
  2890. return out
  2891. }
  2892. function utf16leSlice (buf, start, end) {
  2893. var bytes = buf.slice(start, end)
  2894. var res = ''
  2895. for (var i = 0; i < bytes.length; i += 2) {
  2896. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
  2897. }
  2898. return res
  2899. }
  2900. Buffer.prototype.slice = function (start, end) {
  2901. var len = this.length
  2902. start = ~~start
  2903. end = end === undefined ? len : ~~end
  2904. if (start < 0) {
  2905. start += len;
  2906. if (start < 0)
  2907. start = 0
  2908. } else if (start > len) {
  2909. start = len
  2910. }
  2911. if (end < 0) {
  2912. end += len
  2913. if (end < 0)
  2914. end = 0
  2915. } else if (end > len) {
  2916. end = len
  2917. }
  2918. if (end < start)
  2919. end = start
  2920. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2921. return Buffer._augment(this.subarray(start, end))
  2922. } else {
  2923. var sliceLen = end - start
  2924. var newBuf = new Buffer(sliceLen, undefined, true)
  2925. for (var i = 0; i < sliceLen; i++) {
  2926. newBuf[i] = this[i + start]
  2927. }
  2928. return newBuf
  2929. }
  2930. }
  2931. // `get` will be removed in Node 0.13+
  2932. Buffer.prototype.get = function (offset) {
  2933. console.log('.get() is deprecated. Access using array indexes instead.')
  2934. return this.readUInt8(offset)
  2935. }
  2936. // `set` will be removed in Node 0.13+
  2937. Buffer.prototype.set = function (v, offset) {
  2938. console.log('.set() is deprecated. Access using array indexes instead.')
  2939. return this.writeUInt8(v, offset)
  2940. }
  2941. /*
  2942. * Need to make sure that buffer isn't trying to write out of bounds.
  2943. */
  2944. function checkOffset (offset, ext, length) {
  2945. if ((offset % 1) !== 0 || offset < 0)
  2946. throw new RangeError('offset is not uint')
  2947. if (offset + ext > length)
  2948. throw new RangeError('Trying to access beyond buffer length')
  2949. }
  2950. Buffer.prototype.readUInt8 = function (offset, noAssert) {
  2951. if (!noAssert)
  2952. checkOffset(offset, 1, this.length)
  2953. return this[offset]
  2954. }
  2955. Buffer.prototype.readUInt16LE = function (offset, noAssert) {
  2956. if (!noAssert)
  2957. checkOffset(offset, 2, this.length)
  2958. return this[offset] | (this[offset + 1] << 8)
  2959. }
  2960. Buffer.prototype.readUInt16BE = function (offset, noAssert) {
  2961. if (!noAssert)
  2962. checkOffset(offset, 2, this.length)
  2963. return (this[offset] << 8) | this[offset + 1]
  2964. }
  2965. Buffer.prototype.readUInt32LE = function (offset, noAssert) {
  2966. if (!noAssert)
  2967. checkOffset(offset, 4, this.length)
  2968. return ((this[offset]) |
  2969. (this[offset + 1] << 8) |
  2970. (this[offset + 2] << 16)) +
  2971. (this[offset + 3] * 0x1000000)
  2972. }
  2973. Buffer.prototype.readUInt32BE = function (offset, noAssert) {
  2974. if (!noAssert)
  2975. checkOffset(offset, 4, this.length)
  2976. return (this[offset] * 0x1000000) +
  2977. ((this[offset + 1] << 16) |
  2978. (this[offset + 2] << 8) |
  2979. this[offset + 3])
  2980. }
  2981. Buffer.prototype.readInt8 = function (offset, noAssert) {
  2982. if (!noAssert)
  2983. checkOffset(offset, 1, this.length)
  2984. if (!(this[offset] & 0x80))
  2985. return (this[offset])
  2986. return ((0xff - this[offset] + 1) * -1)
  2987. }
  2988. Buffer.prototype.readInt16LE = function (offset, noAssert) {
  2989. if (!noAssert)
  2990. checkOffset(offset, 2, this.length)
  2991. var val = this[offset] | (this[offset + 1] << 8)
  2992. return (val & 0x8000) ? val | 0xFFFF0000 : val
  2993. }
  2994. Buffer.prototype.readInt16BE = function (offset, noAssert) {
  2995. if (!noAssert)
  2996. checkOffset(offset, 2, this.length)
  2997. var val = this[offset + 1] | (this[offset] << 8)
  2998. return (val & 0x8000) ? val | 0xFFFF0000 : val
  2999. }
  3000. Buffer.prototype.readInt32LE = function (offset, noAssert) {
  3001. if (!noAssert)
  3002. checkOffset(offset, 4, this.length)
  3003. return (this[offset]) |
  3004. (this[offset + 1] << 8) |
  3005. (this[offset + 2] << 16) |
  3006. (this[offset + 3] << 24)
  3007. }
  3008. Buffer.prototype.readInt32BE = function (offset, noAssert) {
  3009. if (!noAssert)
  3010. checkOffset(offset, 4, this.length)
  3011. return (this[offset] << 24) |
  3012. (this[offset + 1] << 16) |
  3013. (this[offset + 2] << 8) |
  3014. (this[offset + 3])
  3015. }
  3016. Buffer.prototype.readFloatLE = function (offset, noAssert) {
  3017. if (!noAssert)
  3018. checkOffset(offset, 4, this.length)
  3019. return ieee754.read(this, offset, true, 23, 4)
  3020. }
  3021. Buffer.prototype.readFloatBE = function (offset, noAssert) {
  3022. if (!noAssert)
  3023. checkOffset(offset, 4, this.length)
  3024. return ieee754.read(this, offset, false, 23, 4)
  3025. }
  3026. Buffer.prototype.readDoubleLE = function (offset, noAssert) {
  3027. if (!noAssert)
  3028. checkOffset(offset, 8, this.length)
  3029. return ieee754.read(this, offset, true, 52, 8)
  3030. }
  3031. Buffer.prototype.readDoubleBE = function (offset, noAssert) {
  3032. if (!noAssert)
  3033. checkOffset(offset, 8, this.length)
  3034. return ieee754.read(this, offset, false, 52, 8)
  3035. }
  3036. function checkInt (buf, value, offset, ext, max, min) {
  3037. assert(Buffer.isBuffer(buf), 'buffer must be a Buffer instance')
  3038. assert(value <= max && value >= min, 'value is out of bounds')
  3039. assert(offset + ext <= buf.length, 'index out of range')
  3040. }
  3041. Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {
  3042. value = +value
  3043. offset = offset >>> 0
  3044. if (!noAssert)
  3045. checkInt(this, value, offset, 1, 0xff, 0)
  3046. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  3047. this[offset] = value
  3048. return offset + 1
  3049. }
  3050. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  3051. if (value < 0) value = 0xffff + value + 1
  3052. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
  3053. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  3054. (littleEndian ? i : 1 - i) * 8
  3055. }
  3056. }
  3057. Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) {
  3058. value = +value
  3059. offset = offset >>> 0
  3060. if (!noAssert)
  3061. checkInt(this, value, offset, 2, 0xffff, 0)
  3062. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3063. this[offset] = value
  3064. this[offset + 1] = (value >>> 8)
  3065. } else objectWriteUInt16(this, value, offset, true)
  3066. return offset + 2
  3067. }
  3068. Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) {
  3069. value = +value
  3070. offset = offset >>> 0
  3071. if (!noAssert)
  3072. checkInt(this, value, offset, 2, 0xffff, 0)
  3073. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3074. this[offset] = (value >>> 8)
  3075. this[offset + 1] = value
  3076. } else objectWriteUInt16(this, value, offset, false)
  3077. return offset + 2
  3078. }
  3079. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  3080. if (value < 0) value = 0xffffffff + value + 1
  3081. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
  3082. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
  3083. }
  3084. }
  3085. Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) {
  3086. value = +value
  3087. offset = offset >>> 0
  3088. if (!noAssert)
  3089. checkInt(this, value, offset, 4, 0xffffffff, 0)
  3090. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3091. this[offset + 3] = (value >>> 24)
  3092. this[offset + 2] = (value >>> 16)
  3093. this[offset + 1] = (value >>> 8)
  3094. this[offset] = value
  3095. } else objectWriteUInt32(this, value, offset, true)
  3096. return offset + 4
  3097. }
  3098. Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) {
  3099. value = +value
  3100. offset = offset >>> 0
  3101. if (!noAssert)
  3102. checkInt(this, value, offset, 4, 0xffffffff, 0)
  3103. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3104. this[offset] = (value >>> 24)
  3105. this[offset + 1] = (value >>> 16)
  3106. this[offset + 2] = (value >>> 8)
  3107. this[offset + 3] = value
  3108. } else objectWriteUInt32(this, value, offset, false)
  3109. return offset + 4
  3110. }
  3111. Buffer.prototype.writeInt8 = function (value, offset, noAssert) {
  3112. value = +value
  3113. offset = offset >>> 0
  3114. if (!noAssert)
  3115. checkInt(this, value, offset, 1, 0x7f, -0x80)
  3116. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  3117. if (value < 0) value = 0xff + value + 1
  3118. this[offset] = value
  3119. return offset + 1
  3120. }
  3121. Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {
  3122. value = +value
  3123. offset = offset >>> 0
  3124. if (!noAssert)
  3125. checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3126. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3127. this[offset] = value
  3128. this[offset + 1] = (value >>> 8)
  3129. } else objectWriteUInt16(this, value, offset, true)
  3130. return offset + 2
  3131. }
  3132. Buffer.prototype.writeInt16BE = function (value, offset, noAssert) {
  3133. value = +value
  3134. offset = offset >>> 0
  3135. if (!noAssert)
  3136. checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  3137. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3138. this[offset] = (value >>> 8)
  3139. this[offset + 1] = value
  3140. } else objectWriteUInt16(this, value, offset, false)
  3141. return offset + 2
  3142. }
  3143. Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
  3144. value = +value
  3145. offset = offset >>> 0
  3146. if (!noAssert)
  3147. checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3148. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3149. this[offset] = value
  3150. this[offset + 1] = (value >>> 8)
  3151. this[offset + 2] = (value >>> 16)
  3152. this[offset + 3] = (value >>> 24)
  3153. } else objectWriteUInt32(this, value, offset, true)
  3154. return offset + 4
  3155. }
  3156. Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {
  3157. value = +value
  3158. offset = offset >>> 0
  3159. if (!noAssert)
  3160. checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  3161. if (value < 0) value = 0xffffffff + value + 1
  3162. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3163. this[offset] = (value >>> 24)
  3164. this[offset + 1] = (value >>> 16)
  3165. this[offset + 2] = (value >>> 8)
  3166. this[offset + 3] = value
  3167. } else objectWriteUInt32(this, value, offset, false)
  3168. return offset + 4
  3169. }
  3170. function checkIEEE754 (buf, value, offset, ext, max, min) {
  3171. assert(value <= max && value >= min, 'value is out of bounds')
  3172. assert(offset + ext <= buf.length, 'index out of range')
  3173. }
  3174. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  3175. if (!noAssert)
  3176. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  3177. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  3178. return offset + 4
  3179. }
  3180. Buffer.prototype.writeFloatLE = function (value, offset, noAssert) {
  3181. return writeFloat(this, value, offset, true, noAssert)
  3182. }
  3183. Buffer.prototype.writeFloatBE = function (value, offset, noAssert) {
  3184. return writeFloat(this, value, offset, false, noAssert)
  3185. }
  3186. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  3187. if (!noAssert)
  3188. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  3189. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  3190. return offset + 8
  3191. }
  3192. Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) {
  3193. return writeDouble(this, value, offset, true, noAssert)
  3194. }
  3195. Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) {
  3196. return writeDouble(this, value, offset, false, noAssert)
  3197. }
  3198. // fill(value, start=0, end=buffer.length)
  3199. Buffer.prototype.fill = function (value, start, end) {
  3200. if (!value) value = 0
  3201. if (!start) start = 0
  3202. if (!end) end = this.length
  3203. assert(end >= start, 'end < start')
  3204. // Fill 0 bytes; we're done
  3205. if (end === start) return
  3206. if (this.length === 0) return
  3207. assert(start >= 0 && start < this.length, 'start out of bounds')
  3208. assert(end >= 0 && end <= this.length, 'end out of bounds')
  3209. var i
  3210. if (typeof value === 'number') {
  3211. for (i = start; i < end; i++) {
  3212. this[i] = value
  3213. }
  3214. } else {
  3215. var bytes = utf8ToBytes(value.toString())
  3216. var len = bytes.length
  3217. for (i = start; i < end; i++) {
  3218. this[i] = bytes[i % len]
  3219. }
  3220. }
  3221. return this
  3222. }
  3223. Buffer.prototype.inspect = function () {
  3224. var out = []
  3225. var len = this.length
  3226. for (var i = 0; i < len; i++) {
  3227. out[i] = toHex(this[i])
  3228. if (i === exports.INSPECT_MAX_BYTES) {
  3229. out[i + 1] = '...'
  3230. break
  3231. }
  3232. }
  3233. return '<Buffer ' + out.join(' ') + '>'
  3234. }
  3235. /**
  3236. * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.
  3237. * Added in Node 0.12. Only available in browsers that support ArrayBuffer.
  3238. */
  3239. Buffer.prototype.toArrayBuffer = function () {
  3240. if (typeof Uint8Array !== 'undefined') {
  3241. if (Buffer.TYPED_ARRAY_SUPPORT) {
  3242. return (new Buffer(this)).buffer
  3243. } else {
  3244. var buf = new Uint8Array(this.length)
  3245. for (var i = 0, len = buf.length; i < len; i += 1) {
  3246. buf[i] = this[i]
  3247. }
  3248. return buf.buffer
  3249. }
  3250. } else {
  3251. throw new Error('Buffer.toArrayBuffer not supported in this browser')
  3252. }
  3253. }
  3254. // HELPER FUNCTIONS
  3255. // ================
  3256. var BP = Buffer.prototype
  3257. /**
  3258. * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods
  3259. */
  3260. Buffer._augment = function (arr) {
  3261. arr._isBuffer = true
  3262. // save reference to original Uint8Array get/set methods before overwriting
  3263. arr._get = arr.get
  3264. arr._set = arr.set
  3265. // deprecated, will be removed in node 0.13+
  3266. arr.get = BP.get
  3267. arr.set = BP.set
  3268. arr.write = BP.write
  3269. arr.toString = BP.toString
  3270. arr.toLocaleString = BP.toString
  3271. arr.toJSON = BP.toJSON
  3272. arr.equals = BP.equals
  3273. arr.compare = BP.compare
  3274. arr.copy = BP.copy
  3275. arr.slice = BP.slice
  3276. arr.readUInt8 = BP.readUInt8
  3277. arr.readUInt16LE = BP.readUInt16LE
  3278. arr.readUInt16BE = BP.readUInt16BE
  3279. arr.readUInt32LE = BP.readUInt32LE
  3280. arr.readUInt32BE = BP.readUInt32BE
  3281. arr.readInt8 = BP.readInt8
  3282. arr.readInt16LE = BP.readInt16LE
  3283. arr.readInt16BE = BP.readInt16BE
  3284. arr.readInt32LE = BP.readInt32LE
  3285. arr.readInt32BE = BP.readInt32BE
  3286. arr.readFloatLE = BP.readFloatLE
  3287. arr.readFloatBE = BP.readFloatBE
  3288. arr.readDoubleLE = BP.readDoubleLE
  3289. arr.readDoubleBE = BP.readDoubleBE
  3290. arr.writeUInt8 = BP.writeUInt8
  3291. arr.writeUInt16LE = BP.writeUInt16LE
  3292. arr.writeUInt16BE = BP.writeUInt16BE
  3293. arr.writeUInt32LE = BP.writeUInt32LE
  3294. arr.writeUInt32BE = BP.writeUInt32BE
  3295. arr.writeInt8 = BP.writeInt8
  3296. arr.writeInt16LE = BP.writeInt16LE
  3297. arr.writeInt16BE = BP.writeInt16BE
  3298. arr.writeInt32LE = BP.writeInt32LE
  3299. arr.writeInt32BE = BP.writeInt32BE
  3300. arr.writeFloatLE = BP.writeFloatLE
  3301. arr.writeFloatBE = BP.writeFloatBE
  3302. arr.writeDoubleLE = BP.writeDoubleLE
  3303. arr.writeDoubleBE = BP.writeDoubleBE
  3304. arr.fill = BP.fill
  3305. arr.inspect = BP.inspect
  3306. arr.toArrayBuffer = BP.toArrayBuffer
  3307. return arr
  3308. }
  3309. var INVALID_BASE64_RE = /[^+\/0-9A-z]/g
  3310. function base64clean (str) {
  3311. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  3312. str = stringtrim(str).replace(INVALID_BASE64_RE, '')
  3313. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  3314. while (str.length % 4 !== 0) {
  3315. str = str + '='
  3316. }
  3317. return str
  3318. }
  3319. function stringtrim (str) {
  3320. if (str.trim) return str.trim()
  3321. return str.replace(/^\s+|\s+$/g, '')
  3322. }
  3323. function isArray (subject) {
  3324. return (Array.isArray || function (subject) {
  3325. return Object.prototype.toString.call(subject) === '[object Array]'
  3326. })(subject)
  3327. }
  3328. function isArrayish (subject) {
  3329. return isArray(subject) || Buffer.isBuffer(subject) ||
  3330. subject && typeof subject === 'object' &&
  3331. typeof subject.length === 'number'
  3332. }
  3333. function toHex (n) {
  3334. if (n < 16) return '0' + n.toString(16)
  3335. return n.toString(16)
  3336. }
  3337. function utf8ToBytes (str) {
  3338. var byteArray = []
  3339. for (var i = 0; i < str.length; i++) {
  3340. var b = str.charCodeAt(i)
  3341. if (b <= 0x7F) {
  3342. byteArray.push(b)
  3343. } else {
  3344. var start = i
  3345. if (b >= 0xD800 && b <= 0xDFFF) i++
  3346. var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%')
  3347. for (var j = 0; j < h.length; j++) {
  3348. byteArray.push(parseInt(h[j], 16))
  3349. }
  3350. }
  3351. }
  3352. return byteArray
  3353. }
  3354. function asciiToBytes (str) {
  3355. var byteArray = []
  3356. for (var i = 0; i < str.length; i++) {
  3357. // Node's code seems to be doing this and not & 0x7F..
  3358. byteArray.push(str.charCodeAt(i) & 0xFF)
  3359. }
  3360. return byteArray
  3361. }
  3362. function utf16leToBytes (str) {
  3363. var c, hi, lo
  3364. var byteArray = []
  3365. for (var i = 0; i < str.length; i++) {
  3366. c = str.charCodeAt(i)
  3367. hi = c >> 8
  3368. lo = c % 256
  3369. byteArray.push(lo)
  3370. byteArray.push(hi)
  3371. }
  3372. return byteArray
  3373. }
  3374. function base64ToBytes (str) {
  3375. return base64.toByteArray(str)
  3376. }
  3377. function blitBuffer (src, dst, offset, length) {
  3378. for (var i = 0; i < length; i++) {
  3379. if ((i + offset >= dst.length) || (i >= src.length))
  3380. break
  3381. dst[i + offset] = src[i]
  3382. }
  3383. return i
  3384. }
  3385. function decodeUtf8Char (str) {
  3386. try {
  3387. return decodeURIComponent(str)
  3388. } catch (err) {
  3389. return String.fromCharCode(0xFFFD) // UTF 8 invalid char
  3390. }
  3391. }
  3392. function assert (test, message) {
  3393. if (!test) throw new Error(message || 'Failed assertion')
  3394. }
  3395. },{"base64-js":45,"ieee754":46}],45:[function(require,module,exports){
  3396. var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  3397. ;(function (exports) {
  3398. 'use strict';
  3399. var Arr = (typeof Uint8Array !== 'undefined')
  3400. ? Uint8Array
  3401. : Array
  3402. var PLUS = '+'.charCodeAt(0)
  3403. var SLASH = '/'.charCodeAt(0)
  3404. var NUMBER = '0'.charCodeAt(0)
  3405. var LOWER = 'a'.charCodeAt(0)
  3406. var UPPER = 'A'.charCodeAt(0)
  3407. function decode (elt) {
  3408. var code = elt.charCodeAt(0)
  3409. if (code === PLUS)
  3410. return 62 // '+'
  3411. if (code === SLASH)
  3412. return 63 // '/'
  3413. if (code < NUMBER)
  3414. return -1 //no match
  3415. if (code < NUMBER + 10)
  3416. return code - NUMBER + 26 + 26
  3417. if (code < UPPER + 26)
  3418. return code - UPPER
  3419. if (code < LOWER + 26)
  3420. return code - LOWER + 26
  3421. }
  3422. function b64ToByteArray (b64) {
  3423. var i, j, l, tmp, placeHolders, arr
  3424. if (b64.length % 4 > 0) {
  3425. throw new Error('Invalid string. Length must be a multiple of 4')
  3426. }
  3427. // the number of equal signs (place holders)
  3428. // if there are two placeholders, than the two characters before it
  3429. // represent one byte
  3430. // if there is only one, then the three characters before it represent 2 bytes
  3431. // this is just a cheap hack to not do indexOf twice
  3432. var len = b64.length
  3433. placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0
  3434. // base64 is 4/3 + up to two characters of the original data
  3435. arr = new Arr(b64.length * 3 / 4 - placeHolders)
  3436. // if there are placeholders, only get up to the last complete 4 chars
  3437. l = placeHolders > 0 ? b64.length - 4 : b64.length
  3438. var L = 0
  3439. function push (v) {
  3440. arr[L++] = v
  3441. }
  3442. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  3443. tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
  3444. push((tmp & 0xFF0000) >> 16)
  3445. push((tmp & 0xFF00) >> 8)
  3446. push(tmp & 0xFF)
  3447. }
  3448. if (placeHolders === 2) {
  3449. tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
  3450. push(tmp & 0xFF)
  3451. } else if (placeHolders === 1) {
  3452. tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
  3453. push((tmp >> 8) & 0xFF)
  3454. push(tmp & 0xFF)
  3455. }
  3456. return arr
  3457. }
  3458. function uint8ToBase64 (uint8) {
  3459. var i,
  3460. extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
  3461. output = "",
  3462. temp, length
  3463. function encode (num) {
  3464. return lookup.charAt(num)
  3465. }
  3466. function tripletToBase64 (num) {
  3467. return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
  3468. }
  3469. // go through the array every three bytes, we'll deal with trailing stuff later
  3470. for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
  3471. temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  3472. output += tripletToBase64(temp)
  3473. }
  3474. // pad the end with zeros, but make sure to not forget the extra bytes
  3475. switch (extraBytes) {
  3476. case 1:
  3477. temp = uint8[uint8.length - 1]
  3478. output += encode(temp >> 2)
  3479. output += encode((temp << 4) & 0x3F)
  3480. output += '=='
  3481. break
  3482. case 2:
  3483. temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
  3484. output += encode(temp >> 10)
  3485. output += encode((temp >> 4) & 0x3F)
  3486. output += encode((temp << 2) & 0x3F)
  3487. output += '='
  3488. break
  3489. }
  3490. return output
  3491. }
  3492. exports.toByteArray = b64ToByteArray
  3493. exports.fromByteArray = uint8ToBase64
  3494. }(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
  3495. },{}],46:[function(require,module,exports){
  3496. exports.read = function(buffer, offset, isLE, mLen, nBytes) {
  3497. var e, m,
  3498. eLen = nBytes * 8 - mLen - 1,
  3499. eMax = (1 << eLen) - 1,
  3500. eBias = eMax >> 1,
  3501. nBits = -7,
  3502. i = isLE ? (nBytes - 1) : 0,
  3503. d = isLE ? -1 : 1,
  3504. s = buffer[offset + i];
  3505. i += d;
  3506. e = s & ((1 << (-nBits)) - 1);
  3507. s >>= (-nBits);
  3508. nBits += eLen;
  3509. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
  3510. m = e & ((1 << (-nBits)) - 1);
  3511. e >>= (-nBits);
  3512. nBits += mLen;
  3513. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
  3514. if (e === 0) {
  3515. e = 1 - eBias;
  3516. } else if (e === eMax) {
  3517. return m ? NaN : ((s ? -1 : 1) * Infinity);
  3518. } else {
  3519. m = m + Math.pow(2, mLen);
  3520. e = e - eBias;
  3521. }
  3522. return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
  3523. };
  3524. exports.write = function(buffer, value, offset, isLE, mLen, nBytes) {
  3525. var e, m, c,
  3526. eLen = nBytes * 8 - mLen - 1,
  3527. eMax = (1 << eLen) - 1,
  3528. eBias = eMax >> 1,
  3529. rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
  3530. i = isLE ? 0 : (nBytes - 1),
  3531. d = isLE ? 1 : -1,
  3532. s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  3533. value = Math.abs(value);
  3534. if (isNaN(value) || value === Infinity) {
  3535. m = isNaN(value) ? 1 : 0;
  3536. e = eMax;
  3537. } else {
  3538. e = Math.floor(Math.log(value) / Math.LN2);
  3539. if (value * (c = Math.pow(2, -e)) < 1) {
  3540. e--;
  3541. c *= 2;
  3542. }
  3543. if (e + eBias >= 1) {
  3544. value += rt / c;
  3545. } else {
  3546. value += rt * Math.pow(2, 1 - eBias);
  3547. }
  3548. if (value * c >= 2) {
  3549. e++;
  3550. c /= 2;
  3551. }
  3552. if (e + eBias >= eMax) {
  3553. m = 0;
  3554. e = eMax;
  3555. } else if (e + eBias >= 1) {
  3556. m = (value * c - 1) * Math.pow(2, mLen);
  3557. e = e + eBias;
  3558. } else {
  3559. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  3560. e = 0;
  3561. }
  3562. }
  3563. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
  3564. e = (e << mLen) | m;
  3565. eLen += mLen;
  3566. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
  3567. buffer[offset + i - d] |= s * 128;
  3568. };
  3569. },{}],47:[function(require,module,exports){
  3570. // Copyright Joyent, Inc. and other Node contributors.
  3571. //
  3572. // Permission is hereby granted, free of charge, to any person obtaining a
  3573. // copy of this software and associated documentation files (the
  3574. // "Software"), to deal in the Software without restriction, including
  3575. // without limitation the rights to use, copy, modify, merge, publish,
  3576. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3577. // persons to whom the Software is furnished to do so, subject to the
  3578. // following conditions:
  3579. //
  3580. // The above copyright notice and this permission notice shall be included
  3581. // in all copies or substantial portions of the Software.
  3582. //
  3583. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3584. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3585. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3586. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3587. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3588. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3589. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3590. function EventEmitter() {
  3591. this._events = this._events || {};
  3592. this._maxListeners = this._maxListeners || undefined;
  3593. }
  3594. module.exports = EventEmitter;
  3595. // Backwards-compat with node 0.10.x
  3596. EventEmitter.EventEmitter = EventEmitter;
  3597. EventEmitter.prototype._events = undefined;
  3598. EventEmitter.prototype._maxListeners = undefined;
  3599. // By default EventEmitters will print a warning if more than 10 listeners are
  3600. // added to it. This is a useful default which helps finding memory leaks.
  3601. EventEmitter.defaultMaxListeners = 10;
  3602. // Obviously not all Emitters should be limited to 10. This function allows
  3603. // that to be increased. Set to zero for unlimited.
  3604. EventEmitter.prototype.setMaxListeners = function(n) {
  3605. if (!isNumber(n) || n < 0 || isNaN(n))
  3606. throw TypeError('n must be a positive number');
  3607. this._maxListeners = n;
  3608. return this;
  3609. };
  3610. EventEmitter.prototype.emit = function(type) {
  3611. var er, handler, len, args, i, listeners;
  3612. if (!this._events)
  3613. this._events = {};
  3614. // If there is no 'error' event listener then throw.
  3615. if (type === 'error') {
  3616. if (!this._events.error ||
  3617. (isObject(this._events.error) && !this._events.error.length)) {
  3618. er = arguments[1];
  3619. if (er instanceof Error) {
  3620. throw er; // Unhandled 'error' event
  3621. }
  3622. throw TypeError('Uncaught, unspecified "error" event.');
  3623. }
  3624. }
  3625. handler = this._events[type];
  3626. if (isUndefined(handler))
  3627. return false;
  3628. if (isFunction(handler)) {
  3629. switch (arguments.length) {
  3630. // fast cases
  3631. case 1:
  3632. handler.call(this);
  3633. break;
  3634. case 2:
  3635. handler.call(this, arguments[1]);
  3636. break;
  3637. case 3:
  3638. handler.call(this, arguments[1], arguments[2]);
  3639. break;
  3640. // slower
  3641. default:
  3642. len = arguments.length;
  3643. args = new Array(len - 1);
  3644. for (i = 1; i < len; i++)
  3645. args[i - 1] = arguments[i];
  3646. handler.apply(this, args);
  3647. }
  3648. } else if (isObject(handler)) {
  3649. len = arguments.length;
  3650. args = new Array(len - 1);
  3651. for (i = 1; i < len; i++)
  3652. args[i - 1] = arguments[i];
  3653. listeners = handler.slice();
  3654. len = listeners.length;
  3655. for (i = 0; i < len; i++)
  3656. listeners[i].apply(this, args);
  3657. }
  3658. return true;
  3659. };
  3660. EventEmitter.prototype.addListener = function(type, listener) {
  3661. var m;
  3662. if (!isFunction(listener))
  3663. throw TypeError('listener must be a function');
  3664. if (!this._events)
  3665. this._events = {};
  3666. // To avoid recursion in the case that type === "newListener"! Before
  3667. // adding it to the listeners, first emit "newListener".
  3668. if (this._events.newListener)
  3669. this.emit('newListener', type,
  3670. isFunction(listener.listener) ?
  3671. listener.listener : listener);
  3672. if (!this._events[type])
  3673. // Optimize the case of one listener. Don't need the extra array object.
  3674. this._events[type] = listener;
  3675. else if (isObject(this._events[type]))
  3676. // If we've already got an array, just append.
  3677. this._events[type].push(listener);
  3678. else
  3679. // Adding the second element, need to change to array.
  3680. this._events[type] = [this._events[type], listener];
  3681. // Check for listener leak
  3682. if (isObject(this._events[type]) && !this._events[type].warned) {
  3683. var m;
  3684. if (!isUndefined(this._maxListeners)) {
  3685. m = this._maxListeners;
  3686. } else {
  3687. m = EventEmitter.defaultMaxListeners;
  3688. }
  3689. if (m && m > 0 && this._events[type].length > m) {
  3690. this._events[type].warned = true;
  3691. console.error('(node) warning: possible EventEmitter memory ' +
  3692. 'leak detected. %d listeners added. ' +
  3693. 'Use emitter.setMaxListeners() to increase limit.',
  3694. this._events[type].length);
  3695. if (typeof console.trace === 'function') {
  3696. // not supported in IE 10
  3697. console.trace();
  3698. }
  3699. }
  3700. }
  3701. return this;
  3702. };
  3703. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  3704. EventEmitter.prototype.once = function(type, listener) {
  3705. if (!isFunction(listener))
  3706. throw TypeError('listener must be a function');
  3707. var fired = false;
  3708. function g() {
  3709. this.removeListener(type, g);
  3710. if (!fired) {
  3711. fired = true;
  3712. listener.apply(this, arguments);
  3713. }
  3714. }
  3715. g.listener = listener;
  3716. this.on(type, g);
  3717. return this;
  3718. };
  3719. // emits a 'removeListener' event iff the listener was removed
  3720. EventEmitter.prototype.removeListener = function(type, listener) {
  3721. var list, position, length, i;
  3722. if (!isFunction(listener))
  3723. throw TypeError('listener must be a function');
  3724. if (!this._events || !this._events[type])
  3725. return this;
  3726. list = this._events[type];
  3727. length = list.length;
  3728. position = -1;
  3729. if (list === listener ||
  3730. (isFunction(list.listener) && list.listener === listener)) {
  3731. delete this._events[type];
  3732. if (this._events.removeListener)
  3733. this.emit('removeListener', type, listener);
  3734. } else if (isObject(list)) {
  3735. for (i = length; i-- > 0;) {
  3736. if (list[i] === listener ||
  3737. (list[i].listener && list[i].listener === listener)) {
  3738. position = i;
  3739. break;
  3740. }
  3741. }
  3742. if (position < 0)
  3743. return this;
  3744. if (list.length === 1) {
  3745. list.length = 0;
  3746. delete this._events[type];
  3747. } else {
  3748. list.splice(position, 1);
  3749. }
  3750. if (this._events.removeListener)
  3751. this.emit('removeListener', type, listener);
  3752. }
  3753. return this;
  3754. };
  3755. EventEmitter.prototype.removeAllListeners = function(type) {
  3756. var key, listeners;
  3757. if (!this._events)
  3758. return this;
  3759. // not listening for removeListener, no need to emit
  3760. if (!this._events.removeListener) {
  3761. if (arguments.length === 0)
  3762. this._events = {};
  3763. else if (this._events[type])
  3764. delete this._events[type];
  3765. return this;
  3766. }
  3767. // emit removeListener for all listeners on all events
  3768. if (arguments.length === 0) {
  3769. for (key in this._events) {
  3770. if (key === 'removeListener') continue;
  3771. this.removeAllListeners(key);
  3772. }
  3773. this.removeAllListeners('removeListener');
  3774. this._events = {};
  3775. return this;
  3776. }
  3777. listeners = this._events[type];
  3778. if (isFunction(listeners)) {
  3779. this.removeListener(type, listeners);
  3780. } else {
  3781. // LIFO order
  3782. while (listeners.length)
  3783. this.removeListener(type, listeners[listeners.length - 1]);
  3784. }
  3785. delete this._events[type];
  3786. return this;
  3787. };
  3788. EventEmitter.prototype.listeners = function(type) {
  3789. var ret;
  3790. if (!this._events || !this._events[type])
  3791. ret = [];
  3792. else if (isFunction(this._events[type]))
  3793. ret = [this._events[type]];
  3794. else
  3795. ret = this._events[type].slice();
  3796. return ret;
  3797. };
  3798. EventEmitter.listenerCount = function(emitter, type) {
  3799. var ret;
  3800. if (!emitter._events || !emitter._events[type])
  3801. ret = 0;
  3802. else if (isFunction(emitter._events[type]))
  3803. ret = 1;
  3804. else
  3805. ret = emitter._events[type].length;
  3806. return ret;
  3807. };
  3808. function isFunction(arg) {
  3809. return typeof arg === 'function';
  3810. }
  3811. function isNumber(arg) {
  3812. return typeof arg === 'number';
  3813. }
  3814. function isObject(arg) {
  3815. return typeof arg === 'object' && arg !== null;
  3816. }
  3817. function isUndefined(arg) {
  3818. return arg === void 0;
  3819. }
  3820. },{}],48:[function(require,module,exports){
  3821. module.exports=require(11)
  3822. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/inherits/inherits_browser.js":11}],49:[function(require,module,exports){
  3823. module.exports=require(12)
  3824. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/isarray/index.js":12}],50:[function(require,module,exports){
  3825. // shim for using process in browser
  3826. var process = module.exports = {};
  3827. process.nextTick = (function () {
  3828. var canSetImmediate = typeof window !== 'undefined'
  3829. && window.setImmediate;
  3830. var canPost = typeof window !== 'undefined'
  3831. && window.postMessage && window.addEventListener
  3832. ;
  3833. if (canSetImmediate) {
  3834. return function (f) { return window.setImmediate(f) };
  3835. }
  3836. if (canPost) {
  3837. var queue = [];
  3838. window.addEventListener('message', function (ev) {
  3839. var source = ev.source;
  3840. if ((source === window || source === null) && ev.data === 'process-tick') {
  3841. ev.stopPropagation();
  3842. if (queue.length > 0) {
  3843. var fn = queue.shift();
  3844. fn();
  3845. }
  3846. }
  3847. }, true);
  3848. return function nextTick(fn) {
  3849. queue.push(fn);
  3850. window.postMessage('process-tick', '*');
  3851. };
  3852. }
  3853. return function nextTick(fn) {
  3854. setTimeout(fn, 0);
  3855. };
  3856. })();
  3857. process.title = 'browser';
  3858. process.browser = true;
  3859. process.env = {};
  3860. process.argv = [];
  3861. function noop() {}
  3862. process.on = noop;
  3863. process.addListener = noop;
  3864. process.once = noop;
  3865. process.off = noop;
  3866. process.removeListener = noop;
  3867. process.removeAllListeners = noop;
  3868. process.emit = noop;
  3869. process.binding = function (name) {
  3870. throw new Error('process.binding is not supported');
  3871. }
  3872. // TODO(shtylman)
  3873. process.cwd = function () { return '/' };
  3874. process.chdir = function (dir) {
  3875. throw new Error('process.chdir is not supported');
  3876. };
  3877. },{}],51:[function(require,module,exports){
  3878. module.exports = require("./lib/_stream_duplex.js")
  3879. },{"./lib/_stream_duplex.js":52}],52:[function(require,module,exports){
  3880. module.exports=require(6)
  3881. },{"./_stream_readable":54,"./_stream_writable":56,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_duplex.js":6,"_process":50,"core-util-is":57,"inherits":48}],53:[function(require,module,exports){
  3882. module.exports=require(23)
  3883. },{"./_stream_transform":55,"/Users/macbookpro/dev/bpipe/node_modules/websocket-stream/node_modules/duplexify/node_modules/readable-stream/lib/_stream_passthrough.js":23,"core-util-is":57,"inherits":48}],54:[function(require,module,exports){
  3884. module.exports=require(7)
  3885. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js":7,"_process":50,"buffer":44,"core-util-is":57,"events":47,"inherits":48,"isarray":49,"stream":63,"string_decoder/":58}],55:[function(require,module,exports){
  3886. module.exports=require(8)
  3887. },{"./_stream_duplex":52,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js":8,"core-util-is":57,"inherits":48}],56:[function(require,module,exports){
  3888. module.exports=require(9)
  3889. },{"./_stream_duplex":52,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js":9,"_process":50,"buffer":44,"core-util-is":57,"inherits":48,"stream":63}],57:[function(require,module,exports){
  3890. module.exports=require(10)
  3891. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/core-util-is/lib/util.js":10,"buffer":44}],58:[function(require,module,exports){
  3892. module.exports=require(13)
  3893. },{"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/node_modules/string_decoder/index.js":13,"buffer":44}],59:[function(require,module,exports){
  3894. module.exports = require("./lib/_stream_passthrough.js")
  3895. },{"./lib/_stream_passthrough.js":53}],60:[function(require,module,exports){
  3896. module.exports=require(31)
  3897. },{"./lib/_stream_duplex.js":52,"./lib/_stream_passthrough.js":53,"./lib/_stream_readable.js":54,"./lib/_stream_transform.js":55,"./lib/_stream_writable.js":56,"/Users/macbookpro/dev/bpipe/node_modules/websocket-stream/node_modules/duplexify/node_modules/readable-stream/readable.js":31}],61:[function(require,module,exports){
  3898. module.exports=require(14)
  3899. },{"./lib/_stream_transform.js":55,"/Users/macbookpro/dev/bpipe/node_modules/through2/node_modules/readable-stream/transform.js":14}],62:[function(require,module,exports){
  3900. module.exports = require("./lib/_stream_writable.js")
  3901. },{"./lib/_stream_writable.js":56}],63:[function(require,module,exports){
  3902. // Copyright Joyent, Inc. and other Node contributors.
  3903. //
  3904. // Permission is hereby granted, free of charge, to any person obtaining a
  3905. // copy of this software and associated documentation files (the
  3906. // "Software"), to deal in the Software without restriction, including
  3907. // without limitation the rights to use, copy, modify, merge, publish,
  3908. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3909. // persons to whom the Software is furnished to do so, subject to the
  3910. // following conditions:
  3911. //
  3912. // The above copyright notice and this permission notice shall be included
  3913. // in all copies or substantial portions of the Software.
  3914. //
  3915. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3916. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3917. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3918. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3919. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3920. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3921. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3922. module.exports = Stream;
  3923. var EE = require('events').EventEmitter;
  3924. var inherits = require('inherits');
  3925. inherits(Stream, EE);
  3926. Stream.Readable = require('readable-stream/readable.js');
  3927. Stream.Writable = require('readable-stream/writable.js');
  3928. Stream.Duplex = require('readable-stream/duplex.js');
  3929. Stream.Transform = require('readable-stream/transform.js');
  3930. Stream.PassThrough = require('readable-stream/passthrough.js');
  3931. // Backwards-compat with node 0.4.x
  3932. Stream.Stream = Stream;
  3933. // old-style streams. Note that the pipe method (the only relevant
  3934. // part of this class) is overridden in the Readable class.
  3935. function Stream() {
  3936. EE.call(this);
  3937. }
  3938. Stream.prototype.pipe = function(dest, options) {
  3939. var source = this;
  3940. function ondata(chunk) {
  3941. if (dest.writable) {
  3942. if (false === dest.write(chunk) && source.pause) {
  3943. source.pause();
  3944. }
  3945. }
  3946. }
  3947. source.on('data', ondata);
  3948. function ondrain() {
  3949. if (source.readable && source.resume) {
  3950. source.resume();
  3951. }
  3952. }
  3953. dest.on('drain', ondrain);
  3954. // If the 'end' option is not supplied, dest.end() will be called when
  3955. // source gets the 'end' or 'close' events. Only dest.end() once.
  3956. if (!dest._isStdio && (!options || options.end !== false)) {
  3957. source.on('end', onend);
  3958. source.on('close', onclose);
  3959. }
  3960. var didOnEnd = false;
  3961. function onend() {
  3962. if (didOnEnd) return;
  3963. didOnEnd = true;
  3964. dest.end();
  3965. }
  3966. function onclose() {
  3967. if (didOnEnd) return;
  3968. didOnEnd = true;
  3969. if (typeof dest.destroy === 'function') dest.destroy();
  3970. }
  3971. // don't leave dangling pipes when there are errors.
  3972. function onerror(er) {
  3973. cleanup();
  3974. if (EE.listenerCount(this, 'error') === 0) {
  3975. throw er; // Unhandled stream error in pipe.
  3976. }
  3977. }
  3978. source.on('error', onerror);
  3979. dest.on('error', onerror);
  3980. // remove all the event listeners that were added.
  3981. function cleanup() {
  3982. source.removeListener('data', ondata);
  3983. dest.removeListener('drain', ondrain);
  3984. source.removeListener('end', onend);
  3985. source.removeListener('close', onclose);
  3986. source.removeListener('error', onerror);
  3987. dest.removeListener('error', onerror);
  3988. source.removeListener('end', cleanup);
  3989. source.removeListener('close', cleanup);
  3990. dest.removeListener('close', cleanup);
  3991. }
  3992. source.on('end', cleanup);
  3993. source.on('close', cleanup);
  3994. dest.on('close', cleanup);
  3995. dest.emit('pipe', source);
  3996. // Allow for unix-like usage: A.pipe(B).pipe(C)
  3997. return dest;
  3998. };
  3999. },{"events":47,"inherits":48,"readable-stream/duplex.js":51,"readable-stream/passthrough.js":59,"readable-stream/readable.js":60,"readable-stream/transform.js":61,"readable-stream/writable.js":62}],64:[function(require,module,exports){
  4000. module.exports = function isBuffer(arg) {
  4001. return arg && typeof arg === 'object'
  4002. && typeof arg.copy === 'function'
  4003. && typeof arg.fill === 'function'
  4004. && typeof arg.readUInt8 === 'function';
  4005. }
  4006. },{}],65:[function(require,module,exports){
  4007. (function (process,global){
  4008. // Copyright Joyent, Inc. and other Node contributors.
  4009. //
  4010. // Permission is hereby granted, free of charge, to any person obtaining a
  4011. // copy of this software and associated documentation files (the
  4012. // "Software"), to deal in the Software without restriction, including
  4013. // without limitation the rights to use, copy, modify, merge, publish,
  4014. // distribute, sublicense, and/or sell copies of the Software, and to permit
  4015. // persons to whom the Software is furnished to do so, subject to the
  4016. // following conditions:
  4017. //
  4018. // The above copyright notice and this permission notice shall be included
  4019. // in all copies or substantial portions of the Software.
  4020. //
  4021. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  4022. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  4023. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  4024. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  4025. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  4026. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  4027. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  4028. var formatRegExp = /%[sdj%]/g;
  4029. exports.format = function(f) {
  4030. if (!isString(f)) {
  4031. var objects = [];
  4032. for (var i = 0; i < arguments.length; i++) {
  4033. objects.push(inspect(arguments[i]));
  4034. }
  4035. return objects.join(' ');
  4036. }
  4037. var i = 1;
  4038. var args = arguments;
  4039. var len = args.length;
  4040. var str = String(f).replace(formatRegExp, function(x) {
  4041. if (x === '%%') return '%';
  4042. if (i >= len) return x;
  4043. switch (x) {
  4044. case '%s': return String(args[i++]);
  4045. case '%d': return Number(args[i++]);
  4046. case '%j':
  4047. try {
  4048. return JSON.stringify(args[i++]);
  4049. } catch (_) {
  4050. return '[Circular]';
  4051. }
  4052. default:
  4053. return x;
  4054. }
  4055. });
  4056. for (var x = args[i]; i < len; x = args[++i]) {
  4057. if (isNull(x) || !isObject(x)) {
  4058. str += ' ' + x;
  4059. } else {
  4060. str += ' ' + inspect(x);
  4061. }
  4062. }
  4063. return str;
  4064. };
  4065. // Mark that a method should not be used.
  4066. // Returns a modified function which warns once by default.
  4067. // If --no-deprecation is set, then it is a no-op.
  4068. exports.deprecate = function(fn, msg) {
  4069. // Allow for deprecating things in the process of starting up.
  4070. if (isUndefined(global.process)) {
  4071. return function() {
  4072. return exports.deprecate(fn, msg).apply(this, arguments);
  4073. };
  4074. }
  4075. if (process.noDeprecation === true) {
  4076. return fn;
  4077. }
  4078. var warned = false;
  4079. function deprecated() {
  4080. if (!warned) {
  4081. if (process.throwDeprecation) {
  4082. throw new Error(msg);
  4083. } else if (process.traceDeprecation) {
  4084. console.trace(msg);
  4085. } else {
  4086. console.error(msg);
  4087. }
  4088. warned = true;
  4089. }
  4090. return fn.apply(this, arguments);
  4091. }
  4092. return deprecated;
  4093. };
  4094. var debugs = {};
  4095. var debugEnviron;
  4096. exports.debuglog = function(set) {
  4097. if (isUndefined(debugEnviron))
  4098. debugEnviron = process.env.NODE_DEBUG || '';
  4099. set = set.toUpperCase();
  4100. if (!debugs[set]) {
  4101. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  4102. var pid = process.pid;
  4103. debugs[set] = function() {
  4104. var msg = exports.format.apply(exports, arguments);
  4105. console.error('%s %d: %s', set, pid, msg);
  4106. };
  4107. } else {
  4108. debugs[set] = function() {};
  4109. }
  4110. }
  4111. return debugs[set];
  4112. };
  4113. /**
  4114. * Echos the value of a value. Trys to print the value out
  4115. * in the best way possible given the different types.
  4116. *
  4117. * @param {Object} obj The object to print out.
  4118. * @param {Object} opts Optional options object that alters the output.
  4119. */
  4120. /* legacy: obj, showHidden, depth, colors*/
  4121. function inspect(obj, opts) {
  4122. // default options
  4123. var ctx = {
  4124. seen: [],
  4125. stylize: stylizeNoColor
  4126. };
  4127. // legacy...
  4128. if (arguments.length >= 3) ctx.depth = arguments[2];
  4129. if (arguments.length >= 4) ctx.colors = arguments[3];
  4130. if (isBoolean(opts)) {
  4131. // legacy...
  4132. ctx.showHidden = opts;
  4133. } else if (opts) {
  4134. // got an "options" object
  4135. exports._extend(ctx, opts);
  4136. }
  4137. // set default options
  4138. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  4139. if (isUndefined(ctx.depth)) ctx.depth = 2;
  4140. if (isUndefined(ctx.colors)) ctx.colors = false;
  4141. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  4142. if (ctx.colors) ctx.stylize = stylizeWithColor;
  4143. return formatValue(ctx, obj, ctx.depth);
  4144. }
  4145. exports.inspect = inspect;
  4146. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  4147. inspect.colors = {
  4148. 'bold' : [1, 22],
  4149. 'italic' : [3, 23],
  4150. 'underline' : [4, 24],
  4151. 'inverse' : [7, 27],
  4152. 'white' : [37, 39],
  4153. 'grey' : [90, 39],
  4154. 'black' : [30, 39],
  4155. 'blue' : [34, 39],
  4156. 'cyan' : [36, 39],
  4157. 'green' : [32, 39],
  4158. 'magenta' : [35, 39],
  4159. 'red' : [31, 39],
  4160. 'yellow' : [33, 39]
  4161. };
  4162. // Don't use 'blue' not visible on cmd.exe
  4163. inspect.styles = {
  4164. 'special': 'cyan',
  4165. 'number': 'yellow',
  4166. 'boolean': 'yellow',
  4167. 'undefined': 'grey',
  4168. 'null': 'bold',
  4169. 'string': 'green',
  4170. 'date': 'magenta',
  4171. // "name": intentionally not styling
  4172. 'regexp': 'red'
  4173. };
  4174. function stylizeWithColor(str, styleType) {
  4175. var style = inspect.styles[styleType];
  4176. if (style) {
  4177. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  4178. '\u001b[' + inspect.colors[style][1] + 'm';
  4179. } else {
  4180. return str;
  4181. }
  4182. }
  4183. function stylizeNoColor(str, styleType) {
  4184. return str;
  4185. }
  4186. function arrayToHash(array) {
  4187. var hash = {};
  4188. array.forEach(function(val, idx) {
  4189. hash[val] = true;
  4190. });
  4191. return hash;
  4192. }
  4193. function formatValue(ctx, value, recurseTimes) {
  4194. // Provide a hook for user-specified inspect functions.
  4195. // Check that value is an object with an inspect function on it
  4196. if (ctx.customInspect &&
  4197. value &&
  4198. isFunction(value.inspect) &&
  4199. // Filter out the util module, it's inspect function is special
  4200. value.inspect !== exports.inspect &&
  4201. // Also filter out any prototype objects using the circular check.
  4202. !(value.constructor && value.constructor.prototype === value)) {
  4203. var ret = value.inspect(recurseTimes, ctx);
  4204. if (!isString(ret)) {
  4205. ret = formatValue(ctx, ret, recurseTimes);
  4206. }
  4207. return ret;
  4208. }
  4209. // Primitive types cannot have properties
  4210. var primitive = formatPrimitive(ctx, value);
  4211. if (primitive) {
  4212. return primitive;
  4213. }
  4214. // Look up the keys of the object.
  4215. var keys = Object.keys(value);
  4216. var visibleKeys = arrayToHash(keys);
  4217. if (ctx.showHidden) {
  4218. keys = Object.getOwnPropertyNames(value);
  4219. }
  4220. // IE doesn't make error fields non-enumerable
  4221. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  4222. if (isError(value)
  4223. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  4224. return formatError(value);
  4225. }
  4226. // Some type of object without properties can be shortcutted.
  4227. if (keys.length === 0) {
  4228. if (isFunction(value)) {
  4229. var name = value.name ? ': ' + value.name : '';
  4230. return ctx.stylize('[Function' + name + ']', 'special');
  4231. }
  4232. if (isRegExp(value)) {
  4233. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  4234. }
  4235. if (isDate(value)) {
  4236. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  4237. }
  4238. if (isError(value)) {
  4239. return formatError(value);
  4240. }
  4241. }
  4242. var base = '', array = false, braces = ['{', '}'];
  4243. // Make Array say that they are Array
  4244. if (isArray(value)) {
  4245. array = true;
  4246. braces = ['[', ']'];
  4247. }
  4248. // Make functions say that they are functions
  4249. if (isFunction(value)) {
  4250. var n = value.name ? ': ' + value.name : '';
  4251. base = ' [Function' + n + ']';
  4252. }
  4253. // Make RegExps say that they are RegExps
  4254. if (isRegExp(value)) {
  4255. base = ' ' + RegExp.prototype.toString.call(value);
  4256. }
  4257. // Make dates with properties first say the date
  4258. if (isDate(value)) {
  4259. base = ' ' + Date.prototype.toUTCString.call(value);
  4260. }
  4261. // Make error with message first say the error
  4262. if (isError(value)) {
  4263. base = ' ' + formatError(value);
  4264. }
  4265. if (keys.length === 0 && (!array || value.length == 0)) {
  4266. return braces[0] + base + braces[1];
  4267. }
  4268. if (recurseTimes < 0) {
  4269. if (isRegExp(value)) {
  4270. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  4271. } else {
  4272. return ctx.stylize('[Object]', 'special');
  4273. }
  4274. }
  4275. ctx.seen.push(value);
  4276. var output;
  4277. if (array) {
  4278. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  4279. } else {
  4280. output = keys.map(function(key) {
  4281. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  4282. });
  4283. }
  4284. ctx.seen.pop();
  4285. return reduceToSingleString(output, base, braces);
  4286. }
  4287. function formatPrimitive(ctx, value) {
  4288. if (isUndefined(value))
  4289. return ctx.stylize('undefined', 'undefined');
  4290. if (isString(value)) {
  4291. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  4292. .replace(/'/g, "\\'")
  4293. .replace(/\\"/g, '"') + '\'';
  4294. return ctx.stylize(simple, 'string');
  4295. }
  4296. if (isNumber(value))
  4297. return ctx.stylize('' + value, 'number');
  4298. if (isBoolean(value))
  4299. return ctx.stylize('' + value, 'boolean');
  4300. // For some reason typeof null is "object", so special case here.
  4301. if (isNull(value))
  4302. return ctx.stylize('null', 'null');
  4303. }
  4304. function formatError(value) {
  4305. return '[' + Error.prototype.toString.call(value) + ']';
  4306. }
  4307. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  4308. var output = [];
  4309. for (var i = 0, l = value.length; i < l; ++i) {
  4310. if (hasOwnProperty(value, String(i))) {
  4311. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  4312. String(i), true));
  4313. } else {
  4314. output.push('');
  4315. }
  4316. }
  4317. keys.forEach(function(key) {
  4318. if (!key.match(/^\d+$/)) {
  4319. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  4320. key, true));
  4321. }
  4322. });
  4323. return output;
  4324. }
  4325. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  4326. var name, str, desc;
  4327. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  4328. if (desc.get) {
  4329. if (desc.set) {
  4330. str = ctx.stylize('[Getter/Setter]', 'special');
  4331. } else {
  4332. str = ctx.stylize('[Getter]', 'special');
  4333. }
  4334. } else {
  4335. if (desc.set) {
  4336. str = ctx.stylize('[Setter]', 'special');
  4337. }
  4338. }
  4339. if (!hasOwnProperty(visibleKeys, key)) {
  4340. name = '[' + key + ']';
  4341. }
  4342. if (!str) {
  4343. if (ctx.seen.indexOf(desc.value) < 0) {
  4344. if (isNull(recurseTimes)) {
  4345. str = formatValue(ctx, desc.value, null);
  4346. } else {
  4347. str = formatValue(ctx, desc.value, recurseTimes - 1);
  4348. }
  4349. if (str.indexOf('\n') > -1) {
  4350. if (array) {
  4351. str = str.split('\n').map(function(line) {
  4352. return ' ' + line;
  4353. }).join('\n').substr(2);
  4354. } else {
  4355. str = '\n' + str.split('\n').map(function(line) {
  4356. return ' ' + line;
  4357. }).join('\n');
  4358. }
  4359. }
  4360. } else {
  4361. str = ctx.stylize('[Circular]', 'special');
  4362. }
  4363. }
  4364. if (isUndefined(name)) {
  4365. if (array && key.match(/^\d+$/)) {
  4366. return str;
  4367. }
  4368. name = JSON.stringify('' + key);
  4369. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  4370. name = name.substr(1, name.length - 2);
  4371. name = ctx.stylize(name, 'name');
  4372. } else {
  4373. name = name.replace(/'/g, "\\'")
  4374. .replace(/\\"/g, '"')
  4375. .replace(/(^"|"$)/g, "'");
  4376. name = ctx.stylize(name, 'string');
  4377. }
  4378. }
  4379. return name + ': ' + str;
  4380. }
  4381. function reduceToSingleString(output, base, braces) {
  4382. var numLinesEst = 0;
  4383. var length = output.reduce(function(prev, cur) {
  4384. numLinesEst++;
  4385. if (cur.indexOf('\n') >= 0) numLinesEst++;
  4386. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  4387. }, 0);
  4388. if (length > 60) {
  4389. return braces[0] +
  4390. (base === '' ? '' : base + '\n ') +
  4391. ' ' +
  4392. output.join(',\n ') +
  4393. ' ' +
  4394. braces[1];
  4395. }
  4396. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  4397. }
  4398. // NOTE: These type checking functions intentionally don't use `instanceof`
  4399. // because it is fragile and can be easily faked with `Object.create()`.
  4400. function isArray(ar) {
  4401. return Array.isArray(ar);
  4402. }
  4403. exports.isArray = isArray;
  4404. function isBoolean(arg) {
  4405. return typeof arg === 'boolean';
  4406. }
  4407. exports.isBoolean = isBoolean;
  4408. function isNull(arg) {
  4409. return arg === null;
  4410. }
  4411. exports.isNull = isNull;
  4412. function isNullOrUndefined(arg) {
  4413. return arg == null;
  4414. }
  4415. exports.isNullOrUndefined = isNullOrUndefined;
  4416. function isNumber(arg) {
  4417. return typeof arg === 'number';
  4418. }
  4419. exports.isNumber = isNumber;
  4420. function isString(arg) {
  4421. return typeof arg === 'string';
  4422. }
  4423. exports.isString = isString;
  4424. function isSymbol(arg) {
  4425. return typeof arg === 'symbol';
  4426. }
  4427. exports.isSymbol = isSymbol;
  4428. function isUndefined(arg) {
  4429. return arg === void 0;
  4430. }
  4431. exports.isUndefined = isUndefined;
  4432. function isRegExp(re) {
  4433. return isObject(re) && objectToString(re) === '[object RegExp]';
  4434. }
  4435. exports.isRegExp = isRegExp;
  4436. function isObject(arg) {
  4437. return typeof arg === 'object' && arg !== null;
  4438. }
  4439. exports.isObject = isObject;
  4440. function isDate(d) {
  4441. return isObject(d) && objectToString(d) === '[object Date]';
  4442. }
  4443. exports.isDate = isDate;
  4444. function isError(e) {
  4445. return isObject(e) &&
  4446. (objectToString(e) === '[object Error]' || e instanceof Error);
  4447. }
  4448. exports.isError = isError;
  4449. function isFunction(arg) {
  4450. return typeof arg === 'function';
  4451. }
  4452. exports.isFunction = isFunction;
  4453. function isPrimitive(arg) {
  4454. return arg === null ||
  4455. typeof arg === 'boolean' ||
  4456. typeof arg === 'number' ||
  4457. typeof arg === 'string' ||
  4458. typeof arg === 'symbol' || // ES6 symbol
  4459. typeof arg === 'undefined';
  4460. }
  4461. exports.isPrimitive = isPrimitive;
  4462. exports.isBuffer = require('./support/isBuffer');
  4463. function objectToString(o) {
  4464. return Object.prototype.toString.call(o);
  4465. }
  4466. function pad(n) {
  4467. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  4468. }
  4469. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  4470. 'Oct', 'Nov', 'Dec'];
  4471. // 26 Feb 16:19:34
  4472. function timestamp() {
  4473. var d = new Date();
  4474. var time = [pad(d.getHours()),
  4475. pad(d.getMinutes()),
  4476. pad(d.getSeconds())].join(':');
  4477. return [d.getDate(), months[d.getMonth()], time].join(' ');
  4478. }
  4479. // log is just a thin wrapper to console.log that prepends a timestamp
  4480. exports.log = function() {
  4481. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  4482. };
  4483. /**
  4484. * Inherit the prototype methods from one constructor into another.
  4485. *
  4486. * The Function.prototype.inherits from lang.js rewritten as a standalone
  4487. * function (not on Function.prototype). NOTE: If this file is to be loaded
  4488. * during bootstrapping this function needs to be rewritten using some native
  4489. * functions as prototype setup using normal JavaScript does not work as
  4490. * expected during bootstrapping (see mirror.js in r114903).
  4491. *
  4492. * @param {function} ctor Constructor function which needs to inherit the
  4493. * prototype.
  4494. * @param {function} superCtor Constructor function to inherit prototype from.
  4495. */
  4496. exports.inherits = require('inherits');
  4497. exports._extend = function(origin, add) {
  4498. // Don't do anything if add isn't an object
  4499. if (!add || !isObject(add)) return origin;
  4500. var keys = Object.keys(add);
  4501. var i = keys.length;
  4502. while (i--) {
  4503. origin[keys[i]] = add[keys[i]];
  4504. }
  4505. return origin;
  4506. };
  4507. function hasOwnProperty(obj, prop) {
  4508. return Object.prototype.hasOwnProperty.call(obj, prop);
  4509. }
  4510. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  4511. },{"./support/isBuffer":64,"_process":50,"inherits":48}]},{},[1]);