blob-stream.js 131 KB

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