index.js 265 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.csv_parse = {}));
  5. })(this, (function (exports) { 'use strict';
  6. var global$1 = (typeof global !== "undefined" ? global :
  7. typeof self !== "undefined" ? self :
  8. typeof window !== "undefined" ? window : {});
  9. var lookup = [];
  10. var revLookup = [];
  11. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
  12. var inited = false;
  13. function init () {
  14. inited = true;
  15. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  16. for (var i = 0, len = code.length; i < len; ++i) {
  17. lookup[i] = code[i];
  18. revLookup[code.charCodeAt(i)] = i;
  19. }
  20. revLookup['-'.charCodeAt(0)] = 62;
  21. revLookup['_'.charCodeAt(0)] = 63;
  22. }
  23. function toByteArray (b64) {
  24. if (!inited) {
  25. init();
  26. }
  27. var i, j, l, tmp, placeHolders, arr;
  28. var len = b64.length;
  29. if (len % 4 > 0) {
  30. throw new Error('Invalid string. Length must be a multiple of 4')
  31. }
  32. // the number of equal signs (place holders)
  33. // if there are two placeholders, than the two characters before it
  34. // represent one byte
  35. // if there is only one, then the three characters before it represent 2 bytes
  36. // this is just a cheap hack to not do indexOf twice
  37. placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0;
  38. // base64 is 4/3 + up to two characters of the original data
  39. arr = new Arr(len * 3 / 4 - placeHolders);
  40. // if there are placeholders, only get up to the last complete 4 chars
  41. l = placeHolders > 0 ? len - 4 : len;
  42. var L = 0;
  43. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  44. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)];
  45. arr[L++] = (tmp >> 16) & 0xFF;
  46. arr[L++] = (tmp >> 8) & 0xFF;
  47. arr[L++] = tmp & 0xFF;
  48. }
  49. if (placeHolders === 2) {
  50. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
  51. arr[L++] = tmp & 0xFF;
  52. } else if (placeHolders === 1) {
  53. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2);
  54. arr[L++] = (tmp >> 8) & 0xFF;
  55. arr[L++] = tmp & 0xFF;
  56. }
  57. return arr
  58. }
  59. function tripletToBase64 (num) {
  60. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  61. }
  62. function encodeChunk (uint8, start, end) {
  63. var tmp;
  64. var output = [];
  65. for (var i = start; i < end; i += 3) {
  66. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
  67. output.push(tripletToBase64(tmp));
  68. }
  69. return output.join('')
  70. }
  71. function fromByteArray (uint8) {
  72. if (!inited) {
  73. init();
  74. }
  75. var tmp;
  76. var len = uint8.length;
  77. var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
  78. var output = '';
  79. var parts = [];
  80. var maxChunkLength = 16383; // must be multiple of 3
  81. // go through the array every three bytes, we'll deal with trailing stuff later
  82. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  83. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
  84. }
  85. // pad the end with zeros, but make sure to not forget the extra bytes
  86. if (extraBytes === 1) {
  87. tmp = uint8[len - 1];
  88. output += lookup[tmp >> 2];
  89. output += lookup[(tmp << 4) & 0x3F];
  90. output += '==';
  91. } else if (extraBytes === 2) {
  92. tmp = (uint8[len - 2] << 8) + (uint8[len - 1]);
  93. output += lookup[tmp >> 10];
  94. output += lookup[(tmp >> 4) & 0x3F];
  95. output += lookup[(tmp << 2) & 0x3F];
  96. output += '=';
  97. }
  98. parts.push(output);
  99. return parts.join('')
  100. }
  101. function read (buffer, offset, isLE, mLen, nBytes) {
  102. var e, m;
  103. var eLen = nBytes * 8 - mLen - 1;
  104. var eMax = (1 << eLen) - 1;
  105. var eBias = eMax >> 1;
  106. var nBits = -7;
  107. var i = isLE ? (nBytes - 1) : 0;
  108. var d = isLE ? -1 : 1;
  109. var s = buffer[offset + i];
  110. i += d;
  111. e = s & ((1 << (-nBits)) - 1);
  112. s >>= (-nBits);
  113. nBits += eLen;
  114. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  115. m = e & ((1 << (-nBits)) - 1);
  116. e >>= (-nBits);
  117. nBits += mLen;
  118. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  119. if (e === 0) {
  120. e = 1 - eBias;
  121. } else if (e === eMax) {
  122. return m ? NaN : ((s ? -1 : 1) * Infinity)
  123. } else {
  124. m = m + Math.pow(2, mLen);
  125. e = e - eBias;
  126. }
  127. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  128. }
  129. function write (buffer, value, offset, isLE, mLen, nBytes) {
  130. var e, m, c;
  131. var eLen = nBytes * 8 - mLen - 1;
  132. var eMax = (1 << eLen) - 1;
  133. var eBias = eMax >> 1;
  134. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
  135. var i = isLE ? 0 : (nBytes - 1);
  136. var d = isLE ? 1 : -1;
  137. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  138. value = Math.abs(value);
  139. if (isNaN(value) || value === Infinity) {
  140. m = isNaN(value) ? 1 : 0;
  141. e = eMax;
  142. } else {
  143. e = Math.floor(Math.log(value) / Math.LN2);
  144. if (value * (c = Math.pow(2, -e)) < 1) {
  145. e--;
  146. c *= 2;
  147. }
  148. if (e + eBias >= 1) {
  149. value += rt / c;
  150. } else {
  151. value += rt * Math.pow(2, 1 - eBias);
  152. }
  153. if (value * c >= 2) {
  154. e++;
  155. c /= 2;
  156. }
  157. if (e + eBias >= eMax) {
  158. m = 0;
  159. e = eMax;
  160. } else if (e + eBias >= 1) {
  161. m = (value * c - 1) * Math.pow(2, mLen);
  162. e = e + eBias;
  163. } else {
  164. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  165. e = 0;
  166. }
  167. }
  168. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  169. e = (e << mLen) | m;
  170. eLen += mLen;
  171. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  172. buffer[offset + i - d] |= s * 128;
  173. }
  174. var toString = {}.toString;
  175. var isArray$1 = Array.isArray || function (arr) {
  176. return toString.call(arr) == '[object Array]';
  177. };
  178. var INSPECT_MAX_BYTES = 50;
  179. /**
  180. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  181. * === true Use Uint8Array implementation (fastest)
  182. * === false Use Object implementation (most compatible, even IE6)
  183. *
  184. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  185. * Opera 11.6+, iOS 4.2+.
  186. *
  187. * Due to various browser bugs, sometimes the Object implementation will be used even
  188. * when the browser supports typed arrays.
  189. *
  190. * Note:
  191. *
  192. * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
  193. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  194. *
  195. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  196. *
  197. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  198. * incorrect length in some situations.
  199. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
  200. * get the Object implementation, which is slower but behaves correctly.
  201. */
  202. Buffer.TYPED_ARRAY_SUPPORT = global$1.TYPED_ARRAY_SUPPORT !== undefined
  203. ? global$1.TYPED_ARRAY_SUPPORT
  204. : true;
  205. /*
  206. * Export kMaxLength after typed array support is determined.
  207. */
  208. kMaxLength();
  209. function kMaxLength () {
  210. return Buffer.TYPED_ARRAY_SUPPORT
  211. ? 0x7fffffff
  212. : 0x3fffffff
  213. }
  214. function createBuffer (that, length) {
  215. if (kMaxLength() < length) {
  216. throw new RangeError('Invalid typed array length')
  217. }
  218. if (Buffer.TYPED_ARRAY_SUPPORT) {
  219. // Return an augmented `Uint8Array` instance, for best performance
  220. that = new Uint8Array(length);
  221. that.__proto__ = Buffer.prototype;
  222. } else {
  223. // Fallback: Return an object instance of the Buffer class
  224. if (that === null) {
  225. that = new Buffer(length);
  226. }
  227. that.length = length;
  228. }
  229. return that
  230. }
  231. /**
  232. * The Buffer constructor returns instances of `Uint8Array` that have their
  233. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  234. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  235. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  236. * returns a single octet.
  237. *
  238. * The `Uint8Array` prototype remains unmodified.
  239. */
  240. function Buffer (arg, encodingOrOffset, length) {
  241. if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
  242. return new Buffer(arg, encodingOrOffset, length)
  243. }
  244. // Common case.
  245. if (typeof arg === 'number') {
  246. if (typeof encodingOrOffset === 'string') {
  247. throw new Error(
  248. 'If encoding is specified then the first argument must be a string'
  249. )
  250. }
  251. return allocUnsafe(this, arg)
  252. }
  253. return from(this, arg, encodingOrOffset, length)
  254. }
  255. Buffer.poolSize = 8192; // not used by this implementation
  256. // TODO: Legacy, not needed anymore. Remove in next major version.
  257. Buffer._augment = function (arr) {
  258. arr.__proto__ = Buffer.prototype;
  259. return arr
  260. };
  261. function from (that, value, encodingOrOffset, length) {
  262. if (typeof value === 'number') {
  263. throw new TypeError('"value" argument must not be a number')
  264. }
  265. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  266. return fromArrayBuffer(that, value, encodingOrOffset, length)
  267. }
  268. if (typeof value === 'string') {
  269. return fromString(that, value, encodingOrOffset)
  270. }
  271. return fromObject(that, value)
  272. }
  273. /**
  274. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  275. * if value is a number.
  276. * Buffer.from(str[, encoding])
  277. * Buffer.from(array)
  278. * Buffer.from(buffer)
  279. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  280. **/
  281. Buffer.from = function (value, encodingOrOffset, length) {
  282. return from(null, value, encodingOrOffset, length)
  283. };
  284. if (Buffer.TYPED_ARRAY_SUPPORT) {
  285. Buffer.prototype.__proto__ = Uint8Array.prototype;
  286. Buffer.__proto__ = Uint8Array;
  287. if (typeof Symbol !== 'undefined' && Symbol.species &&
  288. Buffer[Symbol.species] === Buffer) ;
  289. }
  290. function assertSize (size) {
  291. if (typeof size !== 'number') {
  292. throw new TypeError('"size" argument must be a number')
  293. } else if (size < 0) {
  294. throw new RangeError('"size" argument must not be negative')
  295. }
  296. }
  297. function alloc (that, size, fill, encoding) {
  298. assertSize(size);
  299. if (size <= 0) {
  300. return createBuffer(that, size)
  301. }
  302. if (fill !== undefined) {
  303. // Only pay attention to encoding if it's a string. This
  304. // prevents accidentally sending in a number that would
  305. // be interpretted as a start offset.
  306. return typeof encoding === 'string'
  307. ? createBuffer(that, size).fill(fill, encoding)
  308. : createBuffer(that, size).fill(fill)
  309. }
  310. return createBuffer(that, size)
  311. }
  312. /**
  313. * Creates a new filled Buffer instance.
  314. * alloc(size[, fill[, encoding]])
  315. **/
  316. Buffer.alloc = function (size, fill, encoding) {
  317. return alloc(null, size, fill, encoding)
  318. };
  319. function allocUnsafe (that, size) {
  320. assertSize(size);
  321. that = createBuffer(that, size < 0 ? 0 : checked(size) | 0);
  322. if (!Buffer.TYPED_ARRAY_SUPPORT) {
  323. for (var i = 0; i < size; ++i) {
  324. that[i] = 0;
  325. }
  326. }
  327. return that
  328. }
  329. /**
  330. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  331. * */
  332. Buffer.allocUnsafe = function (size) {
  333. return allocUnsafe(null, size)
  334. };
  335. /**
  336. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  337. */
  338. Buffer.allocUnsafeSlow = function (size) {
  339. return allocUnsafe(null, size)
  340. };
  341. function fromString (that, string, encoding) {
  342. if (typeof encoding !== 'string' || encoding === '') {
  343. encoding = 'utf8';
  344. }
  345. if (!Buffer.isEncoding(encoding)) {
  346. throw new TypeError('"encoding" must be a valid string encoding')
  347. }
  348. var length = byteLength(string, encoding) | 0;
  349. that = createBuffer(that, length);
  350. var actual = that.write(string, encoding);
  351. if (actual !== length) {
  352. // Writing a hex string, for example, that contains invalid characters will
  353. // cause everything after the first invalid character to be ignored. (e.g.
  354. // 'abxxcd' will be treated as 'ab')
  355. that = that.slice(0, actual);
  356. }
  357. return that
  358. }
  359. function fromArrayLike (that, array) {
  360. var length = array.length < 0 ? 0 : checked(array.length) | 0;
  361. that = createBuffer(that, length);
  362. for (var i = 0; i < length; i += 1) {
  363. that[i] = array[i] & 255;
  364. }
  365. return that
  366. }
  367. function fromArrayBuffer (that, array, byteOffset, length) {
  368. array.byteLength; // this throws if `array` is not a valid ArrayBuffer
  369. if (byteOffset < 0 || array.byteLength < byteOffset) {
  370. throw new RangeError('\'offset\' is out of bounds')
  371. }
  372. if (array.byteLength < byteOffset + (length || 0)) {
  373. throw new RangeError('\'length\' is out of bounds')
  374. }
  375. if (byteOffset === undefined && length === undefined) {
  376. array = new Uint8Array(array);
  377. } else if (length === undefined) {
  378. array = new Uint8Array(array, byteOffset);
  379. } else {
  380. array = new Uint8Array(array, byteOffset, length);
  381. }
  382. if (Buffer.TYPED_ARRAY_SUPPORT) {
  383. // Return an augmented `Uint8Array` instance, for best performance
  384. that = array;
  385. that.__proto__ = Buffer.prototype;
  386. } else {
  387. // Fallback: Return an object instance of the Buffer class
  388. that = fromArrayLike(that, array);
  389. }
  390. return that
  391. }
  392. function fromObject (that, obj) {
  393. if (internalIsBuffer(obj)) {
  394. var len = checked(obj.length) | 0;
  395. that = createBuffer(that, len);
  396. if (that.length === 0) {
  397. return that
  398. }
  399. obj.copy(that, 0, 0, len);
  400. return that
  401. }
  402. if (obj) {
  403. if ((typeof ArrayBuffer !== 'undefined' &&
  404. obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
  405. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  406. return createBuffer(that, 0)
  407. }
  408. return fromArrayLike(that, obj)
  409. }
  410. if (obj.type === 'Buffer' && isArray$1(obj.data)) {
  411. return fromArrayLike(that, obj.data)
  412. }
  413. }
  414. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  415. }
  416. function checked (length) {
  417. // Note: cannot use `length < kMaxLength()` here because that fails when
  418. // length is NaN (which is otherwise coerced to zero.)
  419. if (length >= kMaxLength()) {
  420. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  421. 'size: 0x' + kMaxLength().toString(16) + ' bytes')
  422. }
  423. return length | 0
  424. }
  425. Buffer.isBuffer = isBuffer;
  426. function internalIsBuffer (b) {
  427. return !!(b != null && b._isBuffer)
  428. }
  429. Buffer.compare = function compare (a, b) {
  430. if (!internalIsBuffer(a) || !internalIsBuffer(b)) {
  431. throw new TypeError('Arguments must be Buffers')
  432. }
  433. if (a === b) return 0
  434. var x = a.length;
  435. var y = b.length;
  436. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  437. if (a[i] !== b[i]) {
  438. x = a[i];
  439. y = b[i];
  440. break
  441. }
  442. }
  443. if (x < y) return -1
  444. if (y < x) return 1
  445. return 0
  446. };
  447. Buffer.isEncoding = function isEncoding (encoding) {
  448. switch (String(encoding).toLowerCase()) {
  449. case 'hex':
  450. case 'utf8':
  451. case 'utf-8':
  452. case 'ascii':
  453. case 'latin1':
  454. case 'binary':
  455. case 'base64':
  456. case 'ucs2':
  457. case 'ucs-2':
  458. case 'utf16le':
  459. case 'utf-16le':
  460. return true
  461. default:
  462. return false
  463. }
  464. };
  465. Buffer.concat = function concat (list, length) {
  466. if (!isArray$1(list)) {
  467. throw new TypeError('"list" argument must be an Array of Buffers')
  468. }
  469. if (list.length === 0) {
  470. return Buffer.alloc(0)
  471. }
  472. var i;
  473. if (length === undefined) {
  474. length = 0;
  475. for (i = 0; i < list.length; ++i) {
  476. length += list[i].length;
  477. }
  478. }
  479. var buffer = Buffer.allocUnsafe(length);
  480. var pos = 0;
  481. for (i = 0; i < list.length; ++i) {
  482. var buf = list[i];
  483. if (!internalIsBuffer(buf)) {
  484. throw new TypeError('"list" argument must be an Array of Buffers')
  485. }
  486. buf.copy(buffer, pos);
  487. pos += buf.length;
  488. }
  489. return buffer
  490. };
  491. function byteLength (string, encoding) {
  492. if (internalIsBuffer(string)) {
  493. return string.length
  494. }
  495. if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
  496. (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
  497. return string.byteLength
  498. }
  499. if (typeof string !== 'string') {
  500. string = '' + string;
  501. }
  502. var len = string.length;
  503. if (len === 0) return 0
  504. // Use a for loop to avoid recursion
  505. var loweredCase = false;
  506. for (;;) {
  507. switch (encoding) {
  508. case 'ascii':
  509. case 'latin1':
  510. case 'binary':
  511. return len
  512. case 'utf8':
  513. case 'utf-8':
  514. case undefined:
  515. return utf8ToBytes(string).length
  516. case 'ucs2':
  517. case 'ucs-2':
  518. case 'utf16le':
  519. case 'utf-16le':
  520. return len * 2
  521. case 'hex':
  522. return len >>> 1
  523. case 'base64':
  524. return base64ToBytes(string).length
  525. default:
  526. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  527. encoding = ('' + encoding).toLowerCase();
  528. loweredCase = true;
  529. }
  530. }
  531. }
  532. Buffer.byteLength = byteLength;
  533. function slowToString (encoding, start, end) {
  534. var loweredCase = false;
  535. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  536. // property of a typed array.
  537. // This behaves neither like String nor Uint8Array in that we set start/end
  538. // to their upper/lower bounds if the value passed is out of range.
  539. // undefined is handled specially as per ECMA-262 6th Edition,
  540. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  541. if (start === undefined || start < 0) {
  542. start = 0;
  543. }
  544. // Return early if start > this.length. Done here to prevent potential uint32
  545. // coercion fail below.
  546. if (start > this.length) {
  547. return ''
  548. }
  549. if (end === undefined || end > this.length) {
  550. end = this.length;
  551. }
  552. if (end <= 0) {
  553. return ''
  554. }
  555. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  556. end >>>= 0;
  557. start >>>= 0;
  558. if (end <= start) {
  559. return ''
  560. }
  561. if (!encoding) encoding = 'utf8';
  562. while (true) {
  563. switch (encoding) {
  564. case 'hex':
  565. return hexSlice(this, start, end)
  566. case 'utf8':
  567. case 'utf-8':
  568. return utf8Slice(this, start, end)
  569. case 'ascii':
  570. return asciiSlice(this, start, end)
  571. case 'latin1':
  572. case 'binary':
  573. return latin1Slice(this, start, end)
  574. case 'base64':
  575. return base64Slice(this, start, end)
  576. case 'ucs2':
  577. case 'ucs-2':
  578. case 'utf16le':
  579. case 'utf-16le':
  580. return utf16leSlice(this, start, end)
  581. default:
  582. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  583. encoding = (encoding + '').toLowerCase();
  584. loweredCase = true;
  585. }
  586. }
  587. }
  588. // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
  589. // Buffer instances.
  590. Buffer.prototype._isBuffer = true;
  591. function swap (b, n, m) {
  592. var i = b[n];
  593. b[n] = b[m];
  594. b[m] = i;
  595. }
  596. Buffer.prototype.swap16 = function swap16 () {
  597. var len = this.length;
  598. if (len % 2 !== 0) {
  599. throw new RangeError('Buffer size must be a multiple of 16-bits')
  600. }
  601. for (var i = 0; i < len; i += 2) {
  602. swap(this, i, i + 1);
  603. }
  604. return this
  605. };
  606. Buffer.prototype.swap32 = function swap32 () {
  607. var len = this.length;
  608. if (len % 4 !== 0) {
  609. throw new RangeError('Buffer size must be a multiple of 32-bits')
  610. }
  611. for (var i = 0; i < len; i += 4) {
  612. swap(this, i, i + 3);
  613. swap(this, i + 1, i + 2);
  614. }
  615. return this
  616. };
  617. Buffer.prototype.swap64 = function swap64 () {
  618. var len = this.length;
  619. if (len % 8 !== 0) {
  620. throw new RangeError('Buffer size must be a multiple of 64-bits')
  621. }
  622. for (var i = 0; i < len; i += 8) {
  623. swap(this, i, i + 7);
  624. swap(this, i + 1, i + 6);
  625. swap(this, i + 2, i + 5);
  626. swap(this, i + 3, i + 4);
  627. }
  628. return this
  629. };
  630. Buffer.prototype.toString = function toString () {
  631. var length = this.length | 0;
  632. if (length === 0) return ''
  633. if (arguments.length === 0) return utf8Slice(this, 0, length)
  634. return slowToString.apply(this, arguments)
  635. };
  636. Buffer.prototype.equals = function equals (b) {
  637. if (!internalIsBuffer(b)) throw new TypeError('Argument must be a Buffer')
  638. if (this === b) return true
  639. return Buffer.compare(this, b) === 0
  640. };
  641. Buffer.prototype.inspect = function inspect () {
  642. var str = '';
  643. var max = INSPECT_MAX_BYTES;
  644. if (this.length > 0) {
  645. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
  646. if (this.length > max) str += ' ... ';
  647. }
  648. return '<Buffer ' + str + '>'
  649. };
  650. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  651. if (!internalIsBuffer(target)) {
  652. throw new TypeError('Argument must be a Buffer')
  653. }
  654. if (start === undefined) {
  655. start = 0;
  656. }
  657. if (end === undefined) {
  658. end = target ? target.length : 0;
  659. }
  660. if (thisStart === undefined) {
  661. thisStart = 0;
  662. }
  663. if (thisEnd === undefined) {
  664. thisEnd = this.length;
  665. }
  666. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  667. throw new RangeError('out of range index')
  668. }
  669. if (thisStart >= thisEnd && start >= end) {
  670. return 0
  671. }
  672. if (thisStart >= thisEnd) {
  673. return -1
  674. }
  675. if (start >= end) {
  676. return 1
  677. }
  678. start >>>= 0;
  679. end >>>= 0;
  680. thisStart >>>= 0;
  681. thisEnd >>>= 0;
  682. if (this === target) return 0
  683. var x = thisEnd - thisStart;
  684. var y = end - start;
  685. var len = Math.min(x, y);
  686. var thisCopy = this.slice(thisStart, thisEnd);
  687. var targetCopy = target.slice(start, end);
  688. for (var i = 0; i < len; ++i) {
  689. if (thisCopy[i] !== targetCopy[i]) {
  690. x = thisCopy[i];
  691. y = targetCopy[i];
  692. break
  693. }
  694. }
  695. if (x < y) return -1
  696. if (y < x) return 1
  697. return 0
  698. };
  699. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  700. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  701. //
  702. // Arguments:
  703. // - buffer - a Buffer to search
  704. // - val - a string, Buffer, or number
  705. // - byteOffset - an index into `buffer`; will be clamped to an int32
  706. // - encoding - an optional encoding, relevant is val is a string
  707. // - dir - true for indexOf, false for lastIndexOf
  708. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  709. // Empty buffer means no match
  710. if (buffer.length === 0) return -1
  711. // Normalize byteOffset
  712. if (typeof byteOffset === 'string') {
  713. encoding = byteOffset;
  714. byteOffset = 0;
  715. } else if (byteOffset > 0x7fffffff) {
  716. byteOffset = 0x7fffffff;
  717. } else if (byteOffset < -0x80000000) {
  718. byteOffset = -0x80000000;
  719. }
  720. byteOffset = +byteOffset; // Coerce to Number.
  721. if (isNaN(byteOffset)) {
  722. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  723. byteOffset = dir ? 0 : (buffer.length - 1);
  724. }
  725. // Normalize byteOffset: negative offsets start from the end of the buffer
  726. if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
  727. if (byteOffset >= buffer.length) {
  728. if (dir) return -1
  729. else byteOffset = buffer.length - 1;
  730. } else if (byteOffset < 0) {
  731. if (dir) byteOffset = 0;
  732. else return -1
  733. }
  734. // Normalize val
  735. if (typeof val === 'string') {
  736. val = Buffer.from(val, encoding);
  737. }
  738. // Finally, search either indexOf (if dir is true) or lastIndexOf
  739. if (internalIsBuffer(val)) {
  740. // Special case: looking for empty string/buffer always fails
  741. if (val.length === 0) {
  742. return -1
  743. }
  744. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  745. } else if (typeof val === 'number') {
  746. val = val & 0xFF; // Search for a byte value [0-255]
  747. if (Buffer.TYPED_ARRAY_SUPPORT &&
  748. typeof Uint8Array.prototype.indexOf === 'function') {
  749. if (dir) {
  750. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  751. } else {
  752. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  753. }
  754. }
  755. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  756. }
  757. throw new TypeError('val must be string, number or Buffer')
  758. }
  759. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  760. var indexSize = 1;
  761. var arrLength = arr.length;
  762. var valLength = val.length;
  763. if (encoding !== undefined) {
  764. encoding = String(encoding).toLowerCase();
  765. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  766. encoding === 'utf16le' || encoding === 'utf-16le') {
  767. if (arr.length < 2 || val.length < 2) {
  768. return -1
  769. }
  770. indexSize = 2;
  771. arrLength /= 2;
  772. valLength /= 2;
  773. byteOffset /= 2;
  774. }
  775. }
  776. function read (buf, i) {
  777. if (indexSize === 1) {
  778. return buf[i]
  779. } else {
  780. return buf.readUInt16BE(i * indexSize)
  781. }
  782. }
  783. var i;
  784. if (dir) {
  785. var foundIndex = -1;
  786. for (i = byteOffset; i < arrLength; i++) {
  787. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  788. if (foundIndex === -1) foundIndex = i;
  789. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  790. } else {
  791. if (foundIndex !== -1) i -= i - foundIndex;
  792. foundIndex = -1;
  793. }
  794. }
  795. } else {
  796. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
  797. for (i = byteOffset; i >= 0; i--) {
  798. var found = true;
  799. for (var j = 0; j < valLength; j++) {
  800. if (read(arr, i + j) !== read(val, j)) {
  801. found = false;
  802. break
  803. }
  804. }
  805. if (found) return i
  806. }
  807. }
  808. return -1
  809. }
  810. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  811. return this.indexOf(val, byteOffset, encoding) !== -1
  812. };
  813. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  814. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  815. };
  816. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  817. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  818. };
  819. function hexWrite (buf, string, offset, length) {
  820. offset = Number(offset) || 0;
  821. var remaining = buf.length - offset;
  822. if (!length) {
  823. length = remaining;
  824. } else {
  825. length = Number(length);
  826. if (length > remaining) {
  827. length = remaining;
  828. }
  829. }
  830. // must be an even number of digits
  831. var strLen = string.length;
  832. if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
  833. if (length > strLen / 2) {
  834. length = strLen / 2;
  835. }
  836. for (var i = 0; i < length; ++i) {
  837. var parsed = parseInt(string.substr(i * 2, 2), 16);
  838. if (isNaN(parsed)) return i
  839. buf[offset + i] = parsed;
  840. }
  841. return i
  842. }
  843. function utf8Write (buf, string, offset, length) {
  844. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  845. }
  846. function asciiWrite (buf, string, offset, length) {
  847. return blitBuffer(asciiToBytes(string), buf, offset, length)
  848. }
  849. function latin1Write (buf, string, offset, length) {
  850. return asciiWrite(buf, string, offset, length)
  851. }
  852. function base64Write (buf, string, offset, length) {
  853. return blitBuffer(base64ToBytes(string), buf, offset, length)
  854. }
  855. function ucs2Write (buf, string, offset, length) {
  856. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  857. }
  858. Buffer.prototype.write = function write (string, offset, length, encoding) {
  859. // Buffer#write(string)
  860. if (offset === undefined) {
  861. encoding = 'utf8';
  862. length = this.length;
  863. offset = 0;
  864. // Buffer#write(string, encoding)
  865. } else if (length === undefined && typeof offset === 'string') {
  866. encoding = offset;
  867. length = this.length;
  868. offset = 0;
  869. // Buffer#write(string, offset[, length][, encoding])
  870. } else if (isFinite(offset)) {
  871. offset = offset | 0;
  872. if (isFinite(length)) {
  873. length = length | 0;
  874. if (encoding === undefined) encoding = 'utf8';
  875. } else {
  876. encoding = length;
  877. length = undefined;
  878. }
  879. // legacy write(string, encoding, offset, length) - remove in v0.13
  880. } else {
  881. throw new Error(
  882. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  883. )
  884. }
  885. var remaining = this.length - offset;
  886. if (length === undefined || length > remaining) length = remaining;
  887. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  888. throw new RangeError('Attempt to write outside buffer bounds')
  889. }
  890. if (!encoding) encoding = 'utf8';
  891. var loweredCase = false;
  892. for (;;) {
  893. switch (encoding) {
  894. case 'hex':
  895. return hexWrite(this, string, offset, length)
  896. case 'utf8':
  897. case 'utf-8':
  898. return utf8Write(this, string, offset, length)
  899. case 'ascii':
  900. return asciiWrite(this, string, offset, length)
  901. case 'latin1':
  902. case 'binary':
  903. return latin1Write(this, string, offset, length)
  904. case 'base64':
  905. // Warning: maxLength not taken into account in base64Write
  906. return base64Write(this, string, offset, length)
  907. case 'ucs2':
  908. case 'ucs-2':
  909. case 'utf16le':
  910. case 'utf-16le':
  911. return ucs2Write(this, string, offset, length)
  912. default:
  913. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  914. encoding = ('' + encoding).toLowerCase();
  915. loweredCase = true;
  916. }
  917. }
  918. };
  919. Buffer.prototype.toJSON = function toJSON () {
  920. return {
  921. type: 'Buffer',
  922. data: Array.prototype.slice.call(this._arr || this, 0)
  923. }
  924. };
  925. function base64Slice (buf, start, end) {
  926. if (start === 0 && end === buf.length) {
  927. return fromByteArray(buf)
  928. } else {
  929. return fromByteArray(buf.slice(start, end))
  930. }
  931. }
  932. function utf8Slice (buf, start, end) {
  933. end = Math.min(buf.length, end);
  934. var res = [];
  935. var i = start;
  936. while (i < end) {
  937. var firstByte = buf[i];
  938. var codePoint = null;
  939. var bytesPerSequence = (firstByte > 0xEF) ? 4
  940. : (firstByte > 0xDF) ? 3
  941. : (firstByte > 0xBF) ? 2
  942. : 1;
  943. if (i + bytesPerSequence <= end) {
  944. var secondByte, thirdByte, fourthByte, tempCodePoint;
  945. switch (bytesPerSequence) {
  946. case 1:
  947. if (firstByte < 0x80) {
  948. codePoint = firstByte;
  949. }
  950. break
  951. case 2:
  952. secondByte = buf[i + 1];
  953. if ((secondByte & 0xC0) === 0x80) {
  954. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
  955. if (tempCodePoint > 0x7F) {
  956. codePoint = tempCodePoint;
  957. }
  958. }
  959. break
  960. case 3:
  961. secondByte = buf[i + 1];
  962. thirdByte = buf[i + 2];
  963. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  964. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
  965. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  966. codePoint = tempCodePoint;
  967. }
  968. }
  969. break
  970. case 4:
  971. secondByte = buf[i + 1];
  972. thirdByte = buf[i + 2];
  973. fourthByte = buf[i + 3];
  974. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  975. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
  976. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  977. codePoint = tempCodePoint;
  978. }
  979. }
  980. }
  981. }
  982. if (codePoint === null) {
  983. // we did not generate a valid codePoint so insert a
  984. // replacement char (U+FFFD) and advance only 1 byte
  985. codePoint = 0xFFFD;
  986. bytesPerSequence = 1;
  987. } else if (codePoint > 0xFFFF) {
  988. // encode to utf16 (surrogate pair dance)
  989. codePoint -= 0x10000;
  990. res.push(codePoint >>> 10 & 0x3FF | 0xD800);
  991. codePoint = 0xDC00 | codePoint & 0x3FF;
  992. }
  993. res.push(codePoint);
  994. i += bytesPerSequence;
  995. }
  996. return decodeCodePointsArray(res)
  997. }
  998. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  999. // the lowest limit is Chrome, with 0x10000 args.
  1000. // We go 1 magnitude less, for safety
  1001. var MAX_ARGUMENTS_LENGTH = 0x1000;
  1002. function decodeCodePointsArray (codePoints) {
  1003. var len = codePoints.length;
  1004. if (len <= MAX_ARGUMENTS_LENGTH) {
  1005. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  1006. }
  1007. // Decode in chunks to avoid "call stack size exceeded".
  1008. var res = '';
  1009. var i = 0;
  1010. while (i < len) {
  1011. res += String.fromCharCode.apply(
  1012. String,
  1013. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  1014. );
  1015. }
  1016. return res
  1017. }
  1018. function asciiSlice (buf, start, end) {
  1019. var ret = '';
  1020. end = Math.min(buf.length, end);
  1021. for (var i = start; i < end; ++i) {
  1022. ret += String.fromCharCode(buf[i] & 0x7F);
  1023. }
  1024. return ret
  1025. }
  1026. function latin1Slice (buf, start, end) {
  1027. var ret = '';
  1028. end = Math.min(buf.length, end);
  1029. for (var i = start; i < end; ++i) {
  1030. ret += String.fromCharCode(buf[i]);
  1031. }
  1032. return ret
  1033. }
  1034. function hexSlice (buf, start, end) {
  1035. var len = buf.length;
  1036. if (!start || start < 0) start = 0;
  1037. if (!end || end < 0 || end > len) end = len;
  1038. var out = '';
  1039. for (var i = start; i < end; ++i) {
  1040. out += toHex(buf[i]);
  1041. }
  1042. return out
  1043. }
  1044. function utf16leSlice (buf, start, end) {
  1045. var bytes = buf.slice(start, end);
  1046. var res = '';
  1047. for (var i = 0; i < bytes.length; i += 2) {
  1048. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
  1049. }
  1050. return res
  1051. }
  1052. Buffer.prototype.slice = function slice (start, end) {
  1053. var len = this.length;
  1054. start = ~~start;
  1055. end = end === undefined ? len : ~~end;
  1056. if (start < 0) {
  1057. start += len;
  1058. if (start < 0) start = 0;
  1059. } else if (start > len) {
  1060. start = len;
  1061. }
  1062. if (end < 0) {
  1063. end += len;
  1064. if (end < 0) end = 0;
  1065. } else if (end > len) {
  1066. end = len;
  1067. }
  1068. if (end < start) end = start;
  1069. var newBuf;
  1070. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1071. newBuf = this.subarray(start, end);
  1072. newBuf.__proto__ = Buffer.prototype;
  1073. } else {
  1074. var sliceLen = end - start;
  1075. newBuf = new Buffer(sliceLen, undefined);
  1076. for (var i = 0; i < sliceLen; ++i) {
  1077. newBuf[i] = this[i + start];
  1078. }
  1079. }
  1080. return newBuf
  1081. };
  1082. /*
  1083. * Need to make sure that buffer isn't trying to write out of bounds.
  1084. */
  1085. function checkOffset (offset, ext, length) {
  1086. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  1087. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  1088. }
  1089. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  1090. offset = offset | 0;
  1091. byteLength = byteLength | 0;
  1092. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1093. var val = this[offset];
  1094. var mul = 1;
  1095. var i = 0;
  1096. while (++i < byteLength && (mul *= 0x100)) {
  1097. val += this[offset + i] * mul;
  1098. }
  1099. return val
  1100. };
  1101. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  1102. offset = offset | 0;
  1103. byteLength = byteLength | 0;
  1104. if (!noAssert) {
  1105. checkOffset(offset, byteLength, this.length);
  1106. }
  1107. var val = this[offset + --byteLength];
  1108. var mul = 1;
  1109. while (byteLength > 0 && (mul *= 0x100)) {
  1110. val += this[offset + --byteLength] * mul;
  1111. }
  1112. return val
  1113. };
  1114. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  1115. if (!noAssert) checkOffset(offset, 1, this.length);
  1116. return this[offset]
  1117. };
  1118. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  1119. if (!noAssert) checkOffset(offset, 2, this.length);
  1120. return this[offset] | (this[offset + 1] << 8)
  1121. };
  1122. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  1123. if (!noAssert) checkOffset(offset, 2, this.length);
  1124. return (this[offset] << 8) | this[offset + 1]
  1125. };
  1126. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  1127. if (!noAssert) checkOffset(offset, 4, this.length);
  1128. return ((this[offset]) |
  1129. (this[offset + 1] << 8) |
  1130. (this[offset + 2] << 16)) +
  1131. (this[offset + 3] * 0x1000000)
  1132. };
  1133. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  1134. if (!noAssert) checkOffset(offset, 4, this.length);
  1135. return (this[offset] * 0x1000000) +
  1136. ((this[offset + 1] << 16) |
  1137. (this[offset + 2] << 8) |
  1138. this[offset + 3])
  1139. };
  1140. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  1141. offset = offset | 0;
  1142. byteLength = byteLength | 0;
  1143. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1144. var val = this[offset];
  1145. var mul = 1;
  1146. var i = 0;
  1147. while (++i < byteLength && (mul *= 0x100)) {
  1148. val += this[offset + i] * mul;
  1149. }
  1150. mul *= 0x80;
  1151. if (val >= mul) val -= Math.pow(2, 8 * byteLength);
  1152. return val
  1153. };
  1154. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  1155. offset = offset | 0;
  1156. byteLength = byteLength | 0;
  1157. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1158. var i = byteLength;
  1159. var mul = 1;
  1160. var val = this[offset + --i];
  1161. while (i > 0 && (mul *= 0x100)) {
  1162. val += this[offset + --i] * mul;
  1163. }
  1164. mul *= 0x80;
  1165. if (val >= mul) val -= Math.pow(2, 8 * byteLength);
  1166. return val
  1167. };
  1168. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  1169. if (!noAssert) checkOffset(offset, 1, this.length);
  1170. if (!(this[offset] & 0x80)) return (this[offset])
  1171. return ((0xff - this[offset] + 1) * -1)
  1172. };
  1173. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  1174. if (!noAssert) checkOffset(offset, 2, this.length);
  1175. var val = this[offset] | (this[offset + 1] << 8);
  1176. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1177. };
  1178. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  1179. if (!noAssert) checkOffset(offset, 2, this.length);
  1180. var val = this[offset + 1] | (this[offset] << 8);
  1181. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1182. };
  1183. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  1184. if (!noAssert) checkOffset(offset, 4, this.length);
  1185. return (this[offset]) |
  1186. (this[offset + 1] << 8) |
  1187. (this[offset + 2] << 16) |
  1188. (this[offset + 3] << 24)
  1189. };
  1190. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  1191. if (!noAssert) checkOffset(offset, 4, this.length);
  1192. return (this[offset] << 24) |
  1193. (this[offset + 1] << 16) |
  1194. (this[offset + 2] << 8) |
  1195. (this[offset + 3])
  1196. };
  1197. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  1198. if (!noAssert) checkOffset(offset, 4, this.length);
  1199. return read(this, offset, true, 23, 4)
  1200. };
  1201. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  1202. if (!noAssert) checkOffset(offset, 4, this.length);
  1203. return read(this, offset, false, 23, 4)
  1204. };
  1205. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  1206. if (!noAssert) checkOffset(offset, 8, this.length);
  1207. return read(this, offset, true, 52, 8)
  1208. };
  1209. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  1210. if (!noAssert) checkOffset(offset, 8, this.length);
  1211. return read(this, offset, false, 52, 8)
  1212. };
  1213. function checkInt (buf, value, offset, ext, max, min) {
  1214. if (!internalIsBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  1215. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  1216. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1217. }
  1218. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  1219. value = +value;
  1220. offset = offset | 0;
  1221. byteLength = byteLength | 0;
  1222. if (!noAssert) {
  1223. var maxBytes = Math.pow(2, 8 * byteLength) - 1;
  1224. checkInt(this, value, offset, byteLength, maxBytes, 0);
  1225. }
  1226. var mul = 1;
  1227. var i = 0;
  1228. this[offset] = value & 0xFF;
  1229. while (++i < byteLength && (mul *= 0x100)) {
  1230. this[offset + i] = (value / mul) & 0xFF;
  1231. }
  1232. return offset + byteLength
  1233. };
  1234. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  1235. value = +value;
  1236. offset = offset | 0;
  1237. byteLength = byteLength | 0;
  1238. if (!noAssert) {
  1239. var maxBytes = Math.pow(2, 8 * byteLength) - 1;
  1240. checkInt(this, value, offset, byteLength, maxBytes, 0);
  1241. }
  1242. var i = byteLength - 1;
  1243. var mul = 1;
  1244. this[offset + i] = value & 0xFF;
  1245. while (--i >= 0 && (mul *= 0x100)) {
  1246. this[offset + i] = (value / mul) & 0xFF;
  1247. }
  1248. return offset + byteLength
  1249. };
  1250. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  1251. value = +value;
  1252. offset = offset | 0;
  1253. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
  1254. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
  1255. this[offset] = (value & 0xff);
  1256. return offset + 1
  1257. };
  1258. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  1259. if (value < 0) value = 0xffff + value + 1;
  1260. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
  1261. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  1262. (littleEndian ? i : 1 - i) * 8;
  1263. }
  1264. }
  1265. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  1266. value = +value;
  1267. offset = offset | 0;
  1268. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
  1269. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1270. this[offset] = (value & 0xff);
  1271. this[offset + 1] = (value >>> 8);
  1272. } else {
  1273. objectWriteUInt16(this, value, offset, true);
  1274. }
  1275. return offset + 2
  1276. };
  1277. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  1278. value = +value;
  1279. offset = offset | 0;
  1280. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
  1281. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1282. this[offset] = (value >>> 8);
  1283. this[offset + 1] = (value & 0xff);
  1284. } else {
  1285. objectWriteUInt16(this, value, offset, false);
  1286. }
  1287. return offset + 2
  1288. };
  1289. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  1290. if (value < 0) value = 0xffffffff + value + 1;
  1291. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
  1292. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff;
  1293. }
  1294. }
  1295. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  1296. value = +value;
  1297. offset = offset | 0;
  1298. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
  1299. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1300. this[offset + 3] = (value >>> 24);
  1301. this[offset + 2] = (value >>> 16);
  1302. this[offset + 1] = (value >>> 8);
  1303. this[offset] = (value & 0xff);
  1304. } else {
  1305. objectWriteUInt32(this, value, offset, true);
  1306. }
  1307. return offset + 4
  1308. };
  1309. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  1310. value = +value;
  1311. offset = offset | 0;
  1312. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
  1313. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1314. this[offset] = (value >>> 24);
  1315. this[offset + 1] = (value >>> 16);
  1316. this[offset + 2] = (value >>> 8);
  1317. this[offset + 3] = (value & 0xff);
  1318. } else {
  1319. objectWriteUInt32(this, value, offset, false);
  1320. }
  1321. return offset + 4
  1322. };
  1323. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  1324. value = +value;
  1325. offset = offset | 0;
  1326. if (!noAssert) {
  1327. var limit = Math.pow(2, 8 * byteLength - 1);
  1328. checkInt(this, value, offset, byteLength, limit - 1, -limit);
  1329. }
  1330. var i = 0;
  1331. var mul = 1;
  1332. var sub = 0;
  1333. this[offset] = value & 0xFF;
  1334. while (++i < byteLength && (mul *= 0x100)) {
  1335. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  1336. sub = 1;
  1337. }
  1338. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
  1339. }
  1340. return offset + byteLength
  1341. };
  1342. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  1343. value = +value;
  1344. offset = offset | 0;
  1345. if (!noAssert) {
  1346. var limit = Math.pow(2, 8 * byteLength - 1);
  1347. checkInt(this, value, offset, byteLength, limit - 1, -limit);
  1348. }
  1349. var i = byteLength - 1;
  1350. var mul = 1;
  1351. var sub = 0;
  1352. this[offset + i] = value & 0xFF;
  1353. while (--i >= 0 && (mul *= 0x100)) {
  1354. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  1355. sub = 1;
  1356. }
  1357. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
  1358. }
  1359. return offset + byteLength
  1360. };
  1361. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  1362. value = +value;
  1363. offset = offset | 0;
  1364. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80);
  1365. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
  1366. if (value < 0) value = 0xff + value + 1;
  1367. this[offset] = (value & 0xff);
  1368. return offset + 1
  1369. };
  1370. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  1371. value = +value;
  1372. offset = offset | 0;
  1373. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
  1374. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1375. this[offset] = (value & 0xff);
  1376. this[offset + 1] = (value >>> 8);
  1377. } else {
  1378. objectWriteUInt16(this, value, offset, true);
  1379. }
  1380. return offset + 2
  1381. };
  1382. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  1383. value = +value;
  1384. offset = offset | 0;
  1385. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
  1386. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1387. this[offset] = (value >>> 8);
  1388. this[offset + 1] = (value & 0xff);
  1389. } else {
  1390. objectWriteUInt16(this, value, offset, false);
  1391. }
  1392. return offset + 2
  1393. };
  1394. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  1395. value = +value;
  1396. offset = offset | 0;
  1397. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
  1398. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1399. this[offset] = (value & 0xff);
  1400. this[offset + 1] = (value >>> 8);
  1401. this[offset + 2] = (value >>> 16);
  1402. this[offset + 3] = (value >>> 24);
  1403. } else {
  1404. objectWriteUInt32(this, value, offset, true);
  1405. }
  1406. return offset + 4
  1407. };
  1408. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  1409. value = +value;
  1410. offset = offset | 0;
  1411. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
  1412. if (value < 0) value = 0xffffffff + value + 1;
  1413. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1414. this[offset] = (value >>> 24);
  1415. this[offset + 1] = (value >>> 16);
  1416. this[offset + 2] = (value >>> 8);
  1417. this[offset + 3] = (value & 0xff);
  1418. } else {
  1419. objectWriteUInt32(this, value, offset, false);
  1420. }
  1421. return offset + 4
  1422. };
  1423. function checkIEEE754 (buf, value, offset, ext, max, min) {
  1424. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1425. if (offset < 0) throw new RangeError('Index out of range')
  1426. }
  1427. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  1428. if (!noAssert) {
  1429. checkIEEE754(buf, value, offset, 4);
  1430. }
  1431. write(buf, value, offset, littleEndian, 23, 4);
  1432. return offset + 4
  1433. }
  1434. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  1435. return writeFloat(this, value, offset, true, noAssert)
  1436. };
  1437. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  1438. return writeFloat(this, value, offset, false, noAssert)
  1439. };
  1440. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  1441. if (!noAssert) {
  1442. checkIEEE754(buf, value, offset, 8);
  1443. }
  1444. write(buf, value, offset, littleEndian, 52, 8);
  1445. return offset + 8
  1446. }
  1447. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  1448. return writeDouble(this, value, offset, true, noAssert)
  1449. };
  1450. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  1451. return writeDouble(this, value, offset, false, noAssert)
  1452. };
  1453. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  1454. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  1455. if (!start) start = 0;
  1456. if (!end && end !== 0) end = this.length;
  1457. if (targetStart >= target.length) targetStart = target.length;
  1458. if (!targetStart) targetStart = 0;
  1459. if (end > 0 && end < start) end = start;
  1460. // Copy 0 bytes; we're done
  1461. if (end === start) return 0
  1462. if (target.length === 0 || this.length === 0) return 0
  1463. // Fatal error conditions
  1464. if (targetStart < 0) {
  1465. throw new RangeError('targetStart out of bounds')
  1466. }
  1467. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  1468. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  1469. // Are we oob?
  1470. if (end > this.length) end = this.length;
  1471. if (target.length - targetStart < end - start) {
  1472. end = target.length - targetStart + start;
  1473. }
  1474. var len = end - start;
  1475. var i;
  1476. if (this === target && start < targetStart && targetStart < end) {
  1477. // descending copy from end
  1478. for (i = len - 1; i >= 0; --i) {
  1479. target[i + targetStart] = this[i + start];
  1480. }
  1481. } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
  1482. // ascending copy from start
  1483. for (i = 0; i < len; ++i) {
  1484. target[i + targetStart] = this[i + start];
  1485. }
  1486. } else {
  1487. Uint8Array.prototype.set.call(
  1488. target,
  1489. this.subarray(start, start + len),
  1490. targetStart
  1491. );
  1492. }
  1493. return len
  1494. };
  1495. // Usage:
  1496. // buffer.fill(number[, offset[, end]])
  1497. // buffer.fill(buffer[, offset[, end]])
  1498. // buffer.fill(string[, offset[, end]][, encoding])
  1499. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  1500. // Handle string cases:
  1501. if (typeof val === 'string') {
  1502. if (typeof start === 'string') {
  1503. encoding = start;
  1504. start = 0;
  1505. end = this.length;
  1506. } else if (typeof end === 'string') {
  1507. encoding = end;
  1508. end = this.length;
  1509. }
  1510. if (val.length === 1) {
  1511. var code = val.charCodeAt(0);
  1512. if (code < 256) {
  1513. val = code;
  1514. }
  1515. }
  1516. if (encoding !== undefined && typeof encoding !== 'string') {
  1517. throw new TypeError('encoding must be a string')
  1518. }
  1519. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  1520. throw new TypeError('Unknown encoding: ' + encoding)
  1521. }
  1522. } else if (typeof val === 'number') {
  1523. val = val & 255;
  1524. }
  1525. // Invalid ranges are not set to a default, so can range check early.
  1526. if (start < 0 || this.length < start || this.length < end) {
  1527. throw new RangeError('Out of range index')
  1528. }
  1529. if (end <= start) {
  1530. return this
  1531. }
  1532. start = start >>> 0;
  1533. end = end === undefined ? this.length : end >>> 0;
  1534. if (!val) val = 0;
  1535. var i;
  1536. if (typeof val === 'number') {
  1537. for (i = start; i < end; ++i) {
  1538. this[i] = val;
  1539. }
  1540. } else {
  1541. var bytes = internalIsBuffer(val)
  1542. ? val
  1543. : utf8ToBytes(new Buffer(val, encoding).toString());
  1544. var len = bytes.length;
  1545. for (i = 0; i < end - start; ++i) {
  1546. this[i + start] = bytes[i % len];
  1547. }
  1548. }
  1549. return this
  1550. };
  1551. // HELPER FUNCTIONS
  1552. // ================
  1553. var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g;
  1554. function base64clean (str) {
  1555. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  1556. str = stringtrim(str).replace(INVALID_BASE64_RE, '');
  1557. // Node converts strings with length < 2 to ''
  1558. if (str.length < 2) return ''
  1559. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  1560. while (str.length % 4 !== 0) {
  1561. str = str + '=';
  1562. }
  1563. return str
  1564. }
  1565. function stringtrim (str) {
  1566. if (str.trim) return str.trim()
  1567. return str.replace(/^\s+|\s+$/g, '')
  1568. }
  1569. function toHex (n) {
  1570. if (n < 16) return '0' + n.toString(16)
  1571. return n.toString(16)
  1572. }
  1573. function utf8ToBytes (string, units) {
  1574. units = units || Infinity;
  1575. var codePoint;
  1576. var length = string.length;
  1577. var leadSurrogate = null;
  1578. var bytes = [];
  1579. for (var i = 0; i < length; ++i) {
  1580. codePoint = string.charCodeAt(i);
  1581. // is surrogate component
  1582. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  1583. // last char was a lead
  1584. if (!leadSurrogate) {
  1585. // no lead yet
  1586. if (codePoint > 0xDBFF) {
  1587. // unexpected trail
  1588. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1589. continue
  1590. } else if (i + 1 === length) {
  1591. // unpaired lead
  1592. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1593. continue
  1594. }
  1595. // valid lead
  1596. leadSurrogate = codePoint;
  1597. continue
  1598. }
  1599. // 2 leads in a row
  1600. if (codePoint < 0xDC00) {
  1601. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1602. leadSurrogate = codePoint;
  1603. continue
  1604. }
  1605. // valid surrogate pair
  1606. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
  1607. } else if (leadSurrogate) {
  1608. // valid bmp char, but last char was a lead
  1609. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1610. }
  1611. leadSurrogate = null;
  1612. // encode utf8
  1613. if (codePoint < 0x80) {
  1614. if ((units -= 1) < 0) break
  1615. bytes.push(codePoint);
  1616. } else if (codePoint < 0x800) {
  1617. if ((units -= 2) < 0) break
  1618. bytes.push(
  1619. codePoint >> 0x6 | 0xC0,
  1620. codePoint & 0x3F | 0x80
  1621. );
  1622. } else if (codePoint < 0x10000) {
  1623. if ((units -= 3) < 0) break
  1624. bytes.push(
  1625. codePoint >> 0xC | 0xE0,
  1626. codePoint >> 0x6 & 0x3F | 0x80,
  1627. codePoint & 0x3F | 0x80
  1628. );
  1629. } else if (codePoint < 0x110000) {
  1630. if ((units -= 4) < 0) break
  1631. bytes.push(
  1632. codePoint >> 0x12 | 0xF0,
  1633. codePoint >> 0xC & 0x3F | 0x80,
  1634. codePoint >> 0x6 & 0x3F | 0x80,
  1635. codePoint & 0x3F | 0x80
  1636. );
  1637. } else {
  1638. throw new Error('Invalid code point')
  1639. }
  1640. }
  1641. return bytes
  1642. }
  1643. function asciiToBytes (str) {
  1644. var byteArray = [];
  1645. for (var i = 0; i < str.length; ++i) {
  1646. // Node's code seems to be doing this and not & 0x7F..
  1647. byteArray.push(str.charCodeAt(i) & 0xFF);
  1648. }
  1649. return byteArray
  1650. }
  1651. function utf16leToBytes (str, units) {
  1652. var c, hi, lo;
  1653. var byteArray = [];
  1654. for (var i = 0; i < str.length; ++i) {
  1655. if ((units -= 2) < 0) break
  1656. c = str.charCodeAt(i);
  1657. hi = c >> 8;
  1658. lo = c % 256;
  1659. byteArray.push(lo);
  1660. byteArray.push(hi);
  1661. }
  1662. return byteArray
  1663. }
  1664. function base64ToBytes (str) {
  1665. return toByteArray(base64clean(str))
  1666. }
  1667. function blitBuffer (src, dst, offset, length) {
  1668. for (var i = 0; i < length; ++i) {
  1669. if ((i + offset >= dst.length) || (i >= src.length)) break
  1670. dst[i + offset] = src[i];
  1671. }
  1672. return i
  1673. }
  1674. function isnan (val) {
  1675. return val !== val // eslint-disable-line no-self-compare
  1676. }
  1677. // the following is from is-buffer, also by Feross Aboukhadijeh and with same lisence
  1678. // The _isBuffer check is for Safari 5-7 support, because it's missing
  1679. // Object.prototype.constructor. Remove this eventually
  1680. function isBuffer(obj) {
  1681. return obj != null && (!!obj._isBuffer || isFastBuffer(obj) || isSlowBuffer(obj))
  1682. }
  1683. function isFastBuffer (obj) {
  1684. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  1685. }
  1686. // For Node v0.10 support. Remove this eventually.
  1687. function isSlowBuffer (obj) {
  1688. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isFastBuffer(obj.slice(0, 0))
  1689. }
  1690. var domain;
  1691. // This constructor is used to store event handlers. Instantiating this is
  1692. // faster than explicitly calling `Object.create(null)` to get a "clean" empty
  1693. // object (tested with v8 v4.9).
  1694. function EventHandlers() {}
  1695. EventHandlers.prototype = Object.create(null);
  1696. function EventEmitter() {
  1697. EventEmitter.init.call(this);
  1698. }
  1699. // nodejs oddity
  1700. // require('events') === require('events').EventEmitter
  1701. EventEmitter.EventEmitter = EventEmitter;
  1702. EventEmitter.usingDomains = false;
  1703. EventEmitter.prototype.domain = undefined;
  1704. EventEmitter.prototype._events = undefined;
  1705. EventEmitter.prototype._maxListeners = undefined;
  1706. // By default EventEmitters will print a warning if more than 10 listeners are
  1707. // added to it. This is a useful default which helps finding memory leaks.
  1708. EventEmitter.defaultMaxListeners = 10;
  1709. EventEmitter.init = function() {
  1710. this.domain = null;
  1711. if (EventEmitter.usingDomains) {
  1712. // if there is an active domain, then attach to it.
  1713. if (domain.active ) ;
  1714. }
  1715. if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
  1716. this._events = new EventHandlers();
  1717. this._eventsCount = 0;
  1718. }
  1719. this._maxListeners = this._maxListeners || undefined;
  1720. };
  1721. // Obviously not all Emitters should be limited to 10. This function allows
  1722. // that to be increased. Set to zero for unlimited.
  1723. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
  1724. if (typeof n !== 'number' || n < 0 || isNaN(n))
  1725. throw new TypeError('"n" argument must be a positive number');
  1726. this._maxListeners = n;
  1727. return this;
  1728. };
  1729. function $getMaxListeners(that) {
  1730. if (that._maxListeners === undefined)
  1731. return EventEmitter.defaultMaxListeners;
  1732. return that._maxListeners;
  1733. }
  1734. EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
  1735. return $getMaxListeners(this);
  1736. };
  1737. // These standalone emit* functions are used to optimize calling of event
  1738. // handlers for fast cases because emit() itself often has a variable number of
  1739. // arguments and can be deoptimized because of that. These functions always have
  1740. // the same number of arguments and thus do not get deoptimized, so the code
  1741. // inside them can execute faster.
  1742. function emitNone(handler, isFn, self) {
  1743. if (isFn)
  1744. handler.call(self);
  1745. else {
  1746. var len = handler.length;
  1747. var listeners = arrayClone(handler, len);
  1748. for (var i = 0; i < len; ++i)
  1749. listeners[i].call(self);
  1750. }
  1751. }
  1752. function emitOne(handler, isFn, self, arg1) {
  1753. if (isFn)
  1754. handler.call(self, arg1);
  1755. else {
  1756. var len = handler.length;
  1757. var listeners = arrayClone(handler, len);
  1758. for (var i = 0; i < len; ++i)
  1759. listeners[i].call(self, arg1);
  1760. }
  1761. }
  1762. function emitTwo(handler, isFn, self, arg1, arg2) {
  1763. if (isFn)
  1764. handler.call(self, arg1, arg2);
  1765. else {
  1766. var len = handler.length;
  1767. var listeners = arrayClone(handler, len);
  1768. for (var i = 0; i < len; ++i)
  1769. listeners[i].call(self, arg1, arg2);
  1770. }
  1771. }
  1772. function emitThree(handler, isFn, self, arg1, arg2, arg3) {
  1773. if (isFn)
  1774. handler.call(self, arg1, arg2, arg3);
  1775. else {
  1776. var len = handler.length;
  1777. var listeners = arrayClone(handler, len);
  1778. for (var i = 0; i < len; ++i)
  1779. listeners[i].call(self, arg1, arg2, arg3);
  1780. }
  1781. }
  1782. function emitMany(handler, isFn, self, args) {
  1783. if (isFn)
  1784. handler.apply(self, args);
  1785. else {
  1786. var len = handler.length;
  1787. var listeners = arrayClone(handler, len);
  1788. for (var i = 0; i < len; ++i)
  1789. listeners[i].apply(self, args);
  1790. }
  1791. }
  1792. EventEmitter.prototype.emit = function emit(type) {
  1793. var er, handler, len, args, i, events, domain;
  1794. var doError = (type === 'error');
  1795. events = this._events;
  1796. if (events)
  1797. doError = (doError && events.error == null);
  1798. else if (!doError)
  1799. return false;
  1800. domain = this.domain;
  1801. // If there is no 'error' event listener then throw.
  1802. if (doError) {
  1803. er = arguments[1];
  1804. if (domain) {
  1805. if (!er)
  1806. er = new Error('Uncaught, unspecified "error" event');
  1807. er.domainEmitter = this;
  1808. er.domain = domain;
  1809. er.domainThrown = false;
  1810. domain.emit('error', er);
  1811. } else if (er instanceof Error) {
  1812. throw er; // Unhandled 'error' event
  1813. } else {
  1814. // At least give some kind of context to the user
  1815. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  1816. err.context = er;
  1817. throw err;
  1818. }
  1819. return false;
  1820. }
  1821. handler = events[type];
  1822. if (!handler)
  1823. return false;
  1824. var isFn = typeof handler === 'function';
  1825. len = arguments.length;
  1826. switch (len) {
  1827. // fast cases
  1828. case 1:
  1829. emitNone(handler, isFn, this);
  1830. break;
  1831. case 2:
  1832. emitOne(handler, isFn, this, arguments[1]);
  1833. break;
  1834. case 3:
  1835. emitTwo(handler, isFn, this, arguments[1], arguments[2]);
  1836. break;
  1837. case 4:
  1838. emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
  1839. break;
  1840. // slower
  1841. default:
  1842. args = new Array(len - 1);
  1843. for (i = 1; i < len; i++)
  1844. args[i - 1] = arguments[i];
  1845. emitMany(handler, isFn, this, args);
  1846. }
  1847. return true;
  1848. };
  1849. function _addListener(target, type, listener, prepend) {
  1850. var m;
  1851. var events;
  1852. var existing;
  1853. if (typeof listener !== 'function')
  1854. throw new TypeError('"listener" argument must be a function');
  1855. events = target._events;
  1856. if (!events) {
  1857. events = target._events = new EventHandlers();
  1858. target._eventsCount = 0;
  1859. } else {
  1860. // To avoid recursion in the case that type === "newListener"! Before
  1861. // adding it to the listeners, first emit "newListener".
  1862. if (events.newListener) {
  1863. target.emit('newListener', type,
  1864. listener.listener ? listener.listener : listener);
  1865. // Re-assign `events` because a newListener handler could have caused the
  1866. // this._events to be assigned to a new object
  1867. events = target._events;
  1868. }
  1869. existing = events[type];
  1870. }
  1871. if (!existing) {
  1872. // Optimize the case of one listener. Don't need the extra array object.
  1873. existing = events[type] = listener;
  1874. ++target._eventsCount;
  1875. } else {
  1876. if (typeof existing === 'function') {
  1877. // Adding the second element, need to change to array.
  1878. existing = events[type] = prepend ? [listener, existing] :
  1879. [existing, listener];
  1880. } else {
  1881. // If we've already got an array, just append.
  1882. if (prepend) {
  1883. existing.unshift(listener);
  1884. } else {
  1885. existing.push(listener);
  1886. }
  1887. }
  1888. // Check for listener leak
  1889. if (!existing.warned) {
  1890. m = $getMaxListeners(target);
  1891. if (m && m > 0 && existing.length > m) {
  1892. existing.warned = true;
  1893. var w = new Error('Possible EventEmitter memory leak detected. ' +
  1894. existing.length + ' ' + type + ' listeners added. ' +
  1895. 'Use emitter.setMaxListeners() to increase limit');
  1896. w.name = 'MaxListenersExceededWarning';
  1897. w.emitter = target;
  1898. w.type = type;
  1899. w.count = existing.length;
  1900. emitWarning(w);
  1901. }
  1902. }
  1903. }
  1904. return target;
  1905. }
  1906. function emitWarning(e) {
  1907. typeof console.warn === 'function' ? console.warn(e) : console.log(e);
  1908. }
  1909. EventEmitter.prototype.addListener = function addListener(type, listener) {
  1910. return _addListener(this, type, listener, false);
  1911. };
  1912. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  1913. EventEmitter.prototype.prependListener =
  1914. function prependListener(type, listener) {
  1915. return _addListener(this, type, listener, true);
  1916. };
  1917. function _onceWrap(target, type, listener) {
  1918. var fired = false;
  1919. function g() {
  1920. target.removeListener(type, g);
  1921. if (!fired) {
  1922. fired = true;
  1923. listener.apply(target, arguments);
  1924. }
  1925. }
  1926. g.listener = listener;
  1927. return g;
  1928. }
  1929. EventEmitter.prototype.once = function once(type, listener) {
  1930. if (typeof listener !== 'function')
  1931. throw new TypeError('"listener" argument must be a function');
  1932. this.on(type, _onceWrap(this, type, listener));
  1933. return this;
  1934. };
  1935. EventEmitter.prototype.prependOnceListener =
  1936. function prependOnceListener(type, listener) {
  1937. if (typeof listener !== 'function')
  1938. throw new TypeError('"listener" argument must be a function');
  1939. this.prependListener(type, _onceWrap(this, type, listener));
  1940. return this;
  1941. };
  1942. // emits a 'removeListener' event iff the listener was removed
  1943. EventEmitter.prototype.removeListener =
  1944. function removeListener(type, listener) {
  1945. var list, events, position, i, originalListener;
  1946. if (typeof listener !== 'function')
  1947. throw new TypeError('"listener" argument must be a function');
  1948. events = this._events;
  1949. if (!events)
  1950. return this;
  1951. list = events[type];
  1952. if (!list)
  1953. return this;
  1954. if (list === listener || (list.listener && list.listener === listener)) {
  1955. if (--this._eventsCount === 0)
  1956. this._events = new EventHandlers();
  1957. else {
  1958. delete events[type];
  1959. if (events.removeListener)
  1960. this.emit('removeListener', type, list.listener || listener);
  1961. }
  1962. } else if (typeof list !== 'function') {
  1963. position = -1;
  1964. for (i = list.length; i-- > 0;) {
  1965. if (list[i] === listener ||
  1966. (list[i].listener && list[i].listener === listener)) {
  1967. originalListener = list[i].listener;
  1968. position = i;
  1969. break;
  1970. }
  1971. }
  1972. if (position < 0)
  1973. return this;
  1974. if (list.length === 1) {
  1975. list[0] = undefined;
  1976. if (--this._eventsCount === 0) {
  1977. this._events = new EventHandlers();
  1978. return this;
  1979. } else {
  1980. delete events[type];
  1981. }
  1982. } else {
  1983. spliceOne(list, position);
  1984. }
  1985. if (events.removeListener)
  1986. this.emit('removeListener', type, originalListener || listener);
  1987. }
  1988. return this;
  1989. };
  1990. EventEmitter.prototype.removeAllListeners =
  1991. function removeAllListeners(type) {
  1992. var listeners, events;
  1993. events = this._events;
  1994. if (!events)
  1995. return this;
  1996. // not listening for removeListener, no need to emit
  1997. if (!events.removeListener) {
  1998. if (arguments.length === 0) {
  1999. this._events = new EventHandlers();
  2000. this._eventsCount = 0;
  2001. } else if (events[type]) {
  2002. if (--this._eventsCount === 0)
  2003. this._events = new EventHandlers();
  2004. else
  2005. delete events[type];
  2006. }
  2007. return this;
  2008. }
  2009. // emit removeListener for all listeners on all events
  2010. if (arguments.length === 0) {
  2011. var keys = Object.keys(events);
  2012. for (var i = 0, key; i < keys.length; ++i) {
  2013. key = keys[i];
  2014. if (key === 'removeListener') continue;
  2015. this.removeAllListeners(key);
  2016. }
  2017. this.removeAllListeners('removeListener');
  2018. this._events = new EventHandlers();
  2019. this._eventsCount = 0;
  2020. return this;
  2021. }
  2022. listeners = events[type];
  2023. if (typeof listeners === 'function') {
  2024. this.removeListener(type, listeners);
  2025. } else if (listeners) {
  2026. // LIFO order
  2027. do {
  2028. this.removeListener(type, listeners[listeners.length - 1]);
  2029. } while (listeners[0]);
  2030. }
  2031. return this;
  2032. };
  2033. EventEmitter.prototype.listeners = function listeners(type) {
  2034. var evlistener;
  2035. var ret;
  2036. var events = this._events;
  2037. if (!events)
  2038. ret = [];
  2039. else {
  2040. evlistener = events[type];
  2041. if (!evlistener)
  2042. ret = [];
  2043. else if (typeof evlistener === 'function')
  2044. ret = [evlistener.listener || evlistener];
  2045. else
  2046. ret = unwrapListeners(evlistener);
  2047. }
  2048. return ret;
  2049. };
  2050. EventEmitter.listenerCount = function(emitter, type) {
  2051. if (typeof emitter.listenerCount === 'function') {
  2052. return emitter.listenerCount(type);
  2053. } else {
  2054. return listenerCount$1.call(emitter, type);
  2055. }
  2056. };
  2057. EventEmitter.prototype.listenerCount = listenerCount$1;
  2058. function listenerCount$1(type) {
  2059. var events = this._events;
  2060. if (events) {
  2061. var evlistener = events[type];
  2062. if (typeof evlistener === 'function') {
  2063. return 1;
  2064. } else if (evlistener) {
  2065. return evlistener.length;
  2066. }
  2067. }
  2068. return 0;
  2069. }
  2070. EventEmitter.prototype.eventNames = function eventNames() {
  2071. return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
  2072. };
  2073. // About 1.5x faster than the two-arg version of Array#splice().
  2074. function spliceOne(list, index) {
  2075. for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
  2076. list[i] = list[k];
  2077. list.pop();
  2078. }
  2079. function arrayClone(arr, i) {
  2080. var copy = new Array(i);
  2081. while (i--)
  2082. copy[i] = arr[i];
  2083. return copy;
  2084. }
  2085. function unwrapListeners(arr) {
  2086. var ret = new Array(arr.length);
  2087. for (var i = 0; i < ret.length; ++i) {
  2088. ret[i] = arr[i].listener || arr[i];
  2089. }
  2090. return ret;
  2091. }
  2092. // shim for using process in browser
  2093. // based off https://github.com/defunctzombie/node-process/blob/master/browser.js
  2094. function defaultSetTimout() {
  2095. throw new Error('setTimeout has not been defined');
  2096. }
  2097. function defaultClearTimeout () {
  2098. throw new Error('clearTimeout has not been defined');
  2099. }
  2100. var cachedSetTimeout = defaultSetTimout;
  2101. var cachedClearTimeout = defaultClearTimeout;
  2102. if (typeof global$1.setTimeout === 'function') {
  2103. cachedSetTimeout = setTimeout;
  2104. }
  2105. if (typeof global$1.clearTimeout === 'function') {
  2106. cachedClearTimeout = clearTimeout;
  2107. }
  2108. function runTimeout(fun) {
  2109. if (cachedSetTimeout === setTimeout) {
  2110. //normal enviroments in sane situations
  2111. return setTimeout(fun, 0);
  2112. }
  2113. // if setTimeout wasn't available but was latter defined
  2114. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  2115. cachedSetTimeout = setTimeout;
  2116. return setTimeout(fun, 0);
  2117. }
  2118. try {
  2119. // when when somebody has screwed with setTimeout but no I.E. maddness
  2120. return cachedSetTimeout(fun, 0);
  2121. } catch(e){
  2122. try {
  2123. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  2124. return cachedSetTimeout.call(null, fun, 0);
  2125. } catch(e){
  2126. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  2127. return cachedSetTimeout.call(this, fun, 0);
  2128. }
  2129. }
  2130. }
  2131. function runClearTimeout(marker) {
  2132. if (cachedClearTimeout === clearTimeout) {
  2133. //normal enviroments in sane situations
  2134. return clearTimeout(marker);
  2135. }
  2136. // if clearTimeout wasn't available but was latter defined
  2137. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  2138. cachedClearTimeout = clearTimeout;
  2139. return clearTimeout(marker);
  2140. }
  2141. try {
  2142. // when when somebody has screwed with setTimeout but no I.E. maddness
  2143. return cachedClearTimeout(marker);
  2144. } catch (e){
  2145. try {
  2146. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  2147. return cachedClearTimeout.call(null, marker);
  2148. } catch (e){
  2149. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  2150. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  2151. return cachedClearTimeout.call(this, marker);
  2152. }
  2153. }
  2154. }
  2155. var queue = [];
  2156. var draining = false;
  2157. var currentQueue;
  2158. var queueIndex = -1;
  2159. function cleanUpNextTick() {
  2160. if (!draining || !currentQueue) {
  2161. return;
  2162. }
  2163. draining = false;
  2164. if (currentQueue.length) {
  2165. queue = currentQueue.concat(queue);
  2166. } else {
  2167. queueIndex = -1;
  2168. }
  2169. if (queue.length) {
  2170. drainQueue();
  2171. }
  2172. }
  2173. function drainQueue() {
  2174. if (draining) {
  2175. return;
  2176. }
  2177. var timeout = runTimeout(cleanUpNextTick);
  2178. draining = true;
  2179. var len = queue.length;
  2180. while(len) {
  2181. currentQueue = queue;
  2182. queue = [];
  2183. while (++queueIndex < len) {
  2184. if (currentQueue) {
  2185. currentQueue[queueIndex].run();
  2186. }
  2187. }
  2188. queueIndex = -1;
  2189. len = queue.length;
  2190. }
  2191. currentQueue = null;
  2192. draining = false;
  2193. runClearTimeout(timeout);
  2194. }
  2195. function nextTick(fun) {
  2196. var args = new Array(arguments.length - 1);
  2197. if (arguments.length > 1) {
  2198. for (var i = 1; i < arguments.length; i++) {
  2199. args[i - 1] = arguments[i];
  2200. }
  2201. }
  2202. queue.push(new Item(fun, args));
  2203. if (queue.length === 1 && !draining) {
  2204. runTimeout(drainQueue);
  2205. }
  2206. }
  2207. // v8 likes predictible objects
  2208. function Item(fun, array) {
  2209. this.fun = fun;
  2210. this.array = array;
  2211. }
  2212. Item.prototype.run = function () {
  2213. this.fun.apply(null, this.array);
  2214. };
  2215. var title = 'browser';
  2216. var platform = 'browser';
  2217. var browser = true;
  2218. var env = {};
  2219. var argv = [];
  2220. var version = ''; // empty string to avoid regexp issues
  2221. var versions = {};
  2222. var release = {};
  2223. var config = {};
  2224. function noop() {}
  2225. var on = noop;
  2226. var addListener = noop;
  2227. var once = noop;
  2228. var off = noop;
  2229. var removeListener = noop;
  2230. var removeAllListeners = noop;
  2231. var emit = noop;
  2232. function binding(name) {
  2233. throw new Error('process.binding is not supported');
  2234. }
  2235. function cwd () { return '/' }
  2236. function chdir (dir) {
  2237. throw new Error('process.chdir is not supported');
  2238. }function umask() { return 0; }
  2239. // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js
  2240. var performance = global$1.performance || {};
  2241. var performanceNow =
  2242. performance.now ||
  2243. performance.mozNow ||
  2244. performance.msNow ||
  2245. performance.oNow ||
  2246. performance.webkitNow ||
  2247. function(){ return (new Date()).getTime() };
  2248. // generate timestamp or delta
  2249. // see http://nodejs.org/api/process.html#process_process_hrtime
  2250. function hrtime(previousTimestamp){
  2251. var clocktime = performanceNow.call(performance)*1e-3;
  2252. var seconds = Math.floor(clocktime);
  2253. var nanoseconds = Math.floor((clocktime%1)*1e9);
  2254. if (previousTimestamp) {
  2255. seconds = seconds - previousTimestamp[0];
  2256. nanoseconds = nanoseconds - previousTimestamp[1];
  2257. if (nanoseconds<0) {
  2258. seconds--;
  2259. nanoseconds += 1e9;
  2260. }
  2261. }
  2262. return [seconds,nanoseconds]
  2263. }
  2264. var startTime = new Date();
  2265. function uptime() {
  2266. var currentTime = new Date();
  2267. var dif = currentTime - startTime;
  2268. return dif / 1000;
  2269. }
  2270. var process = {
  2271. nextTick: nextTick,
  2272. title: title,
  2273. browser: browser,
  2274. env: env,
  2275. argv: argv,
  2276. version: version,
  2277. versions: versions,
  2278. on: on,
  2279. addListener: addListener,
  2280. once: once,
  2281. off: off,
  2282. removeListener: removeListener,
  2283. removeAllListeners: removeAllListeners,
  2284. emit: emit,
  2285. binding: binding,
  2286. cwd: cwd,
  2287. chdir: chdir,
  2288. umask: umask,
  2289. hrtime: hrtime,
  2290. platform: platform,
  2291. release: release,
  2292. config: config,
  2293. uptime: uptime
  2294. };
  2295. var inherits;
  2296. if (typeof Object.create === 'function'){
  2297. inherits = function inherits(ctor, superCtor) {
  2298. // implementation from standard node.js 'util' module
  2299. ctor.super_ = superCtor;
  2300. ctor.prototype = Object.create(superCtor.prototype, {
  2301. constructor: {
  2302. value: ctor,
  2303. enumerable: false,
  2304. writable: true,
  2305. configurable: true
  2306. }
  2307. });
  2308. };
  2309. } else {
  2310. inherits = function inherits(ctor, superCtor) {
  2311. ctor.super_ = superCtor;
  2312. var TempCtor = function () {};
  2313. TempCtor.prototype = superCtor.prototype;
  2314. ctor.prototype = new TempCtor();
  2315. ctor.prototype.constructor = ctor;
  2316. };
  2317. }
  2318. var inherits$1 = inherits;
  2319. var formatRegExp = /%[sdj%]/g;
  2320. function format(f) {
  2321. if (!isString(f)) {
  2322. var objects = [];
  2323. for (var i = 0; i < arguments.length; i++) {
  2324. objects.push(inspect(arguments[i]));
  2325. }
  2326. return objects.join(' ');
  2327. }
  2328. var i = 1;
  2329. var args = arguments;
  2330. var len = args.length;
  2331. var str = String(f).replace(formatRegExp, function(x) {
  2332. if (x === '%%') return '%';
  2333. if (i >= len) return x;
  2334. switch (x) {
  2335. case '%s': return String(args[i++]);
  2336. case '%d': return Number(args[i++]);
  2337. case '%j':
  2338. try {
  2339. return JSON.stringify(args[i++]);
  2340. } catch (_) {
  2341. return '[Circular]';
  2342. }
  2343. default:
  2344. return x;
  2345. }
  2346. });
  2347. for (var x = args[i]; i < len; x = args[++i]) {
  2348. if (isNull(x) || !isObject(x)) {
  2349. str += ' ' + x;
  2350. } else {
  2351. str += ' ' + inspect(x);
  2352. }
  2353. }
  2354. return str;
  2355. }
  2356. // Mark that a method should not be used.
  2357. // Returns a modified function which warns once by default.
  2358. // If --no-deprecation is set, then it is a no-op.
  2359. function deprecate(fn, msg) {
  2360. // Allow for deprecating things in the process of starting up.
  2361. if (isUndefined(global$1.process)) {
  2362. return function() {
  2363. return deprecate(fn, msg).apply(this, arguments);
  2364. };
  2365. }
  2366. if (process.noDeprecation === true) {
  2367. return fn;
  2368. }
  2369. var warned = false;
  2370. function deprecated() {
  2371. if (!warned) {
  2372. if (process.throwDeprecation) {
  2373. throw new Error(msg);
  2374. } else if (process.traceDeprecation) {
  2375. console.trace(msg);
  2376. } else {
  2377. console.error(msg);
  2378. }
  2379. warned = true;
  2380. }
  2381. return fn.apply(this, arguments);
  2382. }
  2383. return deprecated;
  2384. }
  2385. var debugs = {};
  2386. var debugEnviron;
  2387. function debuglog(set) {
  2388. if (isUndefined(debugEnviron))
  2389. debugEnviron = process.env.NODE_DEBUG || '';
  2390. set = set.toUpperCase();
  2391. if (!debugs[set]) {
  2392. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  2393. var pid = 0;
  2394. debugs[set] = function() {
  2395. var msg = format.apply(null, arguments);
  2396. console.error('%s %d: %s', set, pid, msg);
  2397. };
  2398. } else {
  2399. debugs[set] = function() {};
  2400. }
  2401. }
  2402. return debugs[set];
  2403. }
  2404. /**
  2405. * Echos the value of a value. Trys to print the value out
  2406. * in the best way possible given the different types.
  2407. *
  2408. * @param {Object} obj The object to print out.
  2409. * @param {Object} opts Optional options object that alters the output.
  2410. */
  2411. /* legacy: obj, showHidden, depth, colors*/
  2412. function inspect(obj, opts) {
  2413. // default options
  2414. var ctx = {
  2415. seen: [],
  2416. stylize: stylizeNoColor
  2417. };
  2418. // legacy...
  2419. if (arguments.length >= 3) ctx.depth = arguments[2];
  2420. if (arguments.length >= 4) ctx.colors = arguments[3];
  2421. if (isBoolean(opts)) {
  2422. // legacy...
  2423. ctx.showHidden = opts;
  2424. } else if (opts) {
  2425. // got an "options" object
  2426. _extend(ctx, opts);
  2427. }
  2428. // set default options
  2429. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  2430. if (isUndefined(ctx.depth)) ctx.depth = 2;
  2431. if (isUndefined(ctx.colors)) ctx.colors = false;
  2432. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  2433. if (ctx.colors) ctx.stylize = stylizeWithColor;
  2434. return formatValue(ctx, obj, ctx.depth);
  2435. }
  2436. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  2437. inspect.colors = {
  2438. 'bold' : [1, 22],
  2439. 'italic' : [3, 23],
  2440. 'underline' : [4, 24],
  2441. 'inverse' : [7, 27],
  2442. 'white' : [37, 39],
  2443. 'grey' : [90, 39],
  2444. 'black' : [30, 39],
  2445. 'blue' : [34, 39],
  2446. 'cyan' : [36, 39],
  2447. 'green' : [32, 39],
  2448. 'magenta' : [35, 39],
  2449. 'red' : [31, 39],
  2450. 'yellow' : [33, 39]
  2451. };
  2452. // Don't use 'blue' not visible on cmd.exe
  2453. inspect.styles = {
  2454. 'special': 'cyan',
  2455. 'number': 'yellow',
  2456. 'boolean': 'yellow',
  2457. 'undefined': 'grey',
  2458. 'null': 'bold',
  2459. 'string': 'green',
  2460. 'date': 'magenta',
  2461. // "name": intentionally not styling
  2462. 'regexp': 'red'
  2463. };
  2464. function stylizeWithColor(str, styleType) {
  2465. var style = inspect.styles[styleType];
  2466. if (style) {
  2467. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  2468. '\u001b[' + inspect.colors[style][1] + 'm';
  2469. } else {
  2470. return str;
  2471. }
  2472. }
  2473. function stylizeNoColor(str, styleType) {
  2474. return str;
  2475. }
  2476. function arrayToHash(array) {
  2477. var hash = {};
  2478. array.forEach(function(val, idx) {
  2479. hash[val] = true;
  2480. });
  2481. return hash;
  2482. }
  2483. function formatValue(ctx, value, recurseTimes) {
  2484. // Provide a hook for user-specified inspect functions.
  2485. // Check that value is an object with an inspect function on it
  2486. if (ctx.customInspect &&
  2487. value &&
  2488. isFunction(value.inspect) &&
  2489. // Filter out the util module, it's inspect function is special
  2490. value.inspect !== inspect &&
  2491. // Also filter out any prototype objects using the circular check.
  2492. !(value.constructor && value.constructor.prototype === value)) {
  2493. var ret = value.inspect(recurseTimes, ctx);
  2494. if (!isString(ret)) {
  2495. ret = formatValue(ctx, ret, recurseTimes);
  2496. }
  2497. return ret;
  2498. }
  2499. // Primitive types cannot have properties
  2500. var primitive = formatPrimitive(ctx, value);
  2501. if (primitive) {
  2502. return primitive;
  2503. }
  2504. // Look up the keys of the object.
  2505. var keys = Object.keys(value);
  2506. var visibleKeys = arrayToHash(keys);
  2507. if (ctx.showHidden) {
  2508. keys = Object.getOwnPropertyNames(value);
  2509. }
  2510. // IE doesn't make error fields non-enumerable
  2511. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  2512. if (isError(value)
  2513. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  2514. return formatError(value);
  2515. }
  2516. // Some type of object without properties can be shortcutted.
  2517. if (keys.length === 0) {
  2518. if (isFunction(value)) {
  2519. var name = value.name ? ': ' + value.name : '';
  2520. return ctx.stylize('[Function' + name + ']', 'special');
  2521. }
  2522. if (isRegExp(value)) {
  2523. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2524. }
  2525. if (isDate(value)) {
  2526. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  2527. }
  2528. if (isError(value)) {
  2529. return formatError(value);
  2530. }
  2531. }
  2532. var base = '', array = false, braces = ['{', '}'];
  2533. // Make Array say that they are Array
  2534. if (isArray(value)) {
  2535. array = true;
  2536. braces = ['[', ']'];
  2537. }
  2538. // Make functions say that they are functions
  2539. if (isFunction(value)) {
  2540. var n = value.name ? ': ' + value.name : '';
  2541. base = ' [Function' + n + ']';
  2542. }
  2543. // Make RegExps say that they are RegExps
  2544. if (isRegExp(value)) {
  2545. base = ' ' + RegExp.prototype.toString.call(value);
  2546. }
  2547. // Make dates with properties first say the date
  2548. if (isDate(value)) {
  2549. base = ' ' + Date.prototype.toUTCString.call(value);
  2550. }
  2551. // Make error with message first say the error
  2552. if (isError(value)) {
  2553. base = ' ' + formatError(value);
  2554. }
  2555. if (keys.length === 0 && (!array || value.length == 0)) {
  2556. return braces[0] + base + braces[1];
  2557. }
  2558. if (recurseTimes < 0) {
  2559. if (isRegExp(value)) {
  2560. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  2561. } else {
  2562. return ctx.stylize('[Object]', 'special');
  2563. }
  2564. }
  2565. ctx.seen.push(value);
  2566. var output;
  2567. if (array) {
  2568. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  2569. } else {
  2570. output = keys.map(function(key) {
  2571. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  2572. });
  2573. }
  2574. ctx.seen.pop();
  2575. return reduceToSingleString(output, base, braces);
  2576. }
  2577. function formatPrimitive(ctx, value) {
  2578. if (isUndefined(value))
  2579. return ctx.stylize('undefined', 'undefined');
  2580. if (isString(value)) {
  2581. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  2582. .replace(/'/g, "\\'")
  2583. .replace(/\\"/g, '"') + '\'';
  2584. return ctx.stylize(simple, 'string');
  2585. }
  2586. if (isNumber(value))
  2587. return ctx.stylize('' + value, 'number');
  2588. if (isBoolean(value))
  2589. return ctx.stylize('' + value, 'boolean');
  2590. // For some reason typeof null is "object", so special case here.
  2591. if (isNull(value))
  2592. return ctx.stylize('null', 'null');
  2593. }
  2594. function formatError(value) {
  2595. return '[' + Error.prototype.toString.call(value) + ']';
  2596. }
  2597. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  2598. var output = [];
  2599. for (var i = 0, l = value.length; i < l; ++i) {
  2600. if (hasOwnProperty(value, String(i))) {
  2601. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2602. String(i), true));
  2603. } else {
  2604. output.push('');
  2605. }
  2606. }
  2607. keys.forEach(function(key) {
  2608. if (!key.match(/^\d+$/)) {
  2609. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  2610. key, true));
  2611. }
  2612. });
  2613. return output;
  2614. }
  2615. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  2616. var name, str, desc;
  2617. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  2618. if (desc.get) {
  2619. if (desc.set) {
  2620. str = ctx.stylize('[Getter/Setter]', 'special');
  2621. } else {
  2622. str = ctx.stylize('[Getter]', 'special');
  2623. }
  2624. } else {
  2625. if (desc.set) {
  2626. str = ctx.stylize('[Setter]', 'special');
  2627. }
  2628. }
  2629. if (!hasOwnProperty(visibleKeys, key)) {
  2630. name = '[' + key + ']';
  2631. }
  2632. if (!str) {
  2633. if (ctx.seen.indexOf(desc.value) < 0) {
  2634. if (isNull(recurseTimes)) {
  2635. str = formatValue(ctx, desc.value, null);
  2636. } else {
  2637. str = formatValue(ctx, desc.value, recurseTimes - 1);
  2638. }
  2639. if (str.indexOf('\n') > -1) {
  2640. if (array) {
  2641. str = str.split('\n').map(function(line) {
  2642. return ' ' + line;
  2643. }).join('\n').substr(2);
  2644. } else {
  2645. str = '\n' + str.split('\n').map(function(line) {
  2646. return ' ' + line;
  2647. }).join('\n');
  2648. }
  2649. }
  2650. } else {
  2651. str = ctx.stylize('[Circular]', 'special');
  2652. }
  2653. }
  2654. if (isUndefined(name)) {
  2655. if (array && key.match(/^\d+$/)) {
  2656. return str;
  2657. }
  2658. name = JSON.stringify('' + key);
  2659. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  2660. name = name.substr(1, name.length - 2);
  2661. name = ctx.stylize(name, 'name');
  2662. } else {
  2663. name = name.replace(/'/g, "\\'")
  2664. .replace(/\\"/g, '"')
  2665. .replace(/(^"|"$)/g, "'");
  2666. name = ctx.stylize(name, 'string');
  2667. }
  2668. }
  2669. return name + ': ' + str;
  2670. }
  2671. function reduceToSingleString(output, base, braces) {
  2672. var length = output.reduce(function(prev, cur) {
  2673. if (cur.indexOf('\n') >= 0) ;
  2674. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  2675. }, 0);
  2676. if (length > 60) {
  2677. return braces[0] +
  2678. (base === '' ? '' : base + '\n ') +
  2679. ' ' +
  2680. output.join(',\n ') +
  2681. ' ' +
  2682. braces[1];
  2683. }
  2684. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  2685. }
  2686. // NOTE: These type checking functions intentionally don't use `instanceof`
  2687. // because it is fragile and can be easily faked with `Object.create()`.
  2688. function isArray(ar) {
  2689. return Array.isArray(ar);
  2690. }
  2691. function isBoolean(arg) {
  2692. return typeof arg === 'boolean';
  2693. }
  2694. function isNull(arg) {
  2695. return arg === null;
  2696. }
  2697. function isNumber(arg) {
  2698. return typeof arg === 'number';
  2699. }
  2700. function isString(arg) {
  2701. return typeof arg === 'string';
  2702. }
  2703. function isUndefined(arg) {
  2704. return arg === void 0;
  2705. }
  2706. function isRegExp(re) {
  2707. return isObject(re) && objectToString(re) === '[object RegExp]';
  2708. }
  2709. function isObject(arg) {
  2710. return typeof arg === 'object' && arg !== null;
  2711. }
  2712. function isDate(d) {
  2713. return isObject(d) && objectToString(d) === '[object Date]';
  2714. }
  2715. function isError(e) {
  2716. return isObject(e) &&
  2717. (objectToString(e) === '[object Error]' || e instanceof Error);
  2718. }
  2719. function isFunction(arg) {
  2720. return typeof arg === 'function';
  2721. }
  2722. function objectToString(o) {
  2723. return Object.prototype.toString.call(o);
  2724. }
  2725. function _extend(origin, add) {
  2726. // Don't do anything if add isn't an object
  2727. if (!add || !isObject(add)) return origin;
  2728. var keys = Object.keys(add);
  2729. var i = keys.length;
  2730. while (i--) {
  2731. origin[keys[i]] = add[keys[i]];
  2732. }
  2733. return origin;
  2734. }
  2735. function hasOwnProperty(obj, prop) {
  2736. return Object.prototype.hasOwnProperty.call(obj, prop);
  2737. }
  2738. function BufferList() {
  2739. this.head = null;
  2740. this.tail = null;
  2741. this.length = 0;
  2742. }
  2743. BufferList.prototype.push = function (v) {
  2744. var entry = { data: v, next: null };
  2745. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  2746. this.tail = entry;
  2747. ++this.length;
  2748. };
  2749. BufferList.prototype.unshift = function (v) {
  2750. var entry = { data: v, next: this.head };
  2751. if (this.length === 0) this.tail = entry;
  2752. this.head = entry;
  2753. ++this.length;
  2754. };
  2755. BufferList.prototype.shift = function () {
  2756. if (this.length === 0) return;
  2757. var ret = this.head.data;
  2758. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  2759. --this.length;
  2760. return ret;
  2761. };
  2762. BufferList.prototype.clear = function () {
  2763. this.head = this.tail = null;
  2764. this.length = 0;
  2765. };
  2766. BufferList.prototype.join = function (s) {
  2767. if (this.length === 0) return '';
  2768. var p = this.head;
  2769. var ret = '' + p.data;
  2770. while (p = p.next) {
  2771. ret += s + p.data;
  2772. }return ret;
  2773. };
  2774. BufferList.prototype.concat = function (n) {
  2775. if (this.length === 0) return Buffer.alloc(0);
  2776. if (this.length === 1) return this.head.data;
  2777. var ret = Buffer.allocUnsafe(n >>> 0);
  2778. var p = this.head;
  2779. var i = 0;
  2780. while (p) {
  2781. p.data.copy(ret, i);
  2782. i += p.data.length;
  2783. p = p.next;
  2784. }
  2785. return ret;
  2786. };
  2787. // Copyright Joyent, Inc. and other Node contributors.
  2788. //
  2789. // Permission is hereby granted, free of charge, to any person obtaining a
  2790. // copy of this software and associated documentation files (the
  2791. // "Software"), to deal in the Software without restriction, including
  2792. // without limitation the rights to use, copy, modify, merge, publish,
  2793. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2794. // persons to whom the Software is furnished to do so, subject to the
  2795. // following conditions:
  2796. //
  2797. // The above copyright notice and this permission notice shall be included
  2798. // in all copies or substantial portions of the Software.
  2799. //
  2800. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2801. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2802. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2803. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2804. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2805. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2806. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2807. var isBufferEncoding = Buffer.isEncoding
  2808. || function(encoding) {
  2809. switch (encoding && encoding.toLowerCase()) {
  2810. 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;
  2811. default: return false;
  2812. }
  2813. };
  2814. function assertEncoding(encoding) {
  2815. if (encoding && !isBufferEncoding(encoding)) {
  2816. throw new Error('Unknown encoding: ' + encoding);
  2817. }
  2818. }
  2819. // StringDecoder provides an interface for efficiently splitting a series of
  2820. // buffers into a series of JS strings without breaking apart multi-byte
  2821. // characters. CESU-8 is handled as part of the UTF-8 encoding.
  2822. //
  2823. // @TODO Handling all encodings inside a single object makes it very difficult
  2824. // to reason about this code, so it should be split up in the future.
  2825. // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
  2826. // points as used by CESU-8.
  2827. function StringDecoder(encoding) {
  2828. this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
  2829. assertEncoding(encoding);
  2830. switch (this.encoding) {
  2831. case 'utf8':
  2832. // CESU-8 represents each of Surrogate Pair by 3-bytes
  2833. this.surrogateSize = 3;
  2834. break;
  2835. case 'ucs2':
  2836. case 'utf16le':
  2837. // UTF-16 represents each of Surrogate Pair by 2-bytes
  2838. this.surrogateSize = 2;
  2839. this.detectIncompleteChar = utf16DetectIncompleteChar;
  2840. break;
  2841. case 'base64':
  2842. // Base-64 stores 3 bytes in 4 chars, and pads the remainder.
  2843. this.surrogateSize = 3;
  2844. this.detectIncompleteChar = base64DetectIncompleteChar;
  2845. break;
  2846. default:
  2847. this.write = passThroughWrite;
  2848. return;
  2849. }
  2850. // Enough space to store all bytes of a single character. UTF-8 needs 4
  2851. // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
  2852. this.charBuffer = new Buffer(6);
  2853. // Number of bytes received for the current incomplete multi-byte character.
  2854. this.charReceived = 0;
  2855. // Number of bytes expected for the current incomplete multi-byte character.
  2856. this.charLength = 0;
  2857. }
  2858. // write decodes the given buffer and returns it as JS string that is
  2859. // guaranteed to not contain any partial multi-byte characters. Any partial
  2860. // character found at the end of the buffer is buffered up, and will be
  2861. // returned when calling write again with the remaining bytes.
  2862. //
  2863. // Note: Converting a Buffer containing an orphan surrogate to a String
  2864. // currently works, but converting a String to a Buffer (via `new Buffer`, or
  2865. // Buffer#write) will replace incomplete surrogates with the unicode
  2866. // replacement character. See https://codereview.chromium.org/121173009/ .
  2867. StringDecoder.prototype.write = function(buffer) {
  2868. var charStr = '';
  2869. // if our last write ended with an incomplete multibyte character
  2870. while (this.charLength) {
  2871. // determine how many remaining bytes this buffer has to offer for this char
  2872. var available = (buffer.length >= this.charLength - this.charReceived) ?
  2873. this.charLength - this.charReceived :
  2874. buffer.length;
  2875. // add the new bytes to the char buffer
  2876. buffer.copy(this.charBuffer, this.charReceived, 0, available);
  2877. this.charReceived += available;
  2878. if (this.charReceived < this.charLength) {
  2879. // still not enough chars in this buffer? wait for more ...
  2880. return '';
  2881. }
  2882. // remove bytes belonging to the current character from the buffer
  2883. buffer = buffer.slice(available, buffer.length);
  2884. // get the character that was split
  2885. charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
  2886. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  2887. var charCode = charStr.charCodeAt(charStr.length - 1);
  2888. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  2889. this.charLength += this.surrogateSize;
  2890. charStr = '';
  2891. continue;
  2892. }
  2893. this.charReceived = this.charLength = 0;
  2894. // if there are no more bytes in this buffer, just emit our char
  2895. if (buffer.length === 0) {
  2896. return charStr;
  2897. }
  2898. break;
  2899. }
  2900. // determine and set charLength / charReceived
  2901. this.detectIncompleteChar(buffer);
  2902. var end = buffer.length;
  2903. if (this.charLength) {
  2904. // buffer the incomplete character bytes we got
  2905. buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
  2906. end -= this.charReceived;
  2907. }
  2908. charStr += buffer.toString(this.encoding, 0, end);
  2909. var end = charStr.length - 1;
  2910. var charCode = charStr.charCodeAt(end);
  2911. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  2912. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  2913. var size = this.surrogateSize;
  2914. this.charLength += size;
  2915. this.charReceived += size;
  2916. this.charBuffer.copy(this.charBuffer, size, 0, size);
  2917. buffer.copy(this.charBuffer, 0, 0, size);
  2918. return charStr.substring(0, end);
  2919. }
  2920. // or just emit the charStr
  2921. return charStr;
  2922. };
  2923. // detectIncompleteChar determines if there is an incomplete UTF-8 character at
  2924. // the end of the given buffer. If so, it sets this.charLength to the byte
  2925. // length that character, and sets this.charReceived to the number of bytes
  2926. // that are available for this character.
  2927. StringDecoder.prototype.detectIncompleteChar = function(buffer) {
  2928. // determine how many bytes we have to check at the end of this buffer
  2929. var i = (buffer.length >= 3) ? 3 : buffer.length;
  2930. // Figure out if one of the last i bytes of our buffer announces an
  2931. // incomplete char.
  2932. for (; i > 0; i--) {
  2933. var c = buffer[buffer.length - i];
  2934. // See http://en.wikipedia.org/wiki/UTF-8#Description
  2935. // 110XXXXX
  2936. if (i == 1 && c >> 5 == 0x06) {
  2937. this.charLength = 2;
  2938. break;
  2939. }
  2940. // 1110XXXX
  2941. if (i <= 2 && c >> 4 == 0x0E) {
  2942. this.charLength = 3;
  2943. break;
  2944. }
  2945. // 11110XXX
  2946. if (i <= 3 && c >> 3 == 0x1E) {
  2947. this.charLength = 4;
  2948. break;
  2949. }
  2950. }
  2951. this.charReceived = i;
  2952. };
  2953. StringDecoder.prototype.end = function(buffer) {
  2954. var res = '';
  2955. if (buffer && buffer.length)
  2956. res = this.write(buffer);
  2957. if (this.charReceived) {
  2958. var cr = this.charReceived;
  2959. var buf = this.charBuffer;
  2960. var enc = this.encoding;
  2961. res += buf.slice(0, cr).toString(enc);
  2962. }
  2963. return res;
  2964. };
  2965. function passThroughWrite(buffer) {
  2966. return buffer.toString(this.encoding);
  2967. }
  2968. function utf16DetectIncompleteChar(buffer) {
  2969. this.charReceived = buffer.length % 2;
  2970. this.charLength = this.charReceived ? 2 : 0;
  2971. }
  2972. function base64DetectIncompleteChar(buffer) {
  2973. this.charReceived = buffer.length % 3;
  2974. this.charLength = this.charReceived ? 3 : 0;
  2975. }
  2976. Readable.ReadableState = ReadableState;
  2977. var debug = debuglog('stream');
  2978. inherits$1(Readable, EventEmitter);
  2979. function prependListener(emitter, event, fn) {
  2980. // Sadly this is not cacheable as some libraries bundle their own
  2981. // event emitter implementation with them.
  2982. if (typeof emitter.prependListener === 'function') {
  2983. return emitter.prependListener(event, fn);
  2984. } else {
  2985. // This is a hack to make sure that our error handler is attached before any
  2986. // userland ones. NEVER DO THIS. This is here only because this code needs
  2987. // to continue to work with older versions of Node.js that do not include
  2988. // the prependListener() method. The goal is to eventually remove this hack.
  2989. if (!emitter._events || !emitter._events[event])
  2990. emitter.on(event, fn);
  2991. else if (Array.isArray(emitter._events[event]))
  2992. emitter._events[event].unshift(fn);
  2993. else
  2994. emitter._events[event] = [fn, emitter._events[event]];
  2995. }
  2996. }
  2997. function listenerCount (emitter, type) {
  2998. return emitter.listeners(type).length;
  2999. }
  3000. function ReadableState(options, stream) {
  3001. options = options || {};
  3002. // object stream flag. Used to make read(n) ignore n and to
  3003. // make all the buffer merging and length checks go away
  3004. this.objectMode = !!options.objectMode;
  3005. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
  3006. // the point at which it stops calling _read() to fill the buffer
  3007. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  3008. var hwm = options.highWaterMark;
  3009. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  3010. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  3011. // cast to ints.
  3012. this.highWaterMark = ~ ~this.highWaterMark;
  3013. // A linked list is used to store data chunks instead of an array because the
  3014. // linked list can remove elements from the beginning faster than
  3015. // array.shift()
  3016. this.buffer = new BufferList();
  3017. this.length = 0;
  3018. this.pipes = null;
  3019. this.pipesCount = 0;
  3020. this.flowing = null;
  3021. this.ended = false;
  3022. this.endEmitted = false;
  3023. this.reading = false;
  3024. // a flag to be able to tell if the onwrite cb is called immediately,
  3025. // or on a later tick. We set this to true at first, because any
  3026. // actions that shouldn't happen until "later" should generally also
  3027. // not happen before the first write call.
  3028. this.sync = true;
  3029. // whenever we return null, then we set a flag to say
  3030. // that we're awaiting a 'readable' event emission.
  3031. this.needReadable = false;
  3032. this.emittedReadable = false;
  3033. this.readableListening = false;
  3034. this.resumeScheduled = false;
  3035. // Crypto is kind of old and crusty. Historically, its default string
  3036. // encoding is 'binary' so we have to make this configurable.
  3037. // Everything else in the universe uses 'utf8', though.
  3038. this.defaultEncoding = options.defaultEncoding || 'utf8';
  3039. // when piping, we only care about 'readable' events that happen
  3040. // after read()ing all the bytes and not getting any pushback.
  3041. this.ranOut = false;
  3042. // the number of writers that are awaiting a drain event in .pipe()s
  3043. this.awaitDrain = 0;
  3044. // if true, a maybeReadMore has been scheduled
  3045. this.readingMore = false;
  3046. this.decoder = null;
  3047. this.encoding = null;
  3048. if (options.encoding) {
  3049. this.decoder = new StringDecoder(options.encoding);
  3050. this.encoding = options.encoding;
  3051. }
  3052. }
  3053. function Readable(options) {
  3054. if (!(this instanceof Readable)) return new Readable(options);
  3055. this._readableState = new ReadableState(options, this);
  3056. // legacy
  3057. this.readable = true;
  3058. if (options && typeof options.read === 'function') this._read = options.read;
  3059. EventEmitter.call(this);
  3060. }
  3061. // Manually shove something into the read() buffer.
  3062. // This returns true if the highWaterMark has not been hit yet,
  3063. // similar to how Writable.write() returns true if you should
  3064. // write() some more.
  3065. Readable.prototype.push = function (chunk, encoding) {
  3066. var state = this._readableState;
  3067. if (!state.objectMode && typeof chunk === 'string') {
  3068. encoding = encoding || state.defaultEncoding;
  3069. if (encoding !== state.encoding) {
  3070. chunk = Buffer.from(chunk, encoding);
  3071. encoding = '';
  3072. }
  3073. }
  3074. return readableAddChunk(this, state, chunk, encoding, false);
  3075. };
  3076. // Unshift should *always* be something directly out of read()
  3077. Readable.prototype.unshift = function (chunk) {
  3078. var state = this._readableState;
  3079. return readableAddChunk(this, state, chunk, '', true);
  3080. };
  3081. Readable.prototype.isPaused = function () {
  3082. return this._readableState.flowing === false;
  3083. };
  3084. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  3085. var er = chunkInvalid(state, chunk);
  3086. if (er) {
  3087. stream.emit('error', er);
  3088. } else if (chunk === null) {
  3089. state.reading = false;
  3090. onEofChunk(stream, state);
  3091. } else if (state.objectMode || chunk && chunk.length > 0) {
  3092. if (state.ended && !addToFront) {
  3093. var e = new Error('stream.push() after EOF');
  3094. stream.emit('error', e);
  3095. } else if (state.endEmitted && addToFront) {
  3096. var _e = new Error('stream.unshift() after end event');
  3097. stream.emit('error', _e);
  3098. } else {
  3099. var skipAdd;
  3100. if (state.decoder && !addToFront && !encoding) {
  3101. chunk = state.decoder.write(chunk);
  3102. skipAdd = !state.objectMode && chunk.length === 0;
  3103. }
  3104. if (!addToFront) state.reading = false;
  3105. // Don't add to the buffer if we've decoded to an empty string chunk and
  3106. // we're not in object mode
  3107. if (!skipAdd) {
  3108. // if we want the data now, just emit it.
  3109. if (state.flowing && state.length === 0 && !state.sync) {
  3110. stream.emit('data', chunk);
  3111. stream.read(0);
  3112. } else {
  3113. // update the buffer info.
  3114. state.length += state.objectMode ? 1 : chunk.length;
  3115. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  3116. if (state.needReadable) emitReadable(stream);
  3117. }
  3118. }
  3119. maybeReadMore(stream, state);
  3120. }
  3121. } else if (!addToFront) {
  3122. state.reading = false;
  3123. }
  3124. return needMoreData(state);
  3125. }
  3126. // if it's past the high water mark, we can push in some more.
  3127. // Also, if we have no data yet, we can stand some
  3128. // more bytes. This is to work around cases where hwm=0,
  3129. // such as the repl. Also, if the push() triggered a
  3130. // readable event, and the user called read(largeNumber) such that
  3131. // needReadable was set, then we ought to push more, so that another
  3132. // 'readable' event will be triggered.
  3133. function needMoreData(state) {
  3134. return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
  3135. }
  3136. // backwards compatibility.
  3137. Readable.prototype.setEncoding = function (enc) {
  3138. this._readableState.decoder = new StringDecoder(enc);
  3139. this._readableState.encoding = enc;
  3140. return this;
  3141. };
  3142. // Don't raise the hwm > 8MB
  3143. var MAX_HWM = 0x800000;
  3144. function computeNewHighWaterMark(n) {
  3145. if (n >= MAX_HWM) {
  3146. n = MAX_HWM;
  3147. } else {
  3148. // Get the next highest power of 2 to prevent increasing hwm excessively in
  3149. // tiny amounts
  3150. n--;
  3151. n |= n >>> 1;
  3152. n |= n >>> 2;
  3153. n |= n >>> 4;
  3154. n |= n >>> 8;
  3155. n |= n >>> 16;
  3156. n++;
  3157. }
  3158. return n;
  3159. }
  3160. // This function is designed to be inlinable, so please take care when making
  3161. // changes to the function body.
  3162. function howMuchToRead(n, state) {
  3163. if (n <= 0 || state.length === 0 && state.ended) return 0;
  3164. if (state.objectMode) return 1;
  3165. if (n !== n) {
  3166. // Only flow one buffer at a time
  3167. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  3168. }
  3169. // If we're asking for more than the current hwm, then raise the hwm.
  3170. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  3171. if (n <= state.length) return n;
  3172. // Don't have enough
  3173. if (!state.ended) {
  3174. state.needReadable = true;
  3175. return 0;
  3176. }
  3177. return state.length;
  3178. }
  3179. // you can override either this method, or the async _read(n) below.
  3180. Readable.prototype.read = function (n) {
  3181. debug('read', n);
  3182. n = parseInt(n, 10);
  3183. var state = this._readableState;
  3184. var nOrig = n;
  3185. if (n !== 0) state.emittedReadable = false;
  3186. // if we're doing read(0) to trigger a readable event, but we
  3187. // already have a bunch of data in the buffer, then just trigger
  3188. // the 'readable' event and move on.
  3189. if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
  3190. debug('read: emitReadable', state.length, state.ended);
  3191. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  3192. return null;
  3193. }
  3194. n = howMuchToRead(n, state);
  3195. // if we've ended, and we're now clear, then finish it up.
  3196. if (n === 0 && state.ended) {
  3197. if (state.length === 0) endReadable(this);
  3198. return null;
  3199. }
  3200. // All the actual chunk generation logic needs to be
  3201. // *below* the call to _read. The reason is that in certain
  3202. // synthetic stream cases, such as passthrough streams, _read
  3203. // may be a completely synchronous operation which may change
  3204. // the state of the read buffer, providing enough data when
  3205. // before there was *not* enough.
  3206. //
  3207. // So, the steps are:
  3208. // 1. Figure out what the state of things will be after we do
  3209. // a read from the buffer.
  3210. //
  3211. // 2. If that resulting state will trigger a _read, then call _read.
  3212. // Note that this may be asynchronous, or synchronous. Yes, it is
  3213. // deeply ugly to write APIs this way, but that still doesn't mean
  3214. // that the Readable class should behave improperly, as streams are
  3215. // designed to be sync/async agnostic.
  3216. // Take note if the _read call is sync or async (ie, if the read call
  3217. // has returned yet), so that we know whether or not it's safe to emit
  3218. // 'readable' etc.
  3219. //
  3220. // 3. Actually pull the requested chunks out of the buffer and return.
  3221. // if we need a readable event, then we need to do some reading.
  3222. var doRead = state.needReadable;
  3223. debug('need readable', doRead);
  3224. // if we currently have less than the highWaterMark, then also read some
  3225. if (state.length === 0 || state.length - n < state.highWaterMark) {
  3226. doRead = true;
  3227. debug('length less than watermark', doRead);
  3228. }
  3229. // however, if we've ended, then there's no point, and if we're already
  3230. // reading, then it's unnecessary.
  3231. if (state.ended || state.reading) {
  3232. doRead = false;
  3233. debug('reading or ended', doRead);
  3234. } else if (doRead) {
  3235. debug('do read');
  3236. state.reading = true;
  3237. state.sync = true;
  3238. // if the length is currently zero, then we *need* a readable event.
  3239. if (state.length === 0) state.needReadable = true;
  3240. // call internal read method
  3241. this._read(state.highWaterMark);
  3242. state.sync = false;
  3243. // If _read pushed data synchronously, then `reading` will be false,
  3244. // and we need to re-evaluate how much data we can return to the user.
  3245. if (!state.reading) n = howMuchToRead(nOrig, state);
  3246. }
  3247. var ret;
  3248. if (n > 0) ret = fromList(n, state);else ret = null;
  3249. if (ret === null) {
  3250. state.needReadable = true;
  3251. n = 0;
  3252. } else {
  3253. state.length -= n;
  3254. }
  3255. if (state.length === 0) {
  3256. // If we have nothing in the buffer, then we want to know
  3257. // as soon as we *do* get something into the buffer.
  3258. if (!state.ended) state.needReadable = true;
  3259. // If we tried to read() past the EOF, then emit end on the next tick.
  3260. if (nOrig !== n && state.ended) endReadable(this);
  3261. }
  3262. if (ret !== null) this.emit('data', ret);
  3263. return ret;
  3264. };
  3265. function chunkInvalid(state, chunk) {
  3266. var er = null;
  3267. if (!isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
  3268. er = new TypeError('Invalid non-string/buffer chunk');
  3269. }
  3270. return er;
  3271. }
  3272. function onEofChunk(stream, state) {
  3273. if (state.ended) return;
  3274. if (state.decoder) {
  3275. var chunk = state.decoder.end();
  3276. if (chunk && chunk.length) {
  3277. state.buffer.push(chunk);
  3278. state.length += state.objectMode ? 1 : chunk.length;
  3279. }
  3280. }
  3281. state.ended = true;
  3282. // emit 'readable' now to make sure it gets picked up.
  3283. emitReadable(stream);
  3284. }
  3285. // Don't emit readable right away in sync mode, because this can trigger
  3286. // another read() call => stack overflow. This way, it might trigger
  3287. // a nextTick recursion warning, but that's not so bad.
  3288. function emitReadable(stream) {
  3289. var state = stream._readableState;
  3290. state.needReadable = false;
  3291. if (!state.emittedReadable) {
  3292. debug('emitReadable', state.flowing);
  3293. state.emittedReadable = true;
  3294. if (state.sync) nextTick(emitReadable_, stream);else emitReadable_(stream);
  3295. }
  3296. }
  3297. function emitReadable_(stream) {
  3298. debug('emit readable');
  3299. stream.emit('readable');
  3300. flow(stream);
  3301. }
  3302. // at this point, the user has presumably seen the 'readable' event,
  3303. // and called read() to consume some data. that may have triggered
  3304. // in turn another _read(n) call, in which case reading = true if
  3305. // it's in progress.
  3306. // However, if we're not ended, or reading, and the length < hwm,
  3307. // then go ahead and try to read some more preemptively.
  3308. function maybeReadMore(stream, state) {
  3309. if (!state.readingMore) {
  3310. state.readingMore = true;
  3311. nextTick(maybeReadMore_, stream, state);
  3312. }
  3313. }
  3314. function maybeReadMore_(stream, state) {
  3315. var len = state.length;
  3316. while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
  3317. debug('maybeReadMore read 0');
  3318. stream.read(0);
  3319. if (len === state.length)
  3320. // didn't get any data, stop spinning.
  3321. break;else len = state.length;
  3322. }
  3323. state.readingMore = false;
  3324. }
  3325. // abstract method. to be overridden in specific implementation classes.
  3326. // call cb(er, data) where data is <= n in length.
  3327. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  3328. // arbitrary, and perhaps not very meaningful.
  3329. Readable.prototype._read = function (n) {
  3330. this.emit('error', new Error('not implemented'));
  3331. };
  3332. Readable.prototype.pipe = function (dest, pipeOpts) {
  3333. var src = this;
  3334. var state = this._readableState;
  3335. switch (state.pipesCount) {
  3336. case 0:
  3337. state.pipes = dest;
  3338. break;
  3339. case 1:
  3340. state.pipes = [state.pipes, dest];
  3341. break;
  3342. default:
  3343. state.pipes.push(dest);
  3344. break;
  3345. }
  3346. state.pipesCount += 1;
  3347. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  3348. var doEnd = (!pipeOpts || pipeOpts.end !== false);
  3349. var endFn = doEnd ? onend : cleanup;
  3350. if (state.endEmitted) nextTick(endFn);else src.once('end', endFn);
  3351. dest.on('unpipe', onunpipe);
  3352. function onunpipe(readable) {
  3353. debug('onunpipe');
  3354. if (readable === src) {
  3355. cleanup();
  3356. }
  3357. }
  3358. function onend() {
  3359. debug('onend');
  3360. dest.end();
  3361. }
  3362. // when the dest drains, it reduces the awaitDrain counter
  3363. // on the source. This would be more elegant with a .once()
  3364. // handler in flow(), but adding and removing repeatedly is
  3365. // too slow.
  3366. var ondrain = pipeOnDrain(src);
  3367. dest.on('drain', ondrain);
  3368. var cleanedUp = false;
  3369. function cleanup() {
  3370. debug('cleanup');
  3371. // cleanup event handlers once the pipe is broken
  3372. dest.removeListener('close', onclose);
  3373. dest.removeListener('finish', onfinish);
  3374. dest.removeListener('drain', ondrain);
  3375. dest.removeListener('error', onerror);
  3376. dest.removeListener('unpipe', onunpipe);
  3377. src.removeListener('end', onend);
  3378. src.removeListener('end', cleanup);
  3379. src.removeListener('data', ondata);
  3380. cleanedUp = true;
  3381. // if the reader is waiting for a drain event from this
  3382. // specific writer, then it would cause it to never start
  3383. // flowing again.
  3384. // So, if this is awaiting a drain, then we just call it now.
  3385. // If we don't know, then assume that we are waiting for one.
  3386. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  3387. }
  3388. // If the user pushes more data while we're writing to dest then we'll end up
  3389. // in ondata again. However, we only want to increase awaitDrain once because
  3390. // dest will only emit one 'drain' event for the multiple writes.
  3391. // => Introduce a guard on increasing awaitDrain.
  3392. var increasedAwaitDrain = false;
  3393. src.on('data', ondata);
  3394. function ondata(chunk) {
  3395. debug('ondata');
  3396. increasedAwaitDrain = false;
  3397. var ret = dest.write(chunk);
  3398. if (false === ret && !increasedAwaitDrain) {
  3399. // If the user unpiped during `dest.write()`, it is possible
  3400. // to get stuck in a permanently paused state if that write
  3401. // also returned false.
  3402. // => Check whether `dest` is still a piping destination.
  3403. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  3404. debug('false write response, pause', src._readableState.awaitDrain);
  3405. src._readableState.awaitDrain++;
  3406. increasedAwaitDrain = true;
  3407. }
  3408. src.pause();
  3409. }
  3410. }
  3411. // if the dest has an error, then stop piping into it.
  3412. // however, don't suppress the throwing behavior for this.
  3413. function onerror(er) {
  3414. debug('onerror', er);
  3415. unpipe();
  3416. dest.removeListener('error', onerror);
  3417. if (listenerCount(dest, 'error') === 0) dest.emit('error', er);
  3418. }
  3419. // Make sure our error handler is attached before userland ones.
  3420. prependListener(dest, 'error', onerror);
  3421. // Both close and finish should trigger unpipe, but only once.
  3422. function onclose() {
  3423. dest.removeListener('finish', onfinish);
  3424. unpipe();
  3425. }
  3426. dest.once('close', onclose);
  3427. function onfinish() {
  3428. debug('onfinish');
  3429. dest.removeListener('close', onclose);
  3430. unpipe();
  3431. }
  3432. dest.once('finish', onfinish);
  3433. function unpipe() {
  3434. debug('unpipe');
  3435. src.unpipe(dest);
  3436. }
  3437. // tell the dest that it's being piped to
  3438. dest.emit('pipe', src);
  3439. // start the flow if it hasn't been started already.
  3440. if (!state.flowing) {
  3441. debug('pipe resume');
  3442. src.resume();
  3443. }
  3444. return dest;
  3445. };
  3446. function pipeOnDrain(src) {
  3447. return function () {
  3448. var state = src._readableState;
  3449. debug('pipeOnDrain', state.awaitDrain);
  3450. if (state.awaitDrain) state.awaitDrain--;
  3451. if (state.awaitDrain === 0 && src.listeners('data').length) {
  3452. state.flowing = true;
  3453. flow(src);
  3454. }
  3455. };
  3456. }
  3457. Readable.prototype.unpipe = function (dest) {
  3458. var state = this._readableState;
  3459. // if we're not piping anywhere, then do nothing.
  3460. if (state.pipesCount === 0) return this;
  3461. // just one destination. most common case.
  3462. if (state.pipesCount === 1) {
  3463. // passed in one, but it's not the right one.
  3464. if (dest && dest !== state.pipes) return this;
  3465. if (!dest) dest = state.pipes;
  3466. // got a match.
  3467. state.pipes = null;
  3468. state.pipesCount = 0;
  3469. state.flowing = false;
  3470. if (dest) dest.emit('unpipe', this);
  3471. return this;
  3472. }
  3473. // slow case. multiple pipe destinations.
  3474. if (!dest) {
  3475. // remove all.
  3476. var dests = state.pipes;
  3477. var len = state.pipesCount;
  3478. state.pipes = null;
  3479. state.pipesCount = 0;
  3480. state.flowing = false;
  3481. for (var _i = 0; _i < len; _i++) {
  3482. dests[_i].emit('unpipe', this);
  3483. }return this;
  3484. }
  3485. // try to find the right one.
  3486. var i = indexOf(state.pipes, dest);
  3487. if (i === -1) return this;
  3488. state.pipes.splice(i, 1);
  3489. state.pipesCount -= 1;
  3490. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  3491. dest.emit('unpipe', this);
  3492. return this;
  3493. };
  3494. // set up data events if they are asked for
  3495. // Ensure readable listeners eventually get something
  3496. Readable.prototype.on = function (ev, fn) {
  3497. var res = EventEmitter.prototype.on.call(this, ev, fn);
  3498. if (ev === 'data') {
  3499. // Start flowing on next tick if stream isn't explicitly paused
  3500. if (this._readableState.flowing !== false) this.resume();
  3501. } else if (ev === 'readable') {
  3502. var state = this._readableState;
  3503. if (!state.endEmitted && !state.readableListening) {
  3504. state.readableListening = state.needReadable = true;
  3505. state.emittedReadable = false;
  3506. if (!state.reading) {
  3507. nextTick(nReadingNextTick, this);
  3508. } else if (state.length) {
  3509. emitReadable(this);
  3510. }
  3511. }
  3512. }
  3513. return res;
  3514. };
  3515. Readable.prototype.addListener = Readable.prototype.on;
  3516. function nReadingNextTick(self) {
  3517. debug('readable nexttick read 0');
  3518. self.read(0);
  3519. }
  3520. // pause() and resume() are remnants of the legacy readable stream API
  3521. // If the user uses them, then switch into old mode.
  3522. Readable.prototype.resume = function () {
  3523. var state = this._readableState;
  3524. if (!state.flowing) {
  3525. debug('resume');
  3526. state.flowing = true;
  3527. resume(this, state);
  3528. }
  3529. return this;
  3530. };
  3531. function resume(stream, state) {
  3532. if (!state.resumeScheduled) {
  3533. state.resumeScheduled = true;
  3534. nextTick(resume_, stream, state);
  3535. }
  3536. }
  3537. function resume_(stream, state) {
  3538. if (!state.reading) {
  3539. debug('resume read 0');
  3540. stream.read(0);
  3541. }
  3542. state.resumeScheduled = false;
  3543. state.awaitDrain = 0;
  3544. stream.emit('resume');
  3545. flow(stream);
  3546. if (state.flowing && !state.reading) stream.read(0);
  3547. }
  3548. Readable.prototype.pause = function () {
  3549. debug('call pause flowing=%j', this._readableState.flowing);
  3550. if (false !== this._readableState.flowing) {
  3551. debug('pause');
  3552. this._readableState.flowing = false;
  3553. this.emit('pause');
  3554. }
  3555. return this;
  3556. };
  3557. function flow(stream) {
  3558. var state = stream._readableState;
  3559. debug('flow', state.flowing);
  3560. while (state.flowing && stream.read() !== null) {}
  3561. }
  3562. // wrap an old-style stream as the async data source.
  3563. // This is *not* part of the readable stream interface.
  3564. // It is an ugly unfortunate mess of history.
  3565. Readable.prototype.wrap = function (stream) {
  3566. var state = this._readableState;
  3567. var paused = false;
  3568. var self = this;
  3569. stream.on('end', function () {
  3570. debug('wrapped end');
  3571. if (state.decoder && !state.ended) {
  3572. var chunk = state.decoder.end();
  3573. if (chunk && chunk.length) self.push(chunk);
  3574. }
  3575. self.push(null);
  3576. });
  3577. stream.on('data', function (chunk) {
  3578. debug('wrapped data');
  3579. if (state.decoder) chunk = state.decoder.write(chunk);
  3580. // don't skip over falsy values in objectMode
  3581. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  3582. var ret = self.push(chunk);
  3583. if (!ret) {
  3584. paused = true;
  3585. stream.pause();
  3586. }
  3587. });
  3588. // proxy all the other methods.
  3589. // important when wrapping filters and duplexes.
  3590. for (var i in stream) {
  3591. if (this[i] === undefined && typeof stream[i] === 'function') {
  3592. this[i] = function (method) {
  3593. return function () {
  3594. return stream[method].apply(stream, arguments);
  3595. };
  3596. }(i);
  3597. }
  3598. }
  3599. // proxy certain important events.
  3600. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  3601. forEach(events, function (ev) {
  3602. stream.on(ev, self.emit.bind(self, ev));
  3603. });
  3604. // when we try to consume some more bytes, simply unpause the
  3605. // underlying stream.
  3606. self._read = function (n) {
  3607. debug('wrapped _read', n);
  3608. if (paused) {
  3609. paused = false;
  3610. stream.resume();
  3611. }
  3612. };
  3613. return self;
  3614. };
  3615. // exposed for testing purposes only.
  3616. Readable._fromList = fromList;
  3617. // Pluck off n bytes from an array of buffers.
  3618. // Length is the combined lengths of all the buffers in the list.
  3619. // This function is designed to be inlinable, so please take care when making
  3620. // changes to the function body.
  3621. function fromList(n, state) {
  3622. // nothing buffered
  3623. if (state.length === 0) return null;
  3624. var ret;
  3625. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  3626. // read it all, truncate the list
  3627. if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length);
  3628. state.buffer.clear();
  3629. } else {
  3630. // read part of list
  3631. ret = fromListPartial(n, state.buffer, state.decoder);
  3632. }
  3633. return ret;
  3634. }
  3635. // Extracts only enough buffered data to satisfy the amount requested.
  3636. // This function is designed to be inlinable, so please take care when making
  3637. // changes to the function body.
  3638. function fromListPartial(n, list, hasStrings) {
  3639. var ret;
  3640. if (n < list.head.data.length) {
  3641. // slice is the same for buffers and strings
  3642. ret = list.head.data.slice(0, n);
  3643. list.head.data = list.head.data.slice(n);
  3644. } else if (n === list.head.data.length) {
  3645. // first chunk is a perfect match
  3646. ret = list.shift();
  3647. } else {
  3648. // result spans more than one buffer
  3649. ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
  3650. }
  3651. return ret;
  3652. }
  3653. // Copies a specified amount of characters from the list of buffered data
  3654. // chunks.
  3655. // This function is designed to be inlinable, so please take care when making
  3656. // changes to the function body.
  3657. function copyFromBufferString(n, list) {
  3658. var p = list.head;
  3659. var c = 1;
  3660. var ret = p.data;
  3661. n -= ret.length;
  3662. while (p = p.next) {
  3663. var str = p.data;
  3664. var nb = n > str.length ? str.length : n;
  3665. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  3666. n -= nb;
  3667. if (n === 0) {
  3668. if (nb === str.length) {
  3669. ++c;
  3670. if (p.next) list.head = p.next;else list.head = list.tail = null;
  3671. } else {
  3672. list.head = p;
  3673. p.data = str.slice(nb);
  3674. }
  3675. break;
  3676. }
  3677. ++c;
  3678. }
  3679. list.length -= c;
  3680. return ret;
  3681. }
  3682. // Copies a specified amount of bytes from the list of buffered data chunks.
  3683. // This function is designed to be inlinable, so please take care when making
  3684. // changes to the function body.
  3685. function copyFromBuffer(n, list) {
  3686. var ret = Buffer.allocUnsafe(n);
  3687. var p = list.head;
  3688. var c = 1;
  3689. p.data.copy(ret);
  3690. n -= p.data.length;
  3691. while (p = p.next) {
  3692. var buf = p.data;
  3693. var nb = n > buf.length ? buf.length : n;
  3694. buf.copy(ret, ret.length - n, 0, nb);
  3695. n -= nb;
  3696. if (n === 0) {
  3697. if (nb === buf.length) {
  3698. ++c;
  3699. if (p.next) list.head = p.next;else list.head = list.tail = null;
  3700. } else {
  3701. list.head = p;
  3702. p.data = buf.slice(nb);
  3703. }
  3704. break;
  3705. }
  3706. ++c;
  3707. }
  3708. list.length -= c;
  3709. return ret;
  3710. }
  3711. function endReadable(stream) {
  3712. var state = stream._readableState;
  3713. // If we get here before consuming all the bytes, then that is a
  3714. // bug in node. Should never happen.
  3715. if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
  3716. if (!state.endEmitted) {
  3717. state.ended = true;
  3718. nextTick(endReadableNT, state, stream);
  3719. }
  3720. }
  3721. function endReadableNT(state, stream) {
  3722. // Check that we didn't get one last unshift.
  3723. if (!state.endEmitted && state.length === 0) {
  3724. state.endEmitted = true;
  3725. stream.readable = false;
  3726. stream.emit('end');
  3727. }
  3728. }
  3729. function forEach(xs, f) {
  3730. for (var i = 0, l = xs.length; i < l; i++) {
  3731. f(xs[i], i);
  3732. }
  3733. }
  3734. function indexOf(xs, x) {
  3735. for (var i = 0, l = xs.length; i < l; i++) {
  3736. if (xs[i] === x) return i;
  3737. }
  3738. return -1;
  3739. }
  3740. // A bit simpler than readable streams.
  3741. // Implement an async ._write(chunk, encoding, cb), and it'll handle all
  3742. // the drain event emission and buffering.
  3743. Writable.WritableState = WritableState;
  3744. inherits$1(Writable, EventEmitter);
  3745. function nop() {}
  3746. function WriteReq(chunk, encoding, cb) {
  3747. this.chunk = chunk;
  3748. this.encoding = encoding;
  3749. this.callback = cb;
  3750. this.next = null;
  3751. }
  3752. function WritableState(options, stream) {
  3753. Object.defineProperty(this, 'buffer', {
  3754. get: deprecate(function () {
  3755. return this.getBuffer();
  3756. }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.')
  3757. });
  3758. options = options || {};
  3759. // object stream flag to indicate whether or not this stream
  3760. // contains buffers or objects.
  3761. this.objectMode = !!options.objectMode;
  3762. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode;
  3763. // the point at which write() starts returning false
  3764. // Note: 0 is a valid value, means that we always return false if
  3765. // the entire buffer is not flushed immediately on write()
  3766. var hwm = options.highWaterMark;
  3767. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  3768. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  3769. // cast to ints.
  3770. this.highWaterMark = ~ ~this.highWaterMark;
  3771. this.needDrain = false;
  3772. // at the start of calling end()
  3773. this.ending = false;
  3774. // when end() has been called, and returned
  3775. this.ended = false;
  3776. // when 'finish' is emitted
  3777. this.finished = false;
  3778. // should we decode strings into buffers before passing to _write?
  3779. // this is here so that some node-core streams can optimize string
  3780. // handling at a lower level.
  3781. var noDecode = options.decodeStrings === false;
  3782. this.decodeStrings = !noDecode;
  3783. // Crypto is kind of old and crusty. Historically, its default string
  3784. // encoding is 'binary' so we have to make this configurable.
  3785. // Everything else in the universe uses 'utf8', though.
  3786. this.defaultEncoding = options.defaultEncoding || 'utf8';
  3787. // not an actual buffer we keep track of, but a measurement
  3788. // of how much we're waiting to get pushed to some underlying
  3789. // socket or file.
  3790. this.length = 0;
  3791. // a flag to see when we're in the middle of a write.
  3792. this.writing = false;
  3793. // when true all writes will be buffered until .uncork() call
  3794. this.corked = 0;
  3795. // a flag to be able to tell if the onwrite cb is called immediately,
  3796. // or on a later tick. We set this to true at first, because any
  3797. // actions that shouldn't happen until "later" should generally also
  3798. // not happen before the first write call.
  3799. this.sync = true;
  3800. // a flag to know if we're processing previously buffered items, which
  3801. // may call the _write() callback in the same tick, so that we don't
  3802. // end up in an overlapped onwrite situation.
  3803. this.bufferProcessing = false;
  3804. // the callback that's passed to _write(chunk,cb)
  3805. this.onwrite = function (er) {
  3806. onwrite(stream, er);
  3807. };
  3808. // the callback that the user supplies to write(chunk,encoding,cb)
  3809. this.writecb = null;
  3810. // the amount that is being written when _write is called.
  3811. this.writelen = 0;
  3812. this.bufferedRequest = null;
  3813. this.lastBufferedRequest = null;
  3814. // number of pending user-supplied write callbacks
  3815. // this must be 0 before 'finish' can be emitted
  3816. this.pendingcb = 0;
  3817. // emit prefinish if the only thing we're waiting for is _write cbs
  3818. // This is relevant for synchronous Transform streams
  3819. this.prefinished = false;
  3820. // True if the error was already emitted and should not be thrown again
  3821. this.errorEmitted = false;
  3822. // count buffered requests
  3823. this.bufferedRequestCount = 0;
  3824. // allocate the first CorkedRequest, there is always
  3825. // one allocated and free to use, and we maintain at most two
  3826. this.corkedRequestsFree = new CorkedRequest(this);
  3827. }
  3828. WritableState.prototype.getBuffer = function writableStateGetBuffer() {
  3829. var current = this.bufferedRequest;
  3830. var out = [];
  3831. while (current) {
  3832. out.push(current);
  3833. current = current.next;
  3834. }
  3835. return out;
  3836. };
  3837. function Writable(options) {
  3838. // Writable ctor is applied to Duplexes, though they're not
  3839. // instanceof Writable, they're instanceof Readable.
  3840. if (!(this instanceof Writable) && !(this instanceof Duplex)) return new Writable(options);
  3841. this._writableState = new WritableState(options, this);
  3842. // legacy.
  3843. this.writable = true;
  3844. if (options) {
  3845. if (typeof options.write === 'function') this._write = options.write;
  3846. if (typeof options.writev === 'function') this._writev = options.writev;
  3847. }
  3848. EventEmitter.call(this);
  3849. }
  3850. // Otherwise people can pipe Writable streams, which is just wrong.
  3851. Writable.prototype.pipe = function () {
  3852. this.emit('error', new Error('Cannot pipe, not readable'));
  3853. };
  3854. function writeAfterEnd(stream, cb) {
  3855. var er = new Error('write after end');
  3856. // TODO: defer error events consistently everywhere, not just the cb
  3857. stream.emit('error', er);
  3858. nextTick(cb, er);
  3859. }
  3860. // If we get something that is not a buffer, string, null, or undefined,
  3861. // and we're not in objectMode, then that's an error.
  3862. // Otherwise stream chunks are all considered to be of length=1, and the
  3863. // watermarks determine how many objects to keep in the buffer, rather than
  3864. // how many bytes or characters.
  3865. function validChunk(stream, state, chunk, cb) {
  3866. var valid = true;
  3867. var er = false;
  3868. // Always throw error if a null is written
  3869. // if we are not in object mode then throw
  3870. // if it is not a buffer, string, or undefined.
  3871. if (chunk === null) {
  3872. er = new TypeError('May not write null values to stream');
  3873. } else if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
  3874. er = new TypeError('Invalid non-string/buffer chunk');
  3875. }
  3876. if (er) {
  3877. stream.emit('error', er);
  3878. nextTick(cb, er);
  3879. valid = false;
  3880. }
  3881. return valid;
  3882. }
  3883. Writable.prototype.write = function (chunk, encoding, cb) {
  3884. var state = this._writableState;
  3885. var ret = false;
  3886. if (typeof encoding === 'function') {
  3887. cb = encoding;
  3888. encoding = null;
  3889. }
  3890. if (Buffer.isBuffer(chunk)) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
  3891. if (typeof cb !== 'function') cb = nop;
  3892. if (state.ended) writeAfterEnd(this, cb);else if (validChunk(this, state, chunk, cb)) {
  3893. state.pendingcb++;
  3894. ret = writeOrBuffer(this, state, chunk, encoding, cb);
  3895. }
  3896. return ret;
  3897. };
  3898. Writable.prototype.cork = function () {
  3899. var state = this._writableState;
  3900. state.corked++;
  3901. };
  3902. Writable.prototype.uncork = function () {
  3903. var state = this._writableState;
  3904. if (state.corked) {
  3905. state.corked--;
  3906. if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
  3907. }
  3908. };
  3909. Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
  3910. // node::ParseEncoding() requires lower case.
  3911. if (typeof encoding === 'string') encoding = encoding.toLowerCase();
  3912. if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding);
  3913. this._writableState.defaultEncoding = encoding;
  3914. return this;
  3915. };
  3916. function decodeChunk(state, chunk, encoding) {
  3917. if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
  3918. chunk = Buffer.from(chunk, encoding);
  3919. }
  3920. return chunk;
  3921. }
  3922. // if we're already writing something, then just put this
  3923. // in the queue, and wait our turn. Otherwise, call _write
  3924. // If we return false, then we need a drain event, so set that flag.
  3925. function writeOrBuffer(stream, state, chunk, encoding, cb) {
  3926. chunk = decodeChunk(state, chunk, encoding);
  3927. if (Buffer.isBuffer(chunk)) encoding = 'buffer';
  3928. var len = state.objectMode ? 1 : chunk.length;
  3929. state.length += len;
  3930. var ret = state.length < state.highWaterMark;
  3931. // we must ensure that previous needDrain will not be reset to false.
  3932. if (!ret) state.needDrain = true;
  3933. if (state.writing || state.corked) {
  3934. var last = state.lastBufferedRequest;
  3935. state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
  3936. if (last) {
  3937. last.next = state.lastBufferedRequest;
  3938. } else {
  3939. state.bufferedRequest = state.lastBufferedRequest;
  3940. }
  3941. state.bufferedRequestCount += 1;
  3942. } else {
  3943. doWrite(stream, state, false, len, chunk, encoding, cb);
  3944. }
  3945. return ret;
  3946. }
  3947. function doWrite(stream, state, writev, len, chunk, encoding, cb) {
  3948. state.writelen = len;
  3949. state.writecb = cb;
  3950. state.writing = true;
  3951. state.sync = true;
  3952. if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
  3953. state.sync = false;
  3954. }
  3955. function onwriteError(stream, state, sync, er, cb) {
  3956. --state.pendingcb;
  3957. if (sync) nextTick(cb, er);else cb(er);
  3958. stream._writableState.errorEmitted = true;
  3959. stream.emit('error', er);
  3960. }
  3961. function onwriteStateUpdate(state) {
  3962. state.writing = false;
  3963. state.writecb = null;
  3964. state.length -= state.writelen;
  3965. state.writelen = 0;
  3966. }
  3967. function onwrite(stream, er) {
  3968. var state = stream._writableState;
  3969. var sync = state.sync;
  3970. var cb = state.writecb;
  3971. onwriteStateUpdate(state);
  3972. if (er) onwriteError(stream, state, sync, er, cb);else {
  3973. // Check if we're actually ready to finish, but don't emit yet
  3974. var finished = needFinish(state);
  3975. if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
  3976. clearBuffer(stream, state);
  3977. }
  3978. if (sync) {
  3979. /*<replacement>*/
  3980. nextTick(afterWrite, stream, state, finished, cb);
  3981. /*</replacement>*/
  3982. } else {
  3983. afterWrite(stream, state, finished, cb);
  3984. }
  3985. }
  3986. }
  3987. function afterWrite(stream, state, finished, cb) {
  3988. if (!finished) onwriteDrain(stream, state);
  3989. state.pendingcb--;
  3990. cb();
  3991. finishMaybe(stream, state);
  3992. }
  3993. // Must force callback to be called on nextTick, so that we don't
  3994. // emit 'drain' before the write() consumer gets the 'false' return
  3995. // value, and has a chance to attach a 'drain' listener.
  3996. function onwriteDrain(stream, state) {
  3997. if (state.length === 0 && state.needDrain) {
  3998. state.needDrain = false;
  3999. stream.emit('drain');
  4000. }
  4001. }
  4002. // if there's something in the buffer waiting, then process it
  4003. function clearBuffer(stream, state) {
  4004. state.bufferProcessing = true;
  4005. var entry = state.bufferedRequest;
  4006. if (stream._writev && entry && entry.next) {
  4007. // Fast case, write everything using _writev()
  4008. var l = state.bufferedRequestCount;
  4009. var buffer = new Array(l);
  4010. var holder = state.corkedRequestsFree;
  4011. holder.entry = entry;
  4012. var count = 0;
  4013. while (entry) {
  4014. buffer[count] = entry;
  4015. entry = entry.next;
  4016. count += 1;
  4017. }
  4018. doWrite(stream, state, true, state.length, buffer, '', holder.finish);
  4019. // doWrite is almost always async, defer these to save a bit of time
  4020. // as the hot path ends with doWrite
  4021. state.pendingcb++;
  4022. state.lastBufferedRequest = null;
  4023. if (holder.next) {
  4024. state.corkedRequestsFree = holder.next;
  4025. holder.next = null;
  4026. } else {
  4027. state.corkedRequestsFree = new CorkedRequest(state);
  4028. }
  4029. } else {
  4030. // Slow case, write chunks one-by-one
  4031. while (entry) {
  4032. var chunk = entry.chunk;
  4033. var encoding = entry.encoding;
  4034. var cb = entry.callback;
  4035. var len = state.objectMode ? 1 : chunk.length;
  4036. doWrite(stream, state, false, len, chunk, encoding, cb);
  4037. entry = entry.next;
  4038. // if we didn't call the onwrite immediately, then
  4039. // it means that we need to wait until it does.
  4040. // also, that means that the chunk and cb are currently
  4041. // being processed, so move the buffer counter past them.
  4042. if (state.writing) {
  4043. break;
  4044. }
  4045. }
  4046. if (entry === null) state.lastBufferedRequest = null;
  4047. }
  4048. state.bufferedRequestCount = 0;
  4049. state.bufferedRequest = entry;
  4050. state.bufferProcessing = false;
  4051. }
  4052. Writable.prototype._write = function (chunk, encoding, cb) {
  4053. cb(new Error('not implemented'));
  4054. };
  4055. Writable.prototype._writev = null;
  4056. Writable.prototype.end = function (chunk, encoding, cb) {
  4057. var state = this._writableState;
  4058. if (typeof chunk === 'function') {
  4059. cb = chunk;
  4060. chunk = null;
  4061. encoding = null;
  4062. } else if (typeof encoding === 'function') {
  4063. cb = encoding;
  4064. encoding = null;
  4065. }
  4066. if (chunk !== null && chunk !== undefined) this.write(chunk, encoding);
  4067. // .end() fully uncorks
  4068. if (state.corked) {
  4069. state.corked = 1;
  4070. this.uncork();
  4071. }
  4072. // ignore unnecessary end() calls.
  4073. if (!state.ending && !state.finished) endWritable(this, state, cb);
  4074. };
  4075. function needFinish(state) {
  4076. return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
  4077. }
  4078. function prefinish(stream, state) {
  4079. if (!state.prefinished) {
  4080. state.prefinished = true;
  4081. stream.emit('prefinish');
  4082. }
  4083. }
  4084. function finishMaybe(stream, state) {
  4085. var need = needFinish(state);
  4086. if (need) {
  4087. if (state.pendingcb === 0) {
  4088. prefinish(stream, state);
  4089. state.finished = true;
  4090. stream.emit('finish');
  4091. } else {
  4092. prefinish(stream, state);
  4093. }
  4094. }
  4095. return need;
  4096. }
  4097. function endWritable(stream, state, cb) {
  4098. state.ending = true;
  4099. finishMaybe(stream, state);
  4100. if (cb) {
  4101. if (state.finished) nextTick(cb);else stream.once('finish', cb);
  4102. }
  4103. state.ended = true;
  4104. stream.writable = false;
  4105. }
  4106. // It seems a linked list but it is not
  4107. // there will be only 2 of these for each stream
  4108. function CorkedRequest(state) {
  4109. var _this = this;
  4110. this.next = null;
  4111. this.entry = null;
  4112. this.finish = function (err) {
  4113. var entry = _this.entry;
  4114. _this.entry = null;
  4115. while (entry) {
  4116. var cb = entry.callback;
  4117. state.pendingcb--;
  4118. cb(err);
  4119. entry = entry.next;
  4120. }
  4121. if (state.corkedRequestsFree) {
  4122. state.corkedRequestsFree.next = _this;
  4123. } else {
  4124. state.corkedRequestsFree = _this;
  4125. }
  4126. };
  4127. }
  4128. inherits$1(Duplex, Readable);
  4129. var keys = Object.keys(Writable.prototype);
  4130. for (var v = 0; v < keys.length; v++) {
  4131. var method = keys[v];
  4132. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  4133. }
  4134. function Duplex(options) {
  4135. if (!(this instanceof Duplex)) return new Duplex(options);
  4136. Readable.call(this, options);
  4137. Writable.call(this, options);
  4138. if (options && options.readable === false) this.readable = false;
  4139. if (options && options.writable === false) this.writable = false;
  4140. this.allowHalfOpen = true;
  4141. if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
  4142. this.once('end', onend);
  4143. }
  4144. // the no-half-open enforcer
  4145. function onend() {
  4146. // if we allow half-open state, or if the writable side ended,
  4147. // then we're ok.
  4148. if (this.allowHalfOpen || this._writableState.ended) return;
  4149. // no more data can be written.
  4150. // But allow more writes to happen in this tick.
  4151. nextTick(onEndNT, this);
  4152. }
  4153. function onEndNT(self) {
  4154. self.end();
  4155. }
  4156. // a transform stream is a readable/writable stream where you do
  4157. // something with the data. Sometimes it's called a "filter",
  4158. // but that's not a great name for it, since that implies a thing where
  4159. // some bits pass through, and others are simply ignored. (That would
  4160. // be a valid example of a transform, of course.)
  4161. //
  4162. // While the output is causally related to the input, it's not a
  4163. // necessarily symmetric or synchronous transformation. For example,
  4164. // a zlib stream might take multiple plain-text writes(), and then
  4165. // emit a single compressed chunk some time in the future.
  4166. //
  4167. // Here's how this works:
  4168. //
  4169. // The Transform stream has all the aspects of the readable and writable
  4170. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  4171. // internally, and returns false if there's a lot of pending writes
  4172. // buffered up. When you call read(), that calls _read(n) until
  4173. // there's enough pending readable data buffered up.
  4174. //
  4175. // In a transform stream, the written data is placed in a buffer. When
  4176. // _read(n) is called, it transforms the queued up data, calling the
  4177. // buffered _write cb's as it consumes chunks. If consuming a single
  4178. // written chunk would result in multiple output chunks, then the first
  4179. // outputted bit calls the readcb, and subsequent chunks just go into
  4180. // the read buffer, and will cause it to emit 'readable' if necessary.
  4181. //
  4182. // This way, back-pressure is actually determined by the reading side,
  4183. // since _read has to be called to start processing a new chunk. However,
  4184. // a pathological inflate type of transform can cause excessive buffering
  4185. // here. For example, imagine a stream where every byte of input is
  4186. // interpreted as an integer from 0-255, and then results in that many
  4187. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  4188. // 1kb of data being output. In this case, you could write a very small
  4189. // amount of input, and end up with a very large amount of output. In
  4190. // such a pathological inflating mechanism, there'd be no way to tell
  4191. // the system to stop doing the transform. A single 4MB write could
  4192. // cause the system to run out of memory.
  4193. //
  4194. // However, even in such a pathological case, only a single written chunk
  4195. // would be consumed, and then the rest would wait (un-transformed) until
  4196. // the results of the previous transformed chunk were consumed.
  4197. inherits$1(Transform, Duplex);
  4198. function TransformState(stream) {
  4199. this.afterTransform = function (er, data) {
  4200. return afterTransform(stream, er, data);
  4201. };
  4202. this.needTransform = false;
  4203. this.transforming = false;
  4204. this.writecb = null;
  4205. this.writechunk = null;
  4206. this.writeencoding = null;
  4207. }
  4208. function afterTransform(stream, er, data) {
  4209. var ts = stream._transformState;
  4210. ts.transforming = false;
  4211. var cb = ts.writecb;
  4212. if (!cb) return stream.emit('error', new Error('no writecb in Transform class'));
  4213. ts.writechunk = null;
  4214. ts.writecb = null;
  4215. if (data !== null && data !== undefined) stream.push(data);
  4216. cb(er);
  4217. var rs = stream._readableState;
  4218. rs.reading = false;
  4219. if (rs.needReadable || rs.length < rs.highWaterMark) {
  4220. stream._read(rs.highWaterMark);
  4221. }
  4222. }
  4223. function Transform(options) {
  4224. if (!(this instanceof Transform)) return new Transform(options);
  4225. Duplex.call(this, options);
  4226. this._transformState = new TransformState(this);
  4227. // when the writable side finishes, then flush out anything remaining.
  4228. var stream = this;
  4229. // start out asking for a readable event once data is transformed.
  4230. this._readableState.needReadable = true;
  4231. // we have implemented the _read method, and done the other things
  4232. // that Readable wants before the first _read call, so unset the
  4233. // sync guard flag.
  4234. this._readableState.sync = false;
  4235. if (options) {
  4236. if (typeof options.transform === 'function') this._transform = options.transform;
  4237. if (typeof options.flush === 'function') this._flush = options.flush;
  4238. }
  4239. this.once('prefinish', function () {
  4240. if (typeof this._flush === 'function') this._flush(function (er) {
  4241. done(stream, er);
  4242. });else done(stream);
  4243. });
  4244. }
  4245. Transform.prototype.push = function (chunk, encoding) {
  4246. this._transformState.needTransform = false;
  4247. return Duplex.prototype.push.call(this, chunk, encoding);
  4248. };
  4249. // This is the part where you do stuff!
  4250. // override this function in implementation classes.
  4251. // 'chunk' is an input chunk.
  4252. //
  4253. // Call `push(newChunk)` to pass along transformed output
  4254. // to the readable side. You may call 'push' zero or more times.
  4255. //
  4256. // Call `cb(err)` when you are done with this chunk. If you pass
  4257. // an error, then that'll put the hurt on the whole operation. If you
  4258. // never call cb(), then you'll never get another chunk.
  4259. Transform.prototype._transform = function (chunk, encoding, cb) {
  4260. throw new Error('Not implemented');
  4261. };
  4262. Transform.prototype._write = function (chunk, encoding, cb) {
  4263. var ts = this._transformState;
  4264. ts.writecb = cb;
  4265. ts.writechunk = chunk;
  4266. ts.writeencoding = encoding;
  4267. if (!ts.transforming) {
  4268. var rs = this._readableState;
  4269. if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
  4270. }
  4271. };
  4272. // Doesn't matter what the args are here.
  4273. // _transform does all the work.
  4274. // That we got here means that the readable side wants more data.
  4275. Transform.prototype._read = function (n) {
  4276. var ts = this._transformState;
  4277. if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
  4278. ts.transforming = true;
  4279. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  4280. } else {
  4281. // mark that we need a transform, so that any data that comes in
  4282. // will get processed, now that we've asked for it.
  4283. ts.needTransform = true;
  4284. }
  4285. };
  4286. function done(stream, er) {
  4287. if (er) return stream.emit('error', er);
  4288. // if there's nothing in the write buffer, then that means
  4289. // that nothing more will ever be provided
  4290. var ws = stream._writableState;
  4291. var ts = stream._transformState;
  4292. if (ws.length) throw new Error('Calling transform done when ws.length != 0');
  4293. if (ts.transforming) throw new Error('Calling transform done when still transforming');
  4294. return stream.push(null);
  4295. }
  4296. inherits$1(PassThrough, Transform);
  4297. function PassThrough(options) {
  4298. if (!(this instanceof PassThrough)) return new PassThrough(options);
  4299. Transform.call(this, options);
  4300. }
  4301. PassThrough.prototype._transform = function (chunk, encoding, cb) {
  4302. cb(null, chunk);
  4303. };
  4304. inherits$1(Stream, EventEmitter);
  4305. Stream.Readable = Readable;
  4306. Stream.Writable = Writable;
  4307. Stream.Duplex = Duplex;
  4308. Stream.Transform = Transform;
  4309. Stream.PassThrough = PassThrough;
  4310. // Backwards-compat with node 0.4.x
  4311. Stream.Stream = Stream;
  4312. // old-style streams. Note that the pipe method (the only relevant
  4313. // part of this class) is overridden in the Readable class.
  4314. function Stream() {
  4315. EventEmitter.call(this);
  4316. }
  4317. Stream.prototype.pipe = function(dest, options) {
  4318. var source = this;
  4319. function ondata(chunk) {
  4320. if (dest.writable) {
  4321. if (false === dest.write(chunk) && source.pause) {
  4322. source.pause();
  4323. }
  4324. }
  4325. }
  4326. source.on('data', ondata);
  4327. function ondrain() {
  4328. if (source.readable && source.resume) {
  4329. source.resume();
  4330. }
  4331. }
  4332. dest.on('drain', ondrain);
  4333. // If the 'end' option is not supplied, dest.end() will be called when
  4334. // source gets the 'end' or 'close' events. Only dest.end() once.
  4335. if (!dest._isStdio && (!options || options.end !== false)) {
  4336. source.on('end', onend);
  4337. source.on('close', onclose);
  4338. }
  4339. var didOnEnd = false;
  4340. function onend() {
  4341. if (didOnEnd) return;
  4342. didOnEnd = true;
  4343. dest.end();
  4344. }
  4345. function onclose() {
  4346. if (didOnEnd) return;
  4347. didOnEnd = true;
  4348. if (typeof dest.destroy === 'function') dest.destroy();
  4349. }
  4350. // don't leave dangling pipes when there are errors.
  4351. function onerror(er) {
  4352. cleanup();
  4353. if (EventEmitter.listenerCount(this, 'error') === 0) {
  4354. throw er; // Unhandled stream error in pipe.
  4355. }
  4356. }
  4357. source.on('error', onerror);
  4358. dest.on('error', onerror);
  4359. // remove all the event listeners that were added.
  4360. function cleanup() {
  4361. source.removeListener('data', ondata);
  4362. dest.removeListener('drain', ondrain);
  4363. source.removeListener('end', onend);
  4364. source.removeListener('close', onclose);
  4365. source.removeListener('error', onerror);
  4366. dest.removeListener('error', onerror);
  4367. source.removeListener('end', cleanup);
  4368. source.removeListener('close', cleanup);
  4369. dest.removeListener('close', cleanup);
  4370. }
  4371. source.on('end', cleanup);
  4372. source.on('close', cleanup);
  4373. dest.on('close', cleanup);
  4374. dest.emit('pipe', source);
  4375. // Allow for unix-like usage: A.pipe(B).pipe(C)
  4376. return dest;
  4377. };
  4378. const is_object = function(obj){
  4379. return (typeof obj === 'object' && obj !== null && !Array.isArray(obj));
  4380. };
  4381. class CsvError extends Error {
  4382. constructor(code, message, options, ...contexts) {
  4383. if(Array.isArray(message)) message = message.join(' ').trim();
  4384. super(message);
  4385. if(Error.captureStackTrace !== undefined){
  4386. Error.captureStackTrace(this, CsvError);
  4387. }
  4388. this.code = code;
  4389. for(const context of contexts){
  4390. for(const key in context){
  4391. const value = context[key];
  4392. this[key] = isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value));
  4393. }
  4394. }
  4395. }
  4396. }
  4397. const normalize_columns_array = function(columns){
  4398. const normalizedColumns = [];
  4399. for(let i = 0, l = columns.length; i < l; i++){
  4400. const column = columns[i];
  4401. if(column === undefined || column === null || column === false){
  4402. normalizedColumns[i] = { disabled: true };
  4403. }else if(typeof column === 'string'){
  4404. normalizedColumns[i] = { name: column };
  4405. }else if(is_object(column)){
  4406. if(typeof column.name !== 'string'){
  4407. throw new CsvError('CSV_OPTION_COLUMNS_MISSING_NAME', [
  4408. 'Option columns missing name:',
  4409. `property "name" is required at position ${i}`,
  4410. 'when column is an object literal'
  4411. ]);
  4412. }
  4413. normalizedColumns[i] = column;
  4414. }else {
  4415. throw new CsvError('CSV_INVALID_COLUMN_DEFINITION', [
  4416. 'Invalid column definition:',
  4417. 'expect a string or a literal object,',
  4418. `got ${JSON.stringify(column)} at position ${i}`
  4419. ]);
  4420. }
  4421. }
  4422. return normalizedColumns;
  4423. };
  4424. class ResizeableBuffer{
  4425. constructor(size=100){
  4426. this.size = size;
  4427. this.length = 0;
  4428. this.buf = Buffer.allocUnsafe(size);
  4429. }
  4430. prepend(val){
  4431. if(isBuffer(val)){
  4432. const length = this.length + val.length;
  4433. if(length >= this.size){
  4434. this.resize();
  4435. if(length >= this.size){
  4436. throw Error('INVALID_BUFFER_STATE');
  4437. }
  4438. }
  4439. const buf = this.buf;
  4440. this.buf = Buffer.allocUnsafe(this.size);
  4441. val.copy(this.buf, 0);
  4442. buf.copy(this.buf, val.length);
  4443. this.length += val.length;
  4444. }else {
  4445. const length = this.length++;
  4446. if(length === this.size){
  4447. this.resize();
  4448. }
  4449. const buf = this.clone();
  4450. this.buf[0] = val;
  4451. buf.copy(this.buf,1, 0, length);
  4452. }
  4453. }
  4454. append(val){
  4455. const length = this.length++;
  4456. if(length === this.size){
  4457. this.resize();
  4458. }
  4459. this.buf[length] = val;
  4460. }
  4461. clone(){
  4462. return Buffer.from(this.buf.slice(0, this.length));
  4463. }
  4464. resize(){
  4465. const length = this.length;
  4466. this.size = this.size * 2;
  4467. const buf = Buffer.allocUnsafe(this.size);
  4468. this.buf.copy(buf,0, 0, length);
  4469. this.buf = buf;
  4470. }
  4471. toString(encoding){
  4472. if(encoding){
  4473. return this.buf.slice(0, this.length).toString(encoding);
  4474. }else {
  4475. return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length));
  4476. }
  4477. }
  4478. toJSON(){
  4479. return this.toString('utf8');
  4480. }
  4481. reset(){
  4482. this.length = 0;
  4483. }
  4484. }
  4485. // white space characters
  4486. // https://en.wikipedia.org/wiki/Whitespace_character
  4487. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#Types
  4488. // \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff
  4489. const np = 12;
  4490. const cr$1 = 13; // `\r`, carriage return, 0x0D in hexadécimal, 13 in decimal
  4491. const nl$1 = 10; // `\n`, newline, 0x0A in hexadecimal, 10 in decimal
  4492. const space = 32;
  4493. const tab = 9;
  4494. const init_state = function(options){
  4495. return {
  4496. bomSkipped: false,
  4497. bufBytesStart: 0,
  4498. castField: options.cast_function,
  4499. commenting: false,
  4500. // Current error encountered by a record
  4501. error: undefined,
  4502. enabled: options.from_line === 1,
  4503. escaping: false,
  4504. escapeIsQuote: isBuffer(options.escape) && isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0,
  4505. // columns can be `false`, `true`, `Array`
  4506. expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
  4507. field: new ResizeableBuffer(20),
  4508. firstLineToHeaders: options.cast_first_line_to_header,
  4509. needMoreDataSize: Math.max(
  4510. // Skip if the remaining buffer smaller than comment
  4511. options.comment !== null ? options.comment.length : 0,
  4512. // Skip if the remaining buffer can be delimiter
  4513. ...options.delimiter.map((delimiter) => delimiter.length),
  4514. // Skip if the remaining buffer can be escape sequence
  4515. options.quote !== null ? options.quote.length : 0,
  4516. ),
  4517. previousBuf: undefined,
  4518. quoting: false,
  4519. stop: false,
  4520. rawBuffer: new ResizeableBuffer(100),
  4521. record: [],
  4522. recordHasError: false,
  4523. record_length: 0,
  4524. recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 0 : Math.max(...options.record_delimiter.map((v) => v.length)),
  4525. trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]],
  4526. wasQuoting: false,
  4527. wasRowDelimiter: false,
  4528. timchars: [
  4529. Buffer.from(Buffer.from([cr$1], 'utf8').toString(), options.encoding),
  4530. Buffer.from(Buffer.from([nl$1], 'utf8').toString(), options.encoding),
  4531. Buffer.from(Buffer.from([np], 'utf8').toString(), options.encoding),
  4532. Buffer.from(Buffer.from([space], 'utf8').toString(), options.encoding),
  4533. Buffer.from(Buffer.from([tab], 'utf8').toString(), options.encoding),
  4534. ]
  4535. };
  4536. };
  4537. const underscore = function(str){
  4538. return str.replace(/([A-Z])/g, function(_, match){
  4539. return '_' + match.toLowerCase();
  4540. });
  4541. };
  4542. const normalize_options = function(opts){
  4543. const options = {};
  4544. // Merge with user options
  4545. for(const opt in opts){
  4546. options[underscore(opt)] = opts[opt];
  4547. }
  4548. // Normalize option `encoding`
  4549. // Note: defined first because other options depends on it
  4550. // to convert chars/strings into buffers.
  4551. if(options.encoding === undefined || options.encoding === true){
  4552. options.encoding = 'utf8';
  4553. }else if(options.encoding === null || options.encoding === false){
  4554. options.encoding = null;
  4555. }else if(typeof options.encoding !== 'string' && options.encoding !== null){
  4556. throw new CsvError('CSV_INVALID_OPTION_ENCODING', [
  4557. 'Invalid option encoding:',
  4558. 'encoding must be a string or null to return a buffer,',
  4559. `got ${JSON.stringify(options.encoding)}`
  4560. ], options);
  4561. }
  4562. // Normalize option `bom`
  4563. if(options.bom === undefined || options.bom === null || options.bom === false){
  4564. options.bom = false;
  4565. }else if(options.bom !== true){
  4566. throw new CsvError('CSV_INVALID_OPTION_BOM', [
  4567. 'Invalid option bom:', 'bom must be true,',
  4568. `got ${JSON.stringify(options.bom)}`
  4569. ], options);
  4570. }
  4571. // Normalize option `cast`
  4572. options.cast_function = null;
  4573. if(options.cast === undefined || options.cast === null || options.cast === false || options.cast === ''){
  4574. options.cast = undefined;
  4575. }else if(typeof options.cast === 'function'){
  4576. options.cast_function = options.cast;
  4577. options.cast = true;
  4578. }else if(options.cast !== true){
  4579. throw new CsvError('CSV_INVALID_OPTION_CAST', [
  4580. 'Invalid option cast:', 'cast must be true or a function,',
  4581. `got ${JSON.stringify(options.cast)}`
  4582. ], options);
  4583. }
  4584. // Normalize option `cast_date`
  4585. if(options.cast_date === undefined || options.cast_date === null || options.cast_date === false || options.cast_date === ''){
  4586. options.cast_date = false;
  4587. }else if(options.cast_date === true){
  4588. options.cast_date = function(value){
  4589. const date = Date.parse(value);
  4590. return !isNaN(date) ? new Date(date) : value;
  4591. };
  4592. }else if (typeof options.cast_date !== 'function'){
  4593. throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
  4594. 'Invalid option cast_date:', 'cast_date must be true or a function,',
  4595. `got ${JSON.stringify(options.cast_date)}`
  4596. ], options);
  4597. }
  4598. // Normalize option `columns`
  4599. options.cast_first_line_to_header = null;
  4600. if(options.columns === true){
  4601. // Fields in the first line are converted as-is to columns
  4602. options.cast_first_line_to_header = undefined;
  4603. }else if(typeof options.columns === 'function'){
  4604. options.cast_first_line_to_header = options.columns;
  4605. options.columns = true;
  4606. }else if(Array.isArray(options.columns)){
  4607. options.columns = normalize_columns_array(options.columns);
  4608. }else if(options.columns === undefined || options.columns === null || options.columns === false){
  4609. options.columns = false;
  4610. }else {
  4611. throw new CsvError('CSV_INVALID_OPTION_COLUMNS', [
  4612. 'Invalid option columns:',
  4613. 'expect an array, a function or true,',
  4614. `got ${JSON.stringify(options.columns)}`
  4615. ], options);
  4616. }
  4617. // Normalize option `group_columns_by_name`
  4618. if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){
  4619. options.group_columns_by_name = false;
  4620. }else if(options.group_columns_by_name !== true){
  4621. throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
  4622. 'Invalid option group_columns_by_name:',
  4623. 'expect an boolean,',
  4624. `got ${JSON.stringify(options.group_columns_by_name)}`
  4625. ], options);
  4626. }else if(options.columns === false){
  4627. throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
  4628. 'Invalid option group_columns_by_name:',
  4629. 'the `columns` mode must be activated.'
  4630. ], options);
  4631. }
  4632. // Normalize option `comment`
  4633. if(options.comment === undefined || options.comment === null || options.comment === false || options.comment === ''){
  4634. options.comment = null;
  4635. }else {
  4636. if(typeof options.comment === 'string'){
  4637. options.comment = Buffer.from(options.comment, options.encoding);
  4638. }
  4639. if(!isBuffer(options.comment)){
  4640. throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
  4641. 'Invalid option comment:',
  4642. 'comment must be a buffer or a string,',
  4643. `got ${JSON.stringify(options.comment)}`
  4644. ], options);
  4645. }
  4646. }
  4647. // Normalize option `comment_no_infix`
  4648. if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
  4649. options.comment_no_infix = false;
  4650. }else if(options.comment_no_infix !== true){
  4651. throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
  4652. 'Invalid option comment_no_infix:',
  4653. 'value must be a boolean,',
  4654. `got ${JSON.stringify(options.comment_no_infix)}`
  4655. ], options);
  4656. }
  4657. // Normalize option `delimiter`
  4658. const delimiter_json = JSON.stringify(options.delimiter);
  4659. if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
  4660. if(options.delimiter.length === 0){
  4661. throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
  4662. 'Invalid option delimiter:',
  4663. 'delimiter must be a non empty string or buffer or array of string|buffer,',
  4664. `got ${delimiter_json}`
  4665. ], options);
  4666. }
  4667. options.delimiter = options.delimiter.map(function(delimiter){
  4668. if(delimiter === undefined || delimiter === null || delimiter === false){
  4669. return Buffer.from(',', options.encoding);
  4670. }
  4671. if(typeof delimiter === 'string'){
  4672. delimiter = Buffer.from(delimiter, options.encoding);
  4673. }
  4674. if(!isBuffer(delimiter) || delimiter.length === 0){
  4675. throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
  4676. 'Invalid option delimiter:',
  4677. 'delimiter must be a non empty string or buffer or array of string|buffer,',
  4678. `got ${delimiter_json}`
  4679. ], options);
  4680. }
  4681. return delimiter;
  4682. });
  4683. // Normalize option `escape`
  4684. if(options.escape === undefined || options.escape === true){
  4685. options.escape = Buffer.from('"', options.encoding);
  4686. }else if(typeof options.escape === 'string'){
  4687. options.escape = Buffer.from(options.escape, options.encoding);
  4688. }else if (options.escape === null || options.escape === false){
  4689. options.escape = null;
  4690. }
  4691. if(options.escape !== null){
  4692. if(!isBuffer(options.escape)){
  4693. throw new Error(`Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`);
  4694. }
  4695. }
  4696. // Normalize option `from`
  4697. if(options.from === undefined || options.from === null){
  4698. options.from = 1;
  4699. }else {
  4700. if(typeof options.from === 'string' && /\d+/.test(options.from)){
  4701. options.from = parseInt(options.from);
  4702. }
  4703. if(Number.isInteger(options.from)){
  4704. if(options.from < 0){
  4705. throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`);
  4706. }
  4707. }else {
  4708. throw new Error(`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`);
  4709. }
  4710. }
  4711. // Normalize option `from_line`
  4712. if(options.from_line === undefined || options.from_line === null){
  4713. options.from_line = 1;
  4714. }else {
  4715. if(typeof options.from_line === 'string' && /\d+/.test(options.from_line)){
  4716. options.from_line = parseInt(options.from_line);
  4717. }
  4718. if(Number.isInteger(options.from_line)){
  4719. if(options.from_line <= 0){
  4720. throw new Error(`Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`);
  4721. }
  4722. }else {
  4723. throw new Error(`Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`);
  4724. }
  4725. }
  4726. // Normalize options `ignore_last_delimiters`
  4727. if(options.ignore_last_delimiters === undefined || options.ignore_last_delimiters === null){
  4728. options.ignore_last_delimiters = false;
  4729. }else if(typeof options.ignore_last_delimiters === 'number'){
  4730. options.ignore_last_delimiters = Math.floor(options.ignore_last_delimiters);
  4731. if(options.ignore_last_delimiters === 0){
  4732. options.ignore_last_delimiters = false;
  4733. }
  4734. }else if(typeof options.ignore_last_delimiters !== 'boolean'){
  4735. throw new CsvError('CSV_INVALID_OPTION_IGNORE_LAST_DELIMITERS', [
  4736. 'Invalid option `ignore_last_delimiters`:',
  4737. 'the value must be a boolean value or an integer,',
  4738. `got ${JSON.stringify(options.ignore_last_delimiters)}`
  4739. ], options);
  4740. }
  4741. if(options.ignore_last_delimiters === true && options.columns === false){
  4742. throw new CsvError('CSV_IGNORE_LAST_DELIMITERS_REQUIRES_COLUMNS', [
  4743. 'The option `ignore_last_delimiters`',
  4744. 'requires the activation of the `columns` option'
  4745. ], options);
  4746. }
  4747. // Normalize option `info`
  4748. if(options.info === undefined || options.info === null || options.info === false){
  4749. options.info = false;
  4750. }else if(options.info !== true){
  4751. throw new Error(`Invalid Option: info must be true, got ${JSON.stringify(options.info)}`);
  4752. }
  4753. // Normalize option `max_record_size`
  4754. if(options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false){
  4755. options.max_record_size = 0;
  4756. }else if(Number.isInteger(options.max_record_size) && options.max_record_size >= 0);else if(typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)){
  4757. options.max_record_size = parseInt(options.max_record_size);
  4758. }else {
  4759. throw new Error(`Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`);
  4760. }
  4761. // Normalize option `objname`
  4762. if(options.objname === undefined || options.objname === null || options.objname === false){
  4763. options.objname = undefined;
  4764. }else if(isBuffer(options.objname)){
  4765. if(options.objname.length === 0){
  4766. throw new Error(`Invalid Option: objname must be a non empty buffer`);
  4767. }
  4768. if(options.encoding === null);else {
  4769. options.objname = options.objname.toString(options.encoding);
  4770. }
  4771. }else if(typeof options.objname === 'string'){
  4772. if(options.objname.length === 0){
  4773. throw new Error(`Invalid Option: objname must be a non empty string`);
  4774. }
  4775. // Great, nothing to do
  4776. }else if(typeof options.objname === 'number');else {
  4777. throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`);
  4778. }
  4779. if(options.objname !== undefined){
  4780. if(typeof options.objname === 'number'){
  4781. if(options.columns !== false){
  4782. throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field');
  4783. }
  4784. }else { // A string or a buffer
  4785. if(options.columns === false){
  4786. throw Error('Invalid Option: objname field must be combined with columns or be defined as an index');
  4787. }
  4788. }
  4789. }
  4790. // Normalize option `on_record`
  4791. if(options.on_record === undefined || options.on_record === null){
  4792. options.on_record = undefined;
  4793. }else if(typeof options.on_record !== 'function'){
  4794. throw new CsvError('CSV_INVALID_OPTION_ON_RECORD', [
  4795. 'Invalid option `on_record`:',
  4796. 'expect a function,',
  4797. `got ${JSON.stringify(options.on_record)}`
  4798. ], options);
  4799. }
  4800. // Normalize option `quote`
  4801. if(options.quote === null || options.quote === false || options.quote === ''){
  4802. options.quote = null;
  4803. }else {
  4804. if(options.quote === undefined || options.quote === true){
  4805. options.quote = Buffer.from('"', options.encoding);
  4806. }else if(typeof options.quote === 'string'){
  4807. options.quote = Buffer.from(options.quote, options.encoding);
  4808. }
  4809. if(!isBuffer(options.quote)){
  4810. throw new Error(`Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`);
  4811. }
  4812. }
  4813. // Normalize option `raw`
  4814. if(options.raw === undefined || options.raw === null || options.raw === false){
  4815. options.raw = false;
  4816. }else if(options.raw !== true){
  4817. throw new Error(`Invalid Option: raw must be true, got ${JSON.stringify(options.raw)}`);
  4818. }
  4819. // Normalize option `record_delimiter`
  4820. if(options.record_delimiter === undefined){
  4821. options.record_delimiter = [];
  4822. }else if(typeof options.record_delimiter === 'string' || isBuffer(options.record_delimiter)){
  4823. if(options.record_delimiter.length === 0){
  4824. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  4825. 'Invalid option `record_delimiter`:',
  4826. 'value must be a non empty string or buffer,',
  4827. `got ${JSON.stringify(options.record_delimiter)}`
  4828. ], options);
  4829. }
  4830. options.record_delimiter = [options.record_delimiter];
  4831. }else if(!Array.isArray(options.record_delimiter)){
  4832. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  4833. 'Invalid option `record_delimiter`:',
  4834. 'value must be a string, a buffer or array of string|buffer,',
  4835. `got ${JSON.stringify(options.record_delimiter)}`
  4836. ], options);
  4837. }
  4838. options.record_delimiter = options.record_delimiter.map(function(rd, i){
  4839. if(typeof rd !== 'string' && ! isBuffer(rd)){
  4840. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  4841. 'Invalid option `record_delimiter`:',
  4842. 'value must be a string, a buffer or array of string|buffer',
  4843. `at index ${i},`,
  4844. `got ${JSON.stringify(rd)}`
  4845. ], options);
  4846. }else if(rd.length === 0){
  4847. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  4848. 'Invalid option `record_delimiter`:',
  4849. 'value must be a non empty string or buffer',
  4850. `at index ${i},`,
  4851. `got ${JSON.stringify(rd)}`
  4852. ], options);
  4853. }
  4854. if(typeof rd === 'string'){
  4855. rd = Buffer.from(rd, options.encoding);
  4856. }
  4857. return rd;
  4858. });
  4859. // Normalize option `relax_column_count`
  4860. if(typeof options.relax_column_count === 'boolean');else if(options.relax_column_count === undefined || options.relax_column_count === null){
  4861. options.relax_column_count = false;
  4862. }else {
  4863. throw new Error(`Invalid Option: relax_column_count must be a boolean, got ${JSON.stringify(options.relax_column_count)}`);
  4864. }
  4865. if(typeof options.relax_column_count_less === 'boolean');else if(options.relax_column_count_less === undefined || options.relax_column_count_less === null){
  4866. options.relax_column_count_less = false;
  4867. }else {
  4868. throw new Error(`Invalid Option: relax_column_count_less must be a boolean, got ${JSON.stringify(options.relax_column_count_less)}`);
  4869. }
  4870. if(typeof options.relax_column_count_more === 'boolean');else if(options.relax_column_count_more === undefined || options.relax_column_count_more === null){
  4871. options.relax_column_count_more = false;
  4872. }else {
  4873. throw new Error(`Invalid Option: relax_column_count_more must be a boolean, got ${JSON.stringify(options.relax_column_count_more)}`);
  4874. }
  4875. // Normalize option `relax_quotes`
  4876. if(typeof options.relax_quotes === 'boolean');else if(options.relax_quotes === undefined || options.relax_quotes === null){
  4877. options.relax_quotes = false;
  4878. }else {
  4879. throw new Error(`Invalid Option: relax_quotes must be a boolean, got ${JSON.stringify(options.relax_quotes)}`);
  4880. }
  4881. // Normalize option `skip_empty_lines`
  4882. if(typeof options.skip_empty_lines === 'boolean');else if(options.skip_empty_lines === undefined || options.skip_empty_lines === null){
  4883. options.skip_empty_lines = false;
  4884. }else {
  4885. throw new Error(`Invalid Option: skip_empty_lines must be a boolean, got ${JSON.stringify(options.skip_empty_lines)}`);
  4886. }
  4887. // Normalize option `skip_records_with_empty_values`
  4888. if(typeof options.skip_records_with_empty_values === 'boolean');else if(options.skip_records_with_empty_values === undefined || options.skip_records_with_empty_values === null){
  4889. options.skip_records_with_empty_values = false;
  4890. }else {
  4891. throw new Error(`Invalid Option: skip_records_with_empty_values must be a boolean, got ${JSON.stringify(options.skip_records_with_empty_values)}`);
  4892. }
  4893. // Normalize option `skip_records_with_error`
  4894. if(typeof options.skip_records_with_error === 'boolean');else if(options.skip_records_with_error === undefined || options.skip_records_with_error === null){
  4895. options.skip_records_with_error = false;
  4896. }else {
  4897. throw new Error(`Invalid Option: skip_records_with_error must be a boolean, got ${JSON.stringify(options.skip_records_with_error)}`);
  4898. }
  4899. // Normalize option `rtrim`
  4900. if(options.rtrim === undefined || options.rtrim === null || options.rtrim === false){
  4901. options.rtrim = false;
  4902. }else if(options.rtrim !== true){
  4903. throw new Error(`Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`);
  4904. }
  4905. // Normalize option `ltrim`
  4906. if(options.ltrim === undefined || options.ltrim === null || options.ltrim === false){
  4907. options.ltrim = false;
  4908. }else if(options.ltrim !== true){
  4909. throw new Error(`Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`);
  4910. }
  4911. // Normalize option `trim`
  4912. if(options.trim === undefined || options.trim === null || options.trim === false){
  4913. options.trim = false;
  4914. }else if(options.trim !== true){
  4915. throw new Error(`Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`);
  4916. }
  4917. // Normalize options `trim`, `ltrim` and `rtrim`
  4918. if(options.trim === true && opts.ltrim !== false){
  4919. options.ltrim = true;
  4920. }else if(options.ltrim !== true){
  4921. options.ltrim = false;
  4922. }
  4923. if(options.trim === true && opts.rtrim !== false){
  4924. options.rtrim = true;
  4925. }else if(options.rtrim !== true){
  4926. options.rtrim = false;
  4927. }
  4928. // Normalize option `to`
  4929. if(options.to === undefined || options.to === null){
  4930. options.to = -1;
  4931. }else {
  4932. if(typeof options.to === 'string' && /\d+/.test(options.to)){
  4933. options.to = parseInt(options.to);
  4934. }
  4935. if(Number.isInteger(options.to)){
  4936. if(options.to <= 0){
  4937. throw new Error(`Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`);
  4938. }
  4939. }else {
  4940. throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`);
  4941. }
  4942. }
  4943. // Normalize option `to_line`
  4944. if(options.to_line === undefined || options.to_line === null){
  4945. options.to_line = -1;
  4946. }else {
  4947. if(typeof options.to_line === 'string' && /\d+/.test(options.to_line)){
  4948. options.to_line = parseInt(options.to_line);
  4949. }
  4950. if(Number.isInteger(options.to_line)){
  4951. if(options.to_line <= 0){
  4952. throw new Error(`Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`);
  4953. }
  4954. }else {
  4955. throw new Error(`Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`);
  4956. }
  4957. }
  4958. return options;
  4959. };
  4960. const isRecordEmpty = function(record){
  4961. return record.every((field) => field == null || field.toString && field.toString().trim() === '');
  4962. };
  4963. const cr = 13; // `\r`, carriage return, 0x0D in hexadécimal, 13 in decimal
  4964. const nl = 10; // `\n`, newline, 0x0A in hexadecimal, 10 in decimal
  4965. const boms = {
  4966. // Note, the following are equals:
  4967. // Buffer.from("\ufeff")
  4968. // Buffer.from([239, 187, 191])
  4969. // Buffer.from('EFBBBF', 'hex')
  4970. 'utf8': Buffer.from([239, 187, 191]),
  4971. // Note, the following are equals:
  4972. // Buffer.from "\ufeff", 'utf16le
  4973. // Buffer.from([255, 254])
  4974. 'utf16le': Buffer.from([255, 254])
  4975. };
  4976. const transform = function(original_options = {}) {
  4977. const info = {
  4978. bytes: 0,
  4979. comment_lines: 0,
  4980. empty_lines: 0,
  4981. invalid_field_length: 0,
  4982. lines: 1,
  4983. records: 0
  4984. };
  4985. const options = normalize_options(original_options);
  4986. return {
  4987. info: info,
  4988. original_options: original_options,
  4989. options: options,
  4990. state: init_state(options),
  4991. __needMoreData: function(i, bufLen, end){
  4992. if(end) return false;
  4993. const {encoding, escape, quote} = this.options;
  4994. const {quoting, needMoreDataSize, recordDelimiterMaxLength} = this.state;
  4995. const numOfCharLeft = bufLen - i - 1;
  4996. const requiredLength = Math.max(
  4997. needMoreDataSize,
  4998. // Skip if the remaining buffer smaller than record delimiter
  4999. // If "record_delimiter" is yet to be discovered:
  5000. // 1. It is equals to `[]` and "recordDelimiterMaxLength" equals `0`
  5001. // 2. We set the length to windows line ending in the current encoding
  5002. // Note, that encoding is known from user or bom discovery at that point
  5003. // recordDelimiterMaxLength,
  5004. recordDelimiterMaxLength === 0 ? Buffer.from('\r\n', encoding).length : recordDelimiterMaxLength,
  5005. // Skip if remaining buffer can be an escaped quote
  5006. quoting ? ((escape === null ? 0 : escape.length) + quote.length) : 0,
  5007. // Skip if remaining buffer can be record delimiter following the closing quote
  5008. quoting ? (quote.length + recordDelimiterMaxLength) : 0,
  5009. );
  5010. return numOfCharLeft < requiredLength;
  5011. },
  5012. // Central parser implementation
  5013. parse: function(nextBuf, end, push, close){
  5014. const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
  5015. let {comment, escape, quote, record_delimiter} = this.options;
  5016. const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
  5017. let buf;
  5018. if(previousBuf === undefined){
  5019. if(nextBuf === undefined){
  5020. // Handle empty string
  5021. close();
  5022. return;
  5023. }else {
  5024. buf = nextBuf;
  5025. }
  5026. }else if(previousBuf !== undefined && nextBuf === undefined){
  5027. buf = previousBuf;
  5028. }else {
  5029. buf = Buffer.concat([previousBuf, nextBuf]);
  5030. }
  5031. // Handle UTF BOM
  5032. if(bomSkipped === false){
  5033. if(bom === false){
  5034. this.state.bomSkipped = true;
  5035. }else if(buf.length < 3){
  5036. // No enough data
  5037. if(end === false){
  5038. // Wait for more data
  5039. this.state.previousBuf = buf;
  5040. return;
  5041. }
  5042. }else {
  5043. for(const encoding in boms){
  5044. if(boms[encoding].compare(buf, 0, boms[encoding].length) === 0){
  5045. // Skip BOM
  5046. const bomLength = boms[encoding].length;
  5047. this.state.bufBytesStart += bomLength;
  5048. buf = buf.slice(bomLength);
  5049. // Renormalize original options with the new encoding
  5050. this.options = normalize_options({...this.original_options, encoding: encoding});
  5051. // Options will re-evaluate the Buffer with the new encoding
  5052. ({comment, escape, quote } = this.options);
  5053. break;
  5054. }
  5055. }
  5056. this.state.bomSkipped = true;
  5057. }
  5058. }
  5059. const bufLen = buf.length;
  5060. let pos;
  5061. for(pos = 0; pos < bufLen; pos++){
  5062. // Ensure we get enough space to look ahead
  5063. // There should be a way to move this out of the loop
  5064. if(this.__needMoreData(pos, bufLen, end)){
  5065. break;
  5066. }
  5067. if(this.state.wasRowDelimiter === true){
  5068. this.info.lines++;
  5069. this.state.wasRowDelimiter = false;
  5070. }
  5071. if(to_line !== -1 && this.info.lines > to_line){
  5072. this.state.stop = true;
  5073. close();
  5074. return;
  5075. }
  5076. // Auto discovery of record_delimiter, unix, mac and windows supported
  5077. if(this.state.quoting === false && record_delimiter.length === 0){
  5078. const record_delimiterCount = this.__autoDiscoverRecordDelimiter(buf, pos);
  5079. if(record_delimiterCount){
  5080. record_delimiter = this.options.record_delimiter;
  5081. }
  5082. }
  5083. const chr = buf[pos];
  5084. if(raw === true){
  5085. rawBuffer.append(chr);
  5086. }
  5087. if((chr === cr || chr === nl) && this.state.wasRowDelimiter === false){
  5088. this.state.wasRowDelimiter = true;
  5089. }
  5090. // Previous char was a valid escape char
  5091. // treat the current char as a regular char
  5092. if(this.state.escaping === true){
  5093. this.state.escaping = false;
  5094. }else {
  5095. // Escape is only active inside quoted fields
  5096. // We are quoting, the char is an escape chr and there is a chr to escape
  5097. // if(escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen){
  5098. if(escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen){
  5099. if(escapeIsQuote){
  5100. if(this.__isQuote(buf, pos+escape.length)){
  5101. this.state.escaping = true;
  5102. pos += escape.length - 1;
  5103. continue;
  5104. }
  5105. }else {
  5106. this.state.escaping = true;
  5107. pos += escape.length - 1;
  5108. continue;
  5109. }
  5110. }
  5111. // Not currently escaping and chr is a quote
  5112. // TODO: need to compare bytes instead of single char
  5113. if(this.state.commenting === false && this.__isQuote(buf, pos)){
  5114. if(this.state.quoting === true){
  5115. const nextChr = buf[pos+quote.length];
  5116. const isNextChrTrimable = rtrim && this.__isCharTrimable(buf, pos+quote.length);
  5117. const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos+quote.length, nextChr);
  5118. const isNextChrDelimiter = this.__isDelimiter(buf, pos+quote.length, nextChr);
  5119. const isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos+quote.length) : this.__isRecordDelimiter(nextChr, buf, pos+quote.length);
  5120. // Escape a quote
  5121. // Treat next char as a regular character
  5122. if(escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)){
  5123. pos += escape.length - 1;
  5124. }else if(!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable){
  5125. this.state.quoting = false;
  5126. this.state.wasQuoting = true;
  5127. pos += quote.length - 1;
  5128. continue;
  5129. }else if(relax_quotes === false){
  5130. const err = this.__error(
  5131. new CsvError('CSV_INVALID_CLOSING_QUOTE', [
  5132. 'Invalid Closing Quote:',
  5133. `got "${String.fromCharCode(nextChr)}"`,
  5134. `at line ${this.info.lines}`,
  5135. 'instead of delimiter, record delimiter, trimable character',
  5136. '(if activated) or comment',
  5137. ], this.options, this.__infoField())
  5138. );
  5139. if(err !== undefined) return err;
  5140. }else {
  5141. this.state.quoting = false;
  5142. this.state.wasQuoting = true;
  5143. this.state.field.prepend(quote);
  5144. pos += quote.length - 1;
  5145. }
  5146. }else {
  5147. if(this.state.field.length !== 0){
  5148. // In relax_quotes mode, treat opening quote preceded by chrs as regular
  5149. if(relax_quotes === false){
  5150. const info = this.__infoField();
  5151. const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
  5152. const err = this.__error(
  5153. new CsvError('INVALID_OPENING_QUOTE', [
  5154. 'Invalid Opening Quote:',
  5155. `a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
  5156. bom ? `(${bom} bom)` : undefined
  5157. ], this.options, info, {
  5158. field: this.state.field,
  5159. })
  5160. );
  5161. if(err !== undefined) return err;
  5162. }
  5163. }else {
  5164. this.state.quoting = true;
  5165. pos += quote.length - 1;
  5166. continue;
  5167. }
  5168. }
  5169. }
  5170. if(this.state.quoting === false){
  5171. const recordDelimiterLength = this.__isRecordDelimiter(chr, buf, pos);
  5172. if(recordDelimiterLength !== 0){
  5173. // Do not emit comments which take a full line
  5174. const skipCommentLine = this.state.commenting && (this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0);
  5175. if(skipCommentLine){
  5176. this.info.comment_lines++;
  5177. // Skip full comment line
  5178. }else {
  5179. // Activate records emition if above from_line
  5180. if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0) >= from_line){
  5181. this.state.enabled = true;
  5182. this.__resetField();
  5183. this.__resetRecord();
  5184. pos += recordDelimiterLength - 1;
  5185. continue;
  5186. }
  5187. // Skip if line is empty and skip_empty_lines activated
  5188. if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
  5189. this.info.empty_lines++;
  5190. pos += recordDelimiterLength - 1;
  5191. continue;
  5192. }
  5193. this.info.bytes = this.state.bufBytesStart + pos;
  5194. const errField = this.__onField();
  5195. if(errField !== undefined) return errField;
  5196. this.info.bytes = this.state.bufBytesStart + pos + recordDelimiterLength;
  5197. const errRecord = this.__onRecord(push);
  5198. if(errRecord !== undefined) return errRecord;
  5199. if(to !== -1 && this.info.records >= to){
  5200. this.state.stop = true;
  5201. close();
  5202. return;
  5203. }
  5204. }
  5205. this.state.commenting = false;
  5206. pos += recordDelimiterLength - 1;
  5207. continue;
  5208. }
  5209. if(this.state.commenting){
  5210. continue;
  5211. }
  5212. const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
  5213. if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
  5214. this.state.commenting = true;
  5215. continue;
  5216. }
  5217. const delimiterLength = this.__isDelimiter(buf, pos, chr);
  5218. if(delimiterLength !== 0){
  5219. this.info.bytes = this.state.bufBytesStart + pos;
  5220. const errField = this.__onField();
  5221. if(errField !== undefined) return errField;
  5222. pos += delimiterLength - 1;
  5223. continue;
  5224. }
  5225. }
  5226. }
  5227. if(this.state.commenting === false){
  5228. if(max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size){
  5229. return this.__error(
  5230. new CsvError('CSV_MAX_RECORD_SIZE', [
  5231. 'Max Record Size:',
  5232. 'record exceed the maximum number of tolerated bytes',
  5233. `of ${max_record_size}`,
  5234. `at line ${this.info.lines}`,
  5235. ], this.options, this.__infoField())
  5236. );
  5237. }
  5238. }
  5239. const lappend = ltrim === false || this.state.quoting === true || this.state.field.length !== 0 || !this.__isCharTrimable(buf, pos);
  5240. // rtrim in non quoting is handle in __onField
  5241. const rappend = rtrim === false || this.state.wasQuoting === false;
  5242. if(lappend === true && rappend === true){
  5243. this.state.field.append(chr);
  5244. }else if(rtrim === true && !this.__isCharTrimable(buf, pos)){
  5245. return this.__error(
  5246. new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', [
  5247. 'Invalid Closing Quote:',
  5248. 'found non trimable byte after quote',
  5249. `at line ${this.info.lines}`,
  5250. ], this.options, this.__infoField())
  5251. );
  5252. }else {
  5253. if(lappend === false){
  5254. pos += this.__isCharTrimable(buf, pos) - 1;
  5255. }
  5256. continue;
  5257. }
  5258. }
  5259. if(end === true){
  5260. // Ensure we are not ending in a quoting state
  5261. if(this.state.quoting === true){
  5262. const err = this.__error(
  5263. new CsvError('CSV_QUOTE_NOT_CLOSED', [
  5264. 'Quote Not Closed:',
  5265. `the parsing is finished with an opening quote at line ${this.info.lines}`,
  5266. ], this.options, this.__infoField())
  5267. );
  5268. if(err !== undefined) return err;
  5269. }else {
  5270. // Skip last line if it has no characters
  5271. if(this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0){
  5272. this.info.bytes = this.state.bufBytesStart + pos;
  5273. const errField = this.__onField();
  5274. if(errField !== undefined) return errField;
  5275. const errRecord = this.__onRecord(push);
  5276. if(errRecord !== undefined) return errRecord;
  5277. }else if(this.state.wasRowDelimiter === true){
  5278. this.info.empty_lines++;
  5279. }else if(this.state.commenting === true){
  5280. this.info.comment_lines++;
  5281. }
  5282. }
  5283. }else {
  5284. this.state.bufBytesStart += pos;
  5285. this.state.previousBuf = buf.slice(pos);
  5286. }
  5287. if(this.state.wasRowDelimiter === true){
  5288. this.info.lines++;
  5289. this.state.wasRowDelimiter = false;
  5290. }
  5291. },
  5292. __onRecord: function(push){
  5293. const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options;
  5294. const {enabled, record} = this.state;
  5295. if(enabled === false){
  5296. return this.__resetRecord();
  5297. }
  5298. // Convert the first line into column names
  5299. const recordLength = record.length;
  5300. if(columns === true){
  5301. if(skip_records_with_empty_values === true && isRecordEmpty(record)){
  5302. this.__resetRecord();
  5303. return;
  5304. }
  5305. return this.__firstLineToColumns(record);
  5306. }
  5307. if(columns === false && this.info.records === 0){
  5308. this.state.expectedRecordLength = recordLength;
  5309. }
  5310. if(recordLength !== this.state.expectedRecordLength){
  5311. const err = columns === false ?
  5312. new CsvError('CSV_RECORD_INCONSISTENT_FIELDS_LENGTH', [
  5313. 'Invalid Record Length:',
  5314. `expect ${this.state.expectedRecordLength},`,
  5315. `got ${recordLength} on line ${this.info.lines}`,
  5316. ], this.options, this.__infoField(), {
  5317. record: record,
  5318. })
  5319. :
  5320. new CsvError('CSV_RECORD_INCONSISTENT_COLUMNS', [
  5321. 'Invalid Record Length:',
  5322. `columns length is ${columns.length},`, // rename columns
  5323. `got ${recordLength} on line ${this.info.lines}`,
  5324. ], this.options, this.__infoField(), {
  5325. record: record,
  5326. });
  5327. if(relax_column_count === true ||
  5328. (relax_column_count_less === true && recordLength < this.state.expectedRecordLength) ||
  5329. (relax_column_count_more === true && recordLength > this.state.expectedRecordLength)){
  5330. this.info.invalid_field_length++;
  5331. this.state.error = err;
  5332. // Error is undefined with skip_records_with_error
  5333. }else {
  5334. const finalErr = this.__error(err);
  5335. if(finalErr) return finalErr;
  5336. }
  5337. }
  5338. if(skip_records_with_empty_values === true && isRecordEmpty(record)){
  5339. this.__resetRecord();
  5340. return;
  5341. }
  5342. if(this.state.recordHasError === true){
  5343. this.__resetRecord();
  5344. this.state.recordHasError = false;
  5345. return;
  5346. }
  5347. this.info.records++;
  5348. if(from === 1 || this.info.records >= from){
  5349. const {objname} = this.options;
  5350. // With columns, records are object
  5351. if(columns !== false){
  5352. const obj = {};
  5353. // Transform record array to an object
  5354. for(let i = 0, l = record.length; i < l; i++){
  5355. if(columns[i] === undefined || columns[i].disabled) continue;
  5356. // Turn duplicate columns into an array
  5357. if (group_columns_by_name === true && obj[columns[i].name] !== undefined) {
  5358. if (Array.isArray(obj[columns[i].name])) {
  5359. obj[columns[i].name] = obj[columns[i].name].concat(record[i]);
  5360. } else {
  5361. obj[columns[i].name] = [obj[columns[i].name], record[i]];
  5362. }
  5363. } else {
  5364. obj[columns[i].name] = record[i];
  5365. }
  5366. }
  5367. // Without objname (default)
  5368. if(raw === true || info === true){
  5369. const extRecord = Object.assign(
  5370. {record: obj},
  5371. (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}),
  5372. (info === true ? {info: this.__infoRecord()}: {})
  5373. );
  5374. const err = this.__push(
  5375. objname === undefined ? extRecord : [obj[objname], extRecord]
  5376. , push);
  5377. if(err){
  5378. return err;
  5379. }
  5380. }else {
  5381. const err = this.__push(
  5382. objname === undefined ? obj : [obj[objname], obj]
  5383. , push);
  5384. if(err){
  5385. return err;
  5386. }
  5387. }
  5388. // Without columns, records are array
  5389. }else {
  5390. if(raw === true || info === true){
  5391. const extRecord = Object.assign(
  5392. {record: record},
  5393. raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
  5394. info === true ? {info: this.__infoRecord()}: {}
  5395. );
  5396. const err = this.__push(
  5397. objname === undefined ? extRecord : [record[objname], extRecord]
  5398. , push);
  5399. if(err){
  5400. return err;
  5401. }
  5402. }else {
  5403. const err = this.__push(
  5404. objname === undefined ? record : [record[objname], record]
  5405. , push);
  5406. if(err){
  5407. return err;
  5408. }
  5409. }
  5410. }
  5411. }
  5412. this.__resetRecord();
  5413. },
  5414. __firstLineToColumns: function(record){
  5415. const {firstLineToHeaders} = this.state;
  5416. try{
  5417. const headers = firstLineToHeaders === undefined ? record : firstLineToHeaders.call(null, record);
  5418. if(!Array.isArray(headers)){
  5419. return this.__error(
  5420. new CsvError('CSV_INVALID_COLUMN_MAPPING', [
  5421. 'Invalid Column Mapping:',
  5422. 'expect an array from column function,',
  5423. `got ${JSON.stringify(headers)}`
  5424. ], this.options, this.__infoField(), {
  5425. headers: headers,
  5426. })
  5427. );
  5428. }
  5429. const normalizedHeaders = normalize_columns_array(headers);
  5430. this.state.expectedRecordLength = normalizedHeaders.length;
  5431. this.options.columns = normalizedHeaders;
  5432. this.__resetRecord();
  5433. return;
  5434. }catch(err){
  5435. return err;
  5436. }
  5437. },
  5438. __resetRecord: function(){
  5439. if(this.options.raw === true){
  5440. this.state.rawBuffer.reset();
  5441. }
  5442. this.state.error = undefined;
  5443. this.state.record = [];
  5444. this.state.record_length = 0;
  5445. },
  5446. __onField: function(){
  5447. const {cast, encoding, rtrim, max_record_size} = this.options;
  5448. const {enabled, wasQuoting} = this.state;
  5449. // Short circuit for the from_line options
  5450. if(enabled === false){
  5451. return this.__resetField();
  5452. }
  5453. let field = this.state.field.toString(encoding);
  5454. if(rtrim === true && wasQuoting === false){
  5455. field = field.trimRight();
  5456. }
  5457. if(cast === true){
  5458. const [err, f] = this.__cast(field);
  5459. if(err !== undefined) return err;
  5460. field = f;
  5461. }
  5462. this.state.record.push(field);
  5463. // Increment record length if record size must not exceed a limit
  5464. if(max_record_size !== 0 && typeof field === 'string'){
  5465. this.state.record_length += field.length;
  5466. }
  5467. this.__resetField();
  5468. },
  5469. __resetField: function(){
  5470. this.state.field.reset();
  5471. this.state.wasQuoting = false;
  5472. },
  5473. __push: function(record, push){
  5474. const {on_record} = this.options;
  5475. if(on_record !== undefined){
  5476. const info = this.__infoRecord();
  5477. try{
  5478. record = on_record.call(null, record, info);
  5479. }catch(err){
  5480. return err;
  5481. }
  5482. if(record === undefined || record === null){ return; }
  5483. }
  5484. push(record);
  5485. },
  5486. // Return a tuple with the error and the casted value
  5487. __cast: function(field){
  5488. const {columns, relax_column_count} = this.options;
  5489. const isColumns = Array.isArray(columns);
  5490. // Dont loose time calling cast
  5491. // because the final record is an object
  5492. // and this field can't be associated to a key present in columns
  5493. if(isColumns === true && relax_column_count && this.options.columns.length <= this.state.record.length){
  5494. return [undefined, undefined];
  5495. }
  5496. if(this.state.castField !== null){
  5497. try{
  5498. const info = this.__infoField();
  5499. return [undefined, this.state.castField.call(null, field, info)];
  5500. }catch(err){
  5501. return [err];
  5502. }
  5503. }
  5504. if(this.__isFloat(field)){
  5505. return [undefined, parseFloat(field)];
  5506. }else if(this.options.cast_date !== false){
  5507. const info = this.__infoField();
  5508. return [undefined, this.options.cast_date.call(null, field, info)];
  5509. }
  5510. return [undefined, field];
  5511. },
  5512. // Helper to test if a character is a space or a line delimiter
  5513. __isCharTrimable: function(buf, pos){
  5514. const isTrim = (buf, pos) => {
  5515. const {timchars} = this.state;
  5516. loop1: for(let i = 0; i < timchars.length; i++){
  5517. const timchar = timchars[i];
  5518. for(let j = 0; j < timchar.length; j++){
  5519. if(timchar[j] !== buf[pos+j]) continue loop1;
  5520. }
  5521. return timchar.length;
  5522. }
  5523. return 0;
  5524. };
  5525. return isTrim(buf, pos);
  5526. },
  5527. // Keep it in case we implement the `cast_int` option
  5528. // __isInt(value){
  5529. // // return Number.isInteger(parseInt(value))
  5530. // // return !isNaN( parseInt( obj ) );
  5531. // return /^(\-|\+)?[1-9][0-9]*$/.test(value)
  5532. // }
  5533. __isFloat: function(value){
  5534. return (value - parseFloat(value) + 1) >= 0; // Borrowed from jquery
  5535. },
  5536. __compareBytes: function(sourceBuf, targetBuf, targetPos, firstByte){
  5537. if(sourceBuf[0] !== firstByte) return 0;
  5538. const sourceLength = sourceBuf.length;
  5539. for(let i = 1; i < sourceLength; i++){
  5540. if(sourceBuf[i] !== targetBuf[targetPos+i]) return 0;
  5541. }
  5542. return sourceLength;
  5543. },
  5544. __isDelimiter: function(buf, pos, chr){
  5545. const {delimiter, ignore_last_delimiters} = this.options;
  5546. if(ignore_last_delimiters === true && this.state.record.length === this.options.columns.length - 1){
  5547. return 0;
  5548. }else if(ignore_last_delimiters !== false && typeof ignore_last_delimiters === 'number' && this.state.record.length === ignore_last_delimiters - 1){
  5549. return 0;
  5550. }
  5551. loop1: for(let i = 0; i < delimiter.length; i++){
  5552. const del = delimiter[i];
  5553. if(del[0] === chr){
  5554. for(let j = 1; j < del.length; j++){
  5555. if(del[j] !== buf[pos+j]) continue loop1;
  5556. }
  5557. return del.length;
  5558. }
  5559. }
  5560. return 0;
  5561. },
  5562. __isRecordDelimiter: function(chr, buf, pos){
  5563. const {record_delimiter} = this.options;
  5564. const recordDelimiterLength = record_delimiter.length;
  5565. loop1: for(let i = 0; i < recordDelimiterLength; i++){
  5566. const rd = record_delimiter[i];
  5567. const rdLength = rd.length;
  5568. if(rd[0] !== chr){
  5569. continue;
  5570. }
  5571. for(let j = 1; j < rdLength; j++){
  5572. if(rd[j] !== buf[pos+j]){
  5573. continue loop1;
  5574. }
  5575. }
  5576. return rd.length;
  5577. }
  5578. return 0;
  5579. },
  5580. __isEscape: function(buf, pos, chr){
  5581. const {escape} = this.options;
  5582. if(escape === null) return false;
  5583. const l = escape.length;
  5584. if(escape[0] === chr){
  5585. for(let i = 0; i < l; i++){
  5586. if(escape[i] !== buf[pos+i]){
  5587. return false;
  5588. }
  5589. }
  5590. return true;
  5591. }
  5592. return false;
  5593. },
  5594. __isQuote: function(buf, pos){
  5595. const {quote} = this.options;
  5596. if(quote === null) return false;
  5597. const l = quote.length;
  5598. for(let i = 0; i < l; i++){
  5599. if(quote[i] !== buf[pos+i]){
  5600. return false;
  5601. }
  5602. }
  5603. return true;
  5604. },
  5605. __autoDiscoverRecordDelimiter: function(buf, pos){
  5606. const { encoding } = this.options;
  5607. // Note, we don't need to cache this information in state,
  5608. // It is only called on the first line until we find out a suitable
  5609. // record delimiter.
  5610. const rds = [
  5611. // Important, the windows line ending must be before mac os 9
  5612. Buffer.from('\r\n', encoding),
  5613. Buffer.from('\n', encoding),
  5614. Buffer.from('\r', encoding),
  5615. ];
  5616. loop: for(let i = 0; i < rds.length; i++){
  5617. const l = rds[i].length;
  5618. for(let j = 0; j < l; j++){
  5619. if(rds[i][j] !== buf[pos + j]){
  5620. continue loop;
  5621. }
  5622. }
  5623. this.options.record_delimiter.push(rds[i]);
  5624. this.state.recordDelimiterMaxLength = rds[i].length;
  5625. return rds[i].length;
  5626. }
  5627. return 0;
  5628. },
  5629. __error: function(msg){
  5630. const {encoding, raw, skip_records_with_error} = this.options;
  5631. const err = typeof msg === 'string' ? new Error(msg) : msg;
  5632. if(skip_records_with_error){
  5633. this.state.recordHasError = true;
  5634. if(this.options.on_skip !== undefined){
  5635. this.options.on_skip(err, raw ? this.state.rawBuffer.toString(encoding) : undefined);
  5636. }
  5637. // this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined);
  5638. return undefined;
  5639. }else {
  5640. return err;
  5641. }
  5642. },
  5643. __infoDataSet: function(){
  5644. return {
  5645. ...this.info,
  5646. columns: this.options.columns
  5647. };
  5648. },
  5649. __infoRecord: function(){
  5650. const {columns, raw, encoding} = this.options;
  5651. return {
  5652. ...this.__infoDataSet(),
  5653. error: this.state.error,
  5654. header: columns === true,
  5655. index: this.state.record.length,
  5656. raw: raw ? this.state.rawBuffer.toString(encoding) : undefined
  5657. };
  5658. },
  5659. __infoField: function(){
  5660. const {columns} = this.options;
  5661. const isColumns = Array.isArray(columns);
  5662. return {
  5663. ...this.__infoRecord(),
  5664. column: isColumns === true ?
  5665. (columns.length > this.state.record.length ?
  5666. columns[this.state.record.length].name :
  5667. null
  5668. ) :
  5669. this.state.record.length,
  5670. quoting: this.state.wasQuoting,
  5671. };
  5672. }
  5673. };
  5674. };
  5675. class Parser extends Transform {
  5676. constructor(opts = {}){
  5677. super({...{readableObjectMode: true}, ...opts, encoding: null});
  5678. this.api = transform(opts);
  5679. this.api.options.on_skip = (err, chunk) => {
  5680. this.emit('skip', err, chunk);
  5681. };
  5682. // Backward compatibility
  5683. this.state = this.api.state;
  5684. this.options = this.api.options;
  5685. this.info = this.api.info;
  5686. }
  5687. // Implementation of `Transform._transform`
  5688. _transform(buf, _, callback){
  5689. if(this.state.stop === true){
  5690. return;
  5691. }
  5692. const err = this.api.parse(buf, false, (record) => {
  5693. this.push(record);
  5694. }, () => {
  5695. this.push(null);
  5696. this.end();
  5697. // Fix #333 and break #410
  5698. // ko: api.stream.iterator.coffee
  5699. // ko with v21.4.0, ok with node v20.5.1: api.stream.finished # aborted (with generate())
  5700. // ko: api.stream.finished # aborted (with Readable)
  5701. // this.destroy()
  5702. // Fix #410 and partially break #333
  5703. // ok: api.stream.iterator.coffee
  5704. // ok: api.stream.finished # aborted (with generate())
  5705. // broken: api.stream.finished # aborted (with Readable)
  5706. this.on('end', this.destroy);
  5707. });
  5708. if(err !== undefined){
  5709. this.state.stop = true;
  5710. }
  5711. callback(err);
  5712. }
  5713. // Implementation of `Transform._flush`
  5714. _flush(callback){
  5715. if(this.state.stop === true){
  5716. return;
  5717. }
  5718. const err = this.api.parse(undefined, true, (record) => {
  5719. this.push(record);
  5720. }, () => {
  5721. this.push(null);
  5722. this.on('end', this.destroy);
  5723. });
  5724. callback(err);
  5725. }
  5726. }
  5727. const parse = function(){
  5728. let data, options, callback;
  5729. for(const i in arguments){
  5730. const argument = arguments[i];
  5731. const type = typeof argument;
  5732. if(data === undefined && (typeof argument === 'string' || isBuffer(argument))){
  5733. data = argument;
  5734. }else if(options === undefined && is_object(argument)){
  5735. options = argument;
  5736. }else if(callback === undefined && type === 'function'){
  5737. callback = argument;
  5738. }else {
  5739. throw new CsvError('CSV_INVALID_ARGUMENT', [
  5740. 'Invalid argument:',
  5741. `got ${JSON.stringify(argument)} at index ${i}`
  5742. ], options || {});
  5743. }
  5744. }
  5745. const parser = new Parser(options);
  5746. if(callback){
  5747. const records = options === undefined || options.objname === undefined ? [] : {};
  5748. parser.on('readable', function(){
  5749. let record;
  5750. while((record = this.read()) !== null){
  5751. if(options === undefined || options.objname === undefined){
  5752. records.push(record);
  5753. }else {
  5754. records[record[0]] = record[1];
  5755. }
  5756. }
  5757. });
  5758. parser.on('error', function(err){
  5759. callback(err, undefined, parser.api.__infoDataSet());
  5760. });
  5761. parser.on('end', function(){
  5762. callback(undefined, records, parser.api.__infoDataSet());
  5763. });
  5764. }
  5765. if(data !== undefined){
  5766. const writer = function(){
  5767. parser.write(data);
  5768. parser.end();
  5769. };
  5770. // Support Deno, Rollup doesnt provide a shim for setImmediate
  5771. if(typeof setImmediate === 'function'){
  5772. setImmediate(writer);
  5773. }else {
  5774. setTimeout(writer, 0);
  5775. }
  5776. }
  5777. return parser;
  5778. };
  5779. exports.CsvError = CsvError;
  5780. exports.Parser = Parser;
  5781. exports.parse = parse;
  5782. }));