Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
pirms 8 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.LessPass = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. var v1 = require('./src/v1');
  3. var v2 = require('./src/v2');
  4. var pbkdf2 = require('./src/pbkdf2');
  5. var objectAssign = require('object-assign');
  6. module.exports = {
  7. encryptLogin: v1.encryptLogin,
  8. renderPassword: v1.renderPassword,
  9. createFingerprint: v1.createFingerprint,
  10. _deriveEncryptedLogin: v1._deriveEncryptedLogin,
  11. _getPasswordTemplate: v1._getPasswordTemplate,
  12. _prettyPrint: v1._prettyPrint,
  13. _string2charCodes: v1._string2charCodes,
  14. _getCharType: v1._getCharType,
  15. _getPasswordChar: v1._getPasswordChar,
  16. _createHmac: v1._createHmac,
  17. generatePassword: generatePassword,
  18. _calcEntropy: v2._calcEntropy,
  19. _consumeEntropy: v2._consumeEntropy,
  20. _getSetOfCharacters: v2._getSetOfCharacters,
  21. _getConfiguredRules: v2._getConfiguredRules,
  22. _insertStringPseudoRandomly: v2._insertStringPseudoRandomly,
  23. _getOneCharPerRule: v2._getOneCharPerRule,
  24. _renderPassword: v2._renderPassword,
  25. pbkdf2: pbkdf2
  26. };
  27. var defaultPasswordProfile = {
  28. version: 2,
  29. lowercase: true,
  30. numbers: true,
  31. uppercase: true,
  32. symbols: true,
  33. keylen: 32,
  34. digest: 'sha256',
  35. length: 16,
  36. index: 1,
  37. iterations: 100000
  38. };
  39. function generatePassword(site, login, masterPassword, passwordProfile) {
  40. var _passwordProfile = objectAssign({}, defaultPasswordProfile, passwordProfile);
  41. if (_passwordProfile.version === 1) {
  42. var options = {
  43. counter: _passwordProfile.counter,
  44. length: _passwordProfile.length,
  45. lowercase: _passwordProfile.lowercase,
  46. uppercase: _passwordProfile.uppercase,
  47. numbers: _passwordProfile.numbers,
  48. symbols: _passwordProfile.symbols
  49. };
  50. return v1.encryptLogin(login, masterPassword)
  51. .then(function (encryptedLogin) {
  52. return v1.renderPassword(encryptedLogin, site, options).then(function (generatedPassword) {
  53. return generatedPassword
  54. });
  55. });
  56. }
  57. if (_passwordProfile.version === 2) {
  58. return v2.generatePassword(site, login, masterPassword, _passwordProfile);
  59. }
  60. }
  61. },{"./src/pbkdf2":48,"./src/v1":49,"./src/v2":50,"object-assign":18}],2:[function(require,module,exports){
  62. 'use strict'
  63. exports.byteLength = byteLength
  64. exports.toByteArray = toByteArray
  65. exports.fromByteArray = fromByteArray
  66. var lookup = []
  67. var revLookup = []
  68. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  69. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  70. for (var i = 0, len = code.length; i < len; ++i) {
  71. lookup[i] = code[i]
  72. revLookup[code.charCodeAt(i)] = i
  73. }
  74. revLookup['-'.charCodeAt(0)] = 62
  75. revLookup['_'.charCodeAt(0)] = 63
  76. function placeHoldersCount (b64) {
  77. var len = b64.length
  78. if (len % 4 > 0) {
  79. throw new Error('Invalid string. Length must be a multiple of 4')
  80. }
  81. // the number of equal signs (place holders)
  82. // if there are two placeholders, than the two characters before it
  83. // represent one byte
  84. // if there is only one, then the three characters before it represent 2 bytes
  85. // this is just a cheap hack to not do indexOf twice
  86. return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
  87. }
  88. function byteLength (b64) {
  89. // base64 is 4/3 + up to two characters of the original data
  90. return b64.length * 3 / 4 - placeHoldersCount(b64)
  91. }
  92. function toByteArray (b64) {
  93. var i, j, l, tmp, placeHolders, arr
  94. var len = b64.length
  95. placeHolders = placeHoldersCount(b64)
  96. arr = new Arr(len * 3 / 4 - placeHolders)
  97. // if there are placeholders, only get up to the last complete 4 chars
  98. l = placeHolders > 0 ? len - 4 : len
  99. var L = 0
  100. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  101. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
  102. arr[L++] = (tmp >> 16) & 0xFF
  103. arr[L++] = (tmp >> 8) & 0xFF
  104. arr[L++] = tmp & 0xFF
  105. }
  106. if (placeHolders === 2) {
  107. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
  108. arr[L++] = tmp & 0xFF
  109. } else if (placeHolders === 1) {
  110. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
  111. arr[L++] = (tmp >> 8) & 0xFF
  112. arr[L++] = tmp & 0xFF
  113. }
  114. return arr
  115. }
  116. function tripletToBase64 (num) {
  117. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  118. }
  119. function encodeChunk (uint8, start, end) {
  120. var tmp
  121. var output = []
  122. for (var i = start; i < end; i += 3) {
  123. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  124. output.push(tripletToBase64(tmp))
  125. }
  126. return output.join('')
  127. }
  128. function fromByteArray (uint8) {
  129. var tmp
  130. var len = uint8.length
  131. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  132. var output = ''
  133. var parts = []
  134. var maxChunkLength = 16383 // must be multiple of 3
  135. // go through the array every three bytes, we'll deal with trailing stuff later
  136. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  137. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
  138. }
  139. // pad the end with zeros, but make sure to not forget the extra bytes
  140. if (extraBytes === 1) {
  141. tmp = uint8[len - 1]
  142. output += lookup[tmp >> 2]
  143. output += lookup[(tmp << 4) & 0x3F]
  144. output += '=='
  145. } else if (extraBytes === 2) {
  146. tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
  147. output += lookup[tmp >> 10]
  148. output += lookup[(tmp >> 4) & 0x3F]
  149. output += lookup[(tmp << 2) & 0x3F]
  150. output += '='
  151. }
  152. parts.push(output)
  153. return parts.join('')
  154. }
  155. },{}],3:[function(require,module,exports){
  156. var bigInt = (function (undefined) {
  157. "use strict";
  158. var BASE = 1e7,
  159. LOG_BASE = 7,
  160. MAX_INT = 9007199254740992,
  161. MAX_INT_ARR = smallToArray(MAX_INT),
  162. LOG_MAX_INT = Math.log(MAX_INT);
  163. function Integer(v, radix) {
  164. if (typeof v === "undefined") return Integer[0];
  165. if (typeof radix !== "undefined") return +radix === 10 ? parseValue(v) : parseBase(v, radix);
  166. return parseValue(v);
  167. }
  168. function BigInteger(value, sign) {
  169. this.value = value;
  170. this.sign = sign;
  171. this.isSmall = false;
  172. }
  173. BigInteger.prototype = Object.create(Integer.prototype);
  174. function SmallInteger(value) {
  175. this.value = value;
  176. this.sign = value < 0;
  177. this.isSmall = true;
  178. }
  179. SmallInteger.prototype = Object.create(Integer.prototype);
  180. function isPrecise(n) {
  181. return -MAX_INT < n && n < MAX_INT;
  182. }
  183. function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
  184. if (n < 1e7)
  185. return [n];
  186. if (n < 1e14)
  187. return [n % 1e7, Math.floor(n / 1e7)];
  188. return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
  189. }
  190. function arrayToSmall(arr) { // If BASE changes this function may need to change
  191. trim(arr);
  192. var length = arr.length;
  193. if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
  194. switch (length) {
  195. case 0: return 0;
  196. case 1: return arr[0];
  197. case 2: return arr[0] + arr[1] * BASE;
  198. default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
  199. }
  200. }
  201. return arr;
  202. }
  203. function trim(v) {
  204. var i = v.length;
  205. while (v[--i] === 0);
  206. v.length = i + 1;
  207. }
  208. function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
  209. var x = new Array(length);
  210. var i = -1;
  211. while (++i < length) {
  212. x[i] = 0;
  213. }
  214. return x;
  215. }
  216. function truncate(n) {
  217. if (n > 0) return Math.floor(n);
  218. return Math.ceil(n);
  219. }
  220. function add(a, b) { // assumes a and b are arrays with a.length >= b.length
  221. var l_a = a.length,
  222. l_b = b.length,
  223. r = new Array(l_a),
  224. carry = 0,
  225. base = BASE,
  226. sum, i;
  227. for (i = 0; i < l_b; i++) {
  228. sum = a[i] + b[i] + carry;
  229. carry = sum >= base ? 1 : 0;
  230. r[i] = sum - carry * base;
  231. }
  232. while (i < l_a) {
  233. sum = a[i] + carry;
  234. carry = sum === base ? 1 : 0;
  235. r[i++] = sum - carry * base;
  236. }
  237. if (carry > 0) r.push(carry);
  238. return r;
  239. }
  240. function addAny(a, b) {
  241. if (a.length >= b.length) return add(a, b);
  242. return add(b, a);
  243. }
  244. function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
  245. var l = a.length,
  246. r = new Array(l),
  247. base = BASE,
  248. sum, i;
  249. for (i = 0; i < l; i++) {
  250. sum = a[i] - base + carry;
  251. carry = Math.floor(sum / base);
  252. r[i] = sum - carry * base;
  253. carry += 1;
  254. }
  255. while (carry > 0) {
  256. r[i++] = carry % base;
  257. carry = Math.floor(carry / base);
  258. }
  259. return r;
  260. }
  261. BigInteger.prototype.add = function (v) {
  262. var value, n = parseValue(v);
  263. if (this.sign !== n.sign) {
  264. return this.subtract(n.negate());
  265. }
  266. var a = this.value, b = n.value;
  267. if (n.isSmall) {
  268. return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
  269. }
  270. return new BigInteger(addAny(a, b), this.sign);
  271. };
  272. BigInteger.prototype.plus = BigInteger.prototype.add;
  273. SmallInteger.prototype.add = function (v) {
  274. var n = parseValue(v);
  275. var a = this.value;
  276. if (a < 0 !== n.sign) {
  277. return this.subtract(n.negate());
  278. }
  279. var b = n.value;
  280. if (n.isSmall) {
  281. if (isPrecise(a + b)) return new SmallInteger(a + b);
  282. b = smallToArray(Math.abs(b));
  283. }
  284. return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
  285. };
  286. SmallInteger.prototype.plus = SmallInteger.prototype.add;
  287. function subtract(a, b) { // assumes a and b are arrays with a >= b
  288. var a_l = a.length,
  289. b_l = b.length,
  290. r = new Array(a_l),
  291. borrow = 0,
  292. base = BASE,
  293. i, difference;
  294. for (i = 0; i < b_l; i++) {
  295. difference = a[i] - borrow - b[i];
  296. if (difference < 0) {
  297. difference += base;
  298. borrow = 1;
  299. } else borrow = 0;
  300. r[i] = difference;
  301. }
  302. for (i = b_l; i < a_l; i++) {
  303. difference = a[i] - borrow;
  304. if (difference < 0) difference += base;
  305. else {
  306. r[i++] = difference;
  307. break;
  308. }
  309. r[i] = difference;
  310. }
  311. for (; i < a_l; i++) {
  312. r[i] = a[i];
  313. }
  314. trim(r);
  315. return r;
  316. }
  317. function subtractAny(a, b, sign) {
  318. var value, isSmall;
  319. if (compareAbs(a, b) >= 0) {
  320. value = subtract(a,b);
  321. } else {
  322. value = subtract(b, a);
  323. sign = !sign;
  324. }
  325. value = arrayToSmall(value);
  326. if (typeof value === "number") {
  327. if (sign) value = -value;
  328. return new SmallInteger(value);
  329. }
  330. return new BigInteger(value, sign);
  331. }
  332. function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
  333. var l = a.length,
  334. r = new Array(l),
  335. carry = -b,
  336. base = BASE,
  337. i, difference;
  338. for (i = 0; i < l; i++) {
  339. difference = a[i] + carry;
  340. carry = Math.floor(difference / base);
  341. difference %= base;
  342. r[i] = difference < 0 ? difference + base : difference;
  343. }
  344. r = arrayToSmall(r);
  345. if (typeof r === "number") {
  346. if (sign) r = -r;
  347. return new SmallInteger(r);
  348. } return new BigInteger(r, sign);
  349. }
  350. BigInteger.prototype.subtract = function (v) {
  351. var n = parseValue(v);
  352. if (this.sign !== n.sign) {
  353. return this.add(n.negate());
  354. }
  355. var a = this.value, b = n.value;
  356. if (n.isSmall)
  357. return subtractSmall(a, Math.abs(b), this.sign);
  358. return subtractAny(a, b, this.sign);
  359. };
  360. BigInteger.prototype.minus = BigInteger.prototype.subtract;
  361. SmallInteger.prototype.subtract = function (v) {
  362. var n = parseValue(v);
  363. var a = this.value;
  364. if (a < 0 !== n.sign) {
  365. return this.add(n.negate());
  366. }
  367. var b = n.value;
  368. if (n.isSmall) {
  369. return new SmallInteger(a - b);
  370. }
  371. return subtractSmall(b, Math.abs(a), a >= 0);
  372. };
  373. SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
  374. BigInteger.prototype.negate = function () {
  375. return new BigInteger(this.value, !this.sign);
  376. };
  377. SmallInteger.prototype.negate = function () {
  378. var sign = this.sign;
  379. var small = new SmallInteger(-this.value);
  380. small.sign = !sign;
  381. return small;
  382. };
  383. BigInteger.prototype.abs = function () {
  384. return new BigInteger(this.value, false);
  385. };
  386. SmallInteger.prototype.abs = function () {
  387. return new SmallInteger(Math.abs(this.value));
  388. };
  389. function multiplyLong(a, b) {
  390. var a_l = a.length,
  391. b_l = b.length,
  392. l = a_l + b_l,
  393. r = createArray(l),
  394. base = BASE,
  395. product, carry, i, a_i, b_j;
  396. for (i = 0; i < a_l; ++i) {
  397. a_i = a[i];
  398. for (var j = 0; j < b_l; ++j) {
  399. b_j = b[j];
  400. product = a_i * b_j + r[i + j];
  401. carry = Math.floor(product / base);
  402. r[i + j] = product - carry * base;
  403. r[i + j + 1] += carry;
  404. }
  405. }
  406. trim(r);
  407. return r;
  408. }
  409. function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
  410. var l = a.length,
  411. r = new Array(l),
  412. base = BASE,
  413. carry = 0,
  414. product, i;
  415. for (i = 0; i < l; i++) {
  416. product = a[i] * b + carry;
  417. carry = Math.floor(product / base);
  418. r[i] = product - carry * base;
  419. }
  420. while (carry > 0) {
  421. r[i++] = carry % base;
  422. carry = Math.floor(carry / base);
  423. }
  424. return r;
  425. }
  426. function shiftLeft(x, n) {
  427. var r = [];
  428. while (n-- > 0) r.push(0);
  429. return r.concat(x);
  430. }
  431. function multiplyKaratsuba(x, y) {
  432. var n = Math.max(x.length, y.length);
  433. if (n <= 30) return multiplyLong(x, y);
  434. n = Math.ceil(n / 2);
  435. var b = x.slice(n),
  436. a = x.slice(0, n),
  437. d = y.slice(n),
  438. c = y.slice(0, n);
  439. var ac = multiplyKaratsuba(a, c),
  440. bd = multiplyKaratsuba(b, d),
  441. abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
  442. var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
  443. trim(product);
  444. return product;
  445. }
  446. // The following function is derived from a surface fit of a graph plotting the performance difference
  447. // between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
  448. function useKaratsuba(l1, l2) {
  449. return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
  450. }
  451. BigInteger.prototype.multiply = function (v) {
  452. var value, n = parseValue(v),
  453. a = this.value, b = n.value,
  454. sign = this.sign !== n.sign,
  455. abs;
  456. if (n.isSmall) {
  457. if (b === 0) return Integer[0];
  458. if (b === 1) return this;
  459. if (b === -1) return this.negate();
  460. abs = Math.abs(b);
  461. if (abs < BASE) {
  462. return new BigInteger(multiplySmall(a, abs), sign);
  463. }
  464. b = smallToArray(abs);
  465. }
  466. if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes
  467. return new BigInteger(multiplyKaratsuba(a, b), sign);
  468. return new BigInteger(multiplyLong(a, b), sign);
  469. };
  470. BigInteger.prototype.times = BigInteger.prototype.multiply;
  471. function multiplySmallAndArray(a, b, sign) { // a >= 0
  472. if (a < BASE) {
  473. return new BigInteger(multiplySmall(b, a), sign);
  474. }
  475. return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
  476. }
  477. SmallInteger.prototype._multiplyBySmall = function (a) {
  478. if (isPrecise(a.value * this.value)) {
  479. return new SmallInteger(a.value * this.value);
  480. }
  481. return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
  482. };
  483. BigInteger.prototype._multiplyBySmall = function (a) {
  484. if (a.value === 0) return Integer[0];
  485. if (a.value === 1) return this;
  486. if (a.value === -1) return this.negate();
  487. return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
  488. };
  489. SmallInteger.prototype.multiply = function (v) {
  490. return parseValue(v)._multiplyBySmall(this);
  491. };
  492. SmallInteger.prototype.times = SmallInteger.prototype.multiply;
  493. function square(a) {
  494. var l = a.length,
  495. r = createArray(l + l),
  496. base = BASE,
  497. product, carry, i, a_i, a_j;
  498. for (i = 0; i < l; i++) {
  499. a_i = a[i];
  500. for (var j = 0; j < l; j++) {
  501. a_j = a[j];
  502. product = a_i * a_j + r[i + j];
  503. carry = Math.floor(product / base);
  504. r[i + j] = product - carry * base;
  505. r[i + j + 1] += carry;
  506. }
  507. }
  508. trim(r);
  509. return r;
  510. }
  511. BigInteger.prototype.square = function () {
  512. return new BigInteger(square(this.value), false);
  513. };
  514. SmallInteger.prototype.square = function () {
  515. var value = this.value * this.value;
  516. if (isPrecise(value)) return new SmallInteger(value);
  517. return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
  518. };
  519. function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
  520. var a_l = a.length,
  521. b_l = b.length,
  522. base = BASE,
  523. result = createArray(b.length),
  524. divisorMostSignificantDigit = b[b_l - 1],
  525. // normalization
  526. lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
  527. remainder = multiplySmall(a, lambda),
  528. divisor = multiplySmall(b, lambda),
  529. quotientDigit, shift, carry, borrow, i, l, q;
  530. if (remainder.length <= a_l) remainder.push(0);
  531. divisor.push(0);
  532. divisorMostSignificantDigit = divisor[b_l - 1];
  533. for (shift = a_l - b_l; shift >= 0; shift--) {
  534. quotientDigit = base - 1;
  535. if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
  536. quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
  537. }
  538. // quotientDigit <= base - 1
  539. carry = 0;
  540. borrow = 0;
  541. l = divisor.length;
  542. for (i = 0; i < l; i++) {
  543. carry += quotientDigit * divisor[i];
  544. q = Math.floor(carry / base);
  545. borrow += remainder[shift + i] - (carry - q * base);
  546. carry = q;
  547. if (borrow < 0) {
  548. remainder[shift + i] = borrow + base;
  549. borrow = -1;
  550. } else {
  551. remainder[shift + i] = borrow;
  552. borrow = 0;
  553. }
  554. }
  555. while (borrow !== 0) {
  556. quotientDigit -= 1;
  557. carry = 0;
  558. for (i = 0; i < l; i++) {
  559. carry += remainder[shift + i] - base + divisor[i];
  560. if (carry < 0) {
  561. remainder[shift + i] = carry + base;
  562. carry = 0;
  563. } else {
  564. remainder[shift + i] = carry;
  565. carry = 1;
  566. }
  567. }
  568. borrow += carry;
  569. }
  570. result[shift] = quotientDigit;
  571. }
  572. // denormalization
  573. remainder = divModSmall(remainder, lambda)[0];
  574. return [arrayToSmall(result), arrayToSmall(remainder)];
  575. }
  576. function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
  577. // Performs faster than divMod1 on larger input sizes.
  578. var a_l = a.length,
  579. b_l = b.length,
  580. result = [],
  581. part = [],
  582. base = BASE,
  583. guess, xlen, highx, highy, check;
  584. while (a_l) {
  585. part.unshift(a[--a_l]);
  586. if (compareAbs(part, b) < 0) {
  587. result.push(0);
  588. continue;
  589. }
  590. xlen = part.length;
  591. highx = part[xlen - 1] * base + part[xlen - 2];
  592. highy = b[b_l - 1] * base + b[b_l - 2];
  593. if (xlen > b_l) {
  594. highx = (highx + 1) * base;
  595. }
  596. guess = Math.ceil(highx / highy);
  597. do {
  598. check = multiplySmall(b, guess);
  599. if (compareAbs(check, part) <= 0) break;
  600. guess--;
  601. } while (guess);
  602. result.push(guess);
  603. part = subtract(part, check);
  604. }
  605. result.reverse();
  606. return [arrayToSmall(result), arrayToSmall(part)];
  607. }
  608. function divModSmall(value, lambda) {
  609. var length = value.length,
  610. quotient = createArray(length),
  611. base = BASE,
  612. i, q, remainder, divisor;
  613. remainder = 0;
  614. for (i = length - 1; i >= 0; --i) {
  615. divisor = remainder * base + value[i];
  616. q = truncate(divisor / lambda);
  617. remainder = divisor - q * lambda;
  618. quotient[i] = q | 0;
  619. }
  620. return [quotient, remainder | 0];
  621. }
  622. function divModAny(self, v) {
  623. var value, n = parseValue(v);
  624. var a = self.value, b = n.value;
  625. var quotient;
  626. if (b === 0) throw new Error("Cannot divide by zero");
  627. if (self.isSmall) {
  628. if (n.isSmall) {
  629. return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
  630. }
  631. return [Integer[0], self];
  632. }
  633. if (n.isSmall) {
  634. if (b === 1) return [self, Integer[0]];
  635. if (b == -1) return [self.negate(), Integer[0]];
  636. var abs = Math.abs(b);
  637. if (abs < BASE) {
  638. value = divModSmall(a, abs);
  639. quotient = arrayToSmall(value[0]);
  640. var remainder = value[1];
  641. if (self.sign) remainder = -remainder;
  642. if (typeof quotient === "number") {
  643. if (self.sign !== n.sign) quotient = -quotient;
  644. return [new SmallInteger(quotient), new SmallInteger(remainder)];
  645. }
  646. return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
  647. }
  648. b = smallToArray(abs);
  649. }
  650. var comparison = compareAbs(a, b);
  651. if (comparison === -1) return [Integer[0], self];
  652. if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];
  653. // divMod1 is faster on smaller input sizes
  654. if (a.length + b.length <= 200)
  655. value = divMod1(a, b);
  656. else value = divMod2(a, b);
  657. quotient = value[0];
  658. var qSign = self.sign !== n.sign,
  659. mod = value[1],
  660. mSign = self.sign;
  661. if (typeof quotient === "number") {
  662. if (qSign) quotient = -quotient;
  663. quotient = new SmallInteger(quotient);
  664. } else quotient = new BigInteger(quotient, qSign);
  665. if (typeof mod === "number") {
  666. if (mSign) mod = -mod;
  667. mod = new SmallInteger(mod);
  668. } else mod = new BigInteger(mod, mSign);
  669. return [quotient, mod];
  670. }
  671. BigInteger.prototype.divmod = function (v) {
  672. var result = divModAny(this, v);
  673. return {
  674. quotient: result[0],
  675. remainder: result[1]
  676. };
  677. };
  678. SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
  679. BigInteger.prototype.divide = function (v) {
  680. return divModAny(this, v)[0];
  681. };
  682. SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
  683. BigInteger.prototype.mod = function (v) {
  684. return divModAny(this, v)[1];
  685. };
  686. SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
  687. BigInteger.prototype.pow = function (v) {
  688. var n = parseValue(v),
  689. a = this.value,
  690. b = n.value,
  691. value, x, y;
  692. if (b === 0) return Integer[1];
  693. if (a === 0) return Integer[0];
  694. if (a === 1) return Integer[1];
  695. if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
  696. if (n.sign) {
  697. return Integer[0];
  698. }
  699. if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
  700. if (this.isSmall) {
  701. if (isPrecise(value = Math.pow(a, b)))
  702. return new SmallInteger(truncate(value));
  703. }
  704. x = this;
  705. y = Integer[1];
  706. while (true) {
  707. if (b & 1 === 1) {
  708. y = y.times(x);
  709. --b;
  710. }
  711. if (b === 0) break;
  712. b /= 2;
  713. x = x.square();
  714. }
  715. return y;
  716. };
  717. SmallInteger.prototype.pow = BigInteger.prototype.pow;
  718. BigInteger.prototype.modPow = function (exp, mod) {
  719. exp = parseValue(exp);
  720. mod = parseValue(mod);
  721. if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
  722. var r = Integer[1],
  723. base = this.mod(mod);
  724. while (exp.isPositive()) {
  725. if (base.isZero()) return Integer[0];
  726. if (exp.isOdd()) r = r.multiply(base).mod(mod);
  727. exp = exp.divide(2);
  728. base = base.square().mod(mod);
  729. }
  730. return r;
  731. };
  732. SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
  733. function compareAbs(a, b) {
  734. if (a.length !== b.length) {
  735. return a.length > b.length ? 1 : -1;
  736. }
  737. for (var i = a.length - 1; i >= 0; i--) {
  738. if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
  739. }
  740. return 0;
  741. }
  742. BigInteger.prototype.compareAbs = function (v) {
  743. var n = parseValue(v),
  744. a = this.value,
  745. b = n.value;
  746. if (n.isSmall) return 1;
  747. return compareAbs(a, b);
  748. };
  749. SmallInteger.prototype.compareAbs = function (v) {
  750. var n = parseValue(v),
  751. a = Math.abs(this.value),
  752. b = n.value;
  753. if (n.isSmall) {
  754. b = Math.abs(b);
  755. return a === b ? 0 : a > b ? 1 : -1;
  756. }
  757. return -1;
  758. };
  759. BigInteger.prototype.compare = function (v) {
  760. // See discussion about comparison with Infinity:
  761. // https://github.com/peterolson/BigInteger.js/issues/61
  762. if (v === Infinity) {
  763. return -1;
  764. }
  765. if (v === -Infinity) {
  766. return 1;
  767. }
  768. var n = parseValue(v),
  769. a = this.value,
  770. b = n.value;
  771. if (this.sign !== n.sign) {
  772. return n.sign ? 1 : -1;
  773. }
  774. if (n.isSmall) {
  775. return this.sign ? -1 : 1;
  776. }
  777. return compareAbs(a, b) * (this.sign ? -1 : 1);
  778. };
  779. BigInteger.prototype.compareTo = BigInteger.prototype.compare;
  780. SmallInteger.prototype.compare = function (v) {
  781. if (v === Infinity) {
  782. return -1;
  783. }
  784. if (v === -Infinity) {
  785. return 1;
  786. }
  787. var n = parseValue(v),
  788. a = this.value,
  789. b = n.value;
  790. if (n.isSmall) {
  791. return a == b ? 0 : a > b ? 1 : -1;
  792. }
  793. if (a < 0 !== n.sign) {
  794. return a < 0 ? -1 : 1;
  795. }
  796. return a < 0 ? 1 : -1;
  797. };
  798. SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
  799. BigInteger.prototype.equals = function (v) {
  800. return this.compare(v) === 0;
  801. };
  802. SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
  803. BigInteger.prototype.notEquals = function (v) {
  804. return this.compare(v) !== 0;
  805. };
  806. SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
  807. BigInteger.prototype.greater = function (v) {
  808. return this.compare(v) > 0;
  809. };
  810. SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
  811. BigInteger.prototype.lesser = function (v) {
  812. return this.compare(v) < 0;
  813. };
  814. SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
  815. BigInteger.prototype.greaterOrEquals = function (v) {
  816. return this.compare(v) >= 0;
  817. };
  818. SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
  819. BigInteger.prototype.lesserOrEquals = function (v) {
  820. return this.compare(v) <= 0;
  821. };
  822. SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
  823. BigInteger.prototype.isEven = function () {
  824. return (this.value[0] & 1) === 0;
  825. };
  826. SmallInteger.prototype.isEven = function () {
  827. return (this.value & 1) === 0;
  828. };
  829. BigInteger.prototype.isOdd = function () {
  830. return (this.value[0] & 1) === 1;
  831. };
  832. SmallInteger.prototype.isOdd = function () {
  833. return (this.value & 1) === 1;
  834. };
  835. BigInteger.prototype.isPositive = function () {
  836. return !this.sign;
  837. };
  838. SmallInteger.prototype.isPositive = function () {
  839. return this.value > 0;
  840. };
  841. BigInteger.prototype.isNegative = function () {
  842. return this.sign;
  843. };
  844. SmallInteger.prototype.isNegative = function () {
  845. return this.value < 0;
  846. };
  847. BigInteger.prototype.isUnit = function () {
  848. return false;
  849. };
  850. SmallInteger.prototype.isUnit = function () {
  851. return Math.abs(this.value) === 1;
  852. };
  853. BigInteger.prototype.isZero = function () {
  854. return false;
  855. };
  856. SmallInteger.prototype.isZero = function () {
  857. return this.value === 0;
  858. };
  859. BigInteger.prototype.isDivisibleBy = function (v) {
  860. var n = parseValue(v);
  861. var value = n.value;
  862. if (value === 0) return false;
  863. if (value === 1) return true;
  864. if (value === 2) return this.isEven();
  865. return this.mod(n).equals(Integer[0]);
  866. };
  867. SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
  868. function isBasicPrime(v) {
  869. var n = v.abs();
  870. if (n.isUnit()) return false;
  871. if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
  872. if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
  873. if (n.lesser(25)) return true;
  874. // we don't know if it's prime: let the other functions figure it out
  875. }
  876. BigInteger.prototype.isPrime = function () {
  877. var isPrime = isBasicPrime(this);
  878. if (isPrime !== undefined) return isPrime;
  879. var n = this.abs(),
  880. nPrev = n.prev();
  881. var a = [2, 3, 5, 7, 11, 13, 17, 19],
  882. b = nPrev,
  883. d, t, i, x;
  884. while (b.isEven()) b = b.divide(2);
  885. for (i = 0; i < a.length; i++) {
  886. x = bigInt(a[i]).modPow(b, n);
  887. if (x.equals(Integer[1]) || x.equals(nPrev)) continue;
  888. for (t = true, d = b; t && d.lesser(nPrev) ; d = d.multiply(2)) {
  889. x = x.square().mod(n);
  890. if (x.equals(nPrev)) t = false;
  891. }
  892. if (t) return false;
  893. }
  894. return true;
  895. };
  896. SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
  897. BigInteger.prototype.isProbablePrime = function (iterations) {
  898. var isPrime = isBasicPrime(this);
  899. if (isPrime !== undefined) return isPrime;
  900. var n = this.abs();
  901. var t = iterations === undefined ? 5 : iterations;
  902. // use the Fermat primality test
  903. for (var i = 0; i < t; i++) {
  904. var a = bigInt.randBetween(2, n.minus(2));
  905. if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite
  906. }
  907. return true; // large chance of being prime
  908. };
  909. SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
  910. BigInteger.prototype.modInv = function (n) {
  911. var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR;
  912. while (!newR.equals(bigInt.zero)) {
  913. q = r.divide(newR);
  914. lastT = t;
  915. lastR = r;
  916. t = newT;
  917. r = newR;
  918. newT = lastT.subtract(q.multiply(newT));
  919. newR = lastR.subtract(q.multiply(newR));
  920. }
  921. if (!r.equals(1)) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime");
  922. if (t.compare(0) === -1) {
  923. t = t.add(n);
  924. }
  925. return t;
  926. }
  927. SmallInteger.prototype.modInv = BigInteger.prototype.modInv;
  928. BigInteger.prototype.next = function () {
  929. var value = this.value;
  930. if (this.sign) {
  931. return subtractSmall(value, 1, this.sign);
  932. }
  933. return new BigInteger(addSmall(value, 1), this.sign);
  934. };
  935. SmallInteger.prototype.next = function () {
  936. var value = this.value;
  937. if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
  938. return new BigInteger(MAX_INT_ARR, false);
  939. };
  940. BigInteger.prototype.prev = function () {
  941. var value = this.value;
  942. if (this.sign) {
  943. return new BigInteger(addSmall(value, 1), true);
  944. }
  945. return subtractSmall(value, 1, this.sign);
  946. };
  947. SmallInteger.prototype.prev = function () {
  948. var value = this.value;
  949. if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
  950. return new BigInteger(MAX_INT_ARR, true);
  951. };
  952. var powersOfTwo = [1];
  953. while (powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
  954. var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
  955. function shift_isSmall(n) {
  956. return ((typeof n === "number" || typeof n === "string") && +Math.abs(n) <= BASE) ||
  957. (n instanceof BigInteger && n.value.length <= 1);
  958. }
  959. BigInteger.prototype.shiftLeft = function (n) {
  960. if (!shift_isSmall(n)) {
  961. throw new Error(String(n) + " is too large for shifting.");
  962. }
  963. n = +n;
  964. if (n < 0) return this.shiftRight(-n);
  965. var result = this;
  966. while (n >= powers2Length) {
  967. result = result.multiply(highestPower2);
  968. n -= powers2Length - 1;
  969. }
  970. return result.multiply(powersOfTwo[n]);
  971. };
  972. SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
  973. BigInteger.prototype.shiftRight = function (n) {
  974. var remQuo;
  975. if (!shift_isSmall(n)) {
  976. throw new Error(String(n) + " is too large for shifting.");
  977. }
  978. n = +n;
  979. if (n < 0) return this.shiftLeft(-n);
  980. var result = this;
  981. while (n >= powers2Length) {
  982. if (result.isZero()) return result;
  983. remQuo = divModAny(result, highestPower2);
  984. result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  985. n -= powers2Length - 1;
  986. }
  987. remQuo = divModAny(result, powersOfTwo[n]);
  988. return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  989. };
  990. SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
  991. function bitwise(x, y, fn) {
  992. y = parseValue(y);
  993. var xSign = x.isNegative(), ySign = y.isNegative();
  994. var xRem = xSign ? x.not() : x,
  995. yRem = ySign ? y.not() : y;
  996. var xBits = [], yBits = [];
  997. var xStop = false, yStop = false;
  998. while (!xStop || !yStop) {
  999. if (xRem.isZero()) { // virtual sign extension for simulating two's complement
  1000. xStop = true;
  1001. xBits.push(xSign ? 1 : 0);
  1002. }
  1003. else if (xSign) xBits.push(xRem.isEven() ? 1 : 0); // two's complement for negative numbers
  1004. else xBits.push(xRem.isEven() ? 0 : 1);
  1005. if (yRem.isZero()) {
  1006. yStop = true;
  1007. yBits.push(ySign ? 1 : 0);
  1008. }
  1009. else if (ySign) yBits.push(yRem.isEven() ? 1 : 0);
  1010. else yBits.push(yRem.isEven() ? 0 : 1);
  1011. xRem = xRem.over(2);
  1012. yRem = yRem.over(2);
  1013. }
  1014. var result = [];
  1015. for (var i = 0; i < xBits.length; i++) result.push(fn(xBits[i], yBits[i]));
  1016. var sum = bigInt(result.pop()).negate().times(bigInt(2).pow(result.length));
  1017. while (result.length) {
  1018. sum = sum.add(bigInt(result.pop()).times(bigInt(2).pow(result.length)));
  1019. }
  1020. return sum;
  1021. }
  1022. BigInteger.prototype.not = function () {
  1023. return this.negate().prev();
  1024. };
  1025. SmallInteger.prototype.not = BigInteger.prototype.not;
  1026. BigInteger.prototype.and = function (n) {
  1027. return bitwise(this, n, function (a, b) { return a & b; });
  1028. };
  1029. SmallInteger.prototype.and = BigInteger.prototype.and;
  1030. BigInteger.prototype.or = function (n) {
  1031. return bitwise(this, n, function (a, b) { return a | b; });
  1032. };
  1033. SmallInteger.prototype.or = BigInteger.prototype.or;
  1034. BigInteger.prototype.xor = function (n) {
  1035. return bitwise(this, n, function (a, b) { return a ^ b; });
  1036. };
  1037. SmallInteger.prototype.xor = BigInteger.prototype.xor;
  1038. var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I;
  1039. function roughLOB(n) { // get lowestOneBit (rough)
  1040. // SmallInteger: return Min(lowestOneBit(n), 1 << 30)
  1041. // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
  1042. var v = n.value, x = typeof v === "number" ? v | LOBMASK_I : v[0] + v[1] * BASE | LOBMASK_BI;
  1043. return x & -x;
  1044. }
  1045. function max(a, b) {
  1046. a = parseValue(a);
  1047. b = parseValue(b);
  1048. return a.greater(b) ? a : b;
  1049. }
  1050. function min(a,b) {
  1051. a = parseValue(a);
  1052. b = parseValue(b);
  1053. return a.lesser(b) ? a : b;
  1054. }
  1055. function gcd(a, b) {
  1056. a = parseValue(a).abs();
  1057. b = parseValue(b).abs();
  1058. if (a.equals(b)) return a;
  1059. if (a.isZero()) return b;
  1060. if (b.isZero()) return a;
  1061. var c = Integer[1], d, t;
  1062. while (a.isEven() && b.isEven()) {
  1063. d = Math.min(roughLOB(a), roughLOB(b));
  1064. a = a.divide(d);
  1065. b = b.divide(d);
  1066. c = c.multiply(d);
  1067. }
  1068. while (a.isEven()) {
  1069. a = a.divide(roughLOB(a));
  1070. }
  1071. do {
  1072. while (b.isEven()) {
  1073. b = b.divide(roughLOB(b));
  1074. }
  1075. if (a.greater(b)) {
  1076. t = b; b = a; a = t;
  1077. }
  1078. b = b.subtract(a);
  1079. } while (!b.isZero());
  1080. return c.isUnit() ? a : a.multiply(c);
  1081. }
  1082. function lcm(a, b) {
  1083. a = parseValue(a).abs();
  1084. b = parseValue(b).abs();
  1085. return a.divide(gcd(a, b)).multiply(b);
  1086. }
  1087. function randBetween(a, b) {
  1088. a = parseValue(a);
  1089. b = parseValue(b);
  1090. var low = min(a, b), high = max(a, b);
  1091. var range = high.subtract(low);
  1092. if (range.isSmall) return low.add(Math.round(Math.random() * range));
  1093. var length = range.value.length - 1;
  1094. var result = [], restricted = true;
  1095. for (var i = length; i >= 0; i--) {
  1096. var top = restricted ? range.value[i] : BASE;
  1097. var digit = truncate(Math.random() * top);
  1098. result.unshift(digit);
  1099. if (digit < top) restricted = false;
  1100. }
  1101. result = arrayToSmall(result);
  1102. return low.add(typeof result === "number" ? new SmallInteger(result) : new BigInteger(result, false));
  1103. }
  1104. var parseBase = function (text, base) {
  1105. var val = Integer[0], pow = Integer[1],
  1106. length = text.length;
  1107. if (2 <= base && base <= 36) {
  1108. if (length <= LOG_MAX_INT / Math.log(base)) {
  1109. return new SmallInteger(parseInt(text, base));
  1110. }
  1111. }
  1112. base = parseValue(base);
  1113. var digits = [];
  1114. var i;
  1115. var isNegative = text[0] === "-";
  1116. for (i = isNegative ? 1 : 0; i < text.length; i++) {
  1117. var c = text[i].toLowerCase(),
  1118. charCode = c.charCodeAt(0);
  1119. if (48 <= charCode && charCode <= 57) digits.push(parseValue(c));
  1120. else if (97 <= charCode && charCode <= 122) digits.push(parseValue(c.charCodeAt(0) - 87));
  1121. else if (c === "<") {
  1122. var start = i;
  1123. do { i++; } while (text[i] !== ">");
  1124. digits.push(parseValue(text.slice(start + 1, i)));
  1125. }
  1126. else throw new Error(c + " is not a valid character");
  1127. }
  1128. digits.reverse();
  1129. for (i = 0; i < digits.length; i++) {
  1130. val = val.add(digits[i].times(pow));
  1131. pow = pow.times(base);
  1132. }
  1133. return isNegative ? val.negate() : val;
  1134. };
  1135. function stringify(digit) {
  1136. var v = digit.value;
  1137. if (typeof v === "number") v = [v];
  1138. if (v.length === 1 && v[0] <= 35) {
  1139. return "0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0]);
  1140. }
  1141. return "<" + v + ">";
  1142. }
  1143. function toBase(n, base) {
  1144. base = bigInt(base);
  1145. if (base.isZero()) {
  1146. if (n.isZero()) return "0";
  1147. throw new Error("Cannot convert nonzero numbers to base 0.");
  1148. }
  1149. if (base.equals(-1)) {
  1150. if (n.isZero()) return "0";
  1151. if (n.isNegative()) return new Array(1 - n).join("10");
  1152. return "1" + new Array(+n).join("01");
  1153. }
  1154. var minusSign = "";
  1155. if (n.isNegative() && base.isPositive()) {
  1156. minusSign = "-";
  1157. n = n.abs();
  1158. }
  1159. if (base.equals(1)) {
  1160. if (n.isZero()) return "0";
  1161. return minusSign + new Array(+n + 1).join(1);
  1162. }
  1163. var out = [];
  1164. var left = n, divmod;
  1165. while (left.isNegative() || left.compareAbs(base) >= 0) {
  1166. divmod = left.divmod(base);
  1167. left = divmod.quotient;
  1168. var digit = divmod.remainder;
  1169. if (digit.isNegative()) {
  1170. digit = base.minus(digit).abs();
  1171. left = left.next();
  1172. }
  1173. out.push(stringify(digit));
  1174. }
  1175. out.push(stringify(left));
  1176. return minusSign + out.reverse().join("");
  1177. }
  1178. BigInteger.prototype.toString = function (radix) {
  1179. if (radix === undefined) radix = 10;
  1180. if (radix !== 10) return toBase(this, radix);
  1181. var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
  1182. while (--l >= 0) {
  1183. digit = String(v[l]);
  1184. str += zeros.slice(digit.length) + digit;
  1185. }
  1186. var sign = this.sign ? "-" : "";
  1187. return sign + str;
  1188. };
  1189. SmallInteger.prototype.toString = function (radix) {
  1190. if (radix === undefined) radix = 10;
  1191. if (radix != 10) return toBase(this, radix);
  1192. return String(this.value);
  1193. };
  1194. BigInteger.prototype.valueOf = function () {
  1195. return +this.toString();
  1196. };
  1197. BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
  1198. SmallInteger.prototype.valueOf = function () {
  1199. return this.value;
  1200. };
  1201. SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
  1202. function parseStringValue(v) {
  1203. if (isPrecise(+v)) {
  1204. var x = +v;
  1205. if (x === truncate(x))
  1206. return new SmallInteger(x);
  1207. throw "Invalid integer: " + v;
  1208. }
  1209. var sign = v[0] === "-";
  1210. if (sign) v = v.slice(1);
  1211. var split = v.split(/e/i);
  1212. if (split.length > 2) throw new Error("Invalid integer: " + split.join("e"));
  1213. if (split.length === 2) {
  1214. var exp = split[1];
  1215. if (exp[0] === "+") exp = exp.slice(1);
  1216. exp = +exp;
  1217. if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
  1218. var text = split[0];
  1219. var decimalPlace = text.indexOf(".");
  1220. if (decimalPlace >= 0) {
  1221. exp -= text.length - decimalPlace - 1;
  1222. text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
  1223. }
  1224. if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
  1225. text += (new Array(exp + 1)).join("0");
  1226. v = text;
  1227. }
  1228. var isValid = /^([0-9][0-9]*)$/.test(v);
  1229. if (!isValid) throw new Error("Invalid integer: " + v);
  1230. var r = [], max = v.length, l = LOG_BASE, min = max - l;
  1231. while (max > 0) {
  1232. r.push(+v.slice(min, max));
  1233. min -= l;
  1234. if (min < 0) min = 0;
  1235. max -= l;
  1236. }
  1237. trim(r);
  1238. return new BigInteger(r, sign);
  1239. }
  1240. function parseNumberValue(v) {
  1241. if (isPrecise(v)) {
  1242. if (v !== truncate(v)) throw new Error(v + " is not an integer.");
  1243. return new SmallInteger(v);
  1244. }
  1245. return parseStringValue(v.toString());
  1246. }
  1247. function parseValue(v) {
  1248. if (typeof v === "number") {
  1249. return parseNumberValue(v);
  1250. }
  1251. if (typeof v === "string") {
  1252. return parseStringValue(v);
  1253. }
  1254. return v;
  1255. }
  1256. // Pre-define numbers in range [-999,999]
  1257. for (var i = 0; i < 1000; i++) {
  1258. Integer[i] = new SmallInteger(i);
  1259. if (i > 0) Integer[-i] = new SmallInteger(-i);
  1260. }
  1261. // Backwards compatibility
  1262. Integer.one = Integer[1];
  1263. Integer.zero = Integer[0];
  1264. Integer.minusOne = Integer[-1];
  1265. Integer.max = max;
  1266. Integer.min = min;
  1267. Integer.gcd = gcd;
  1268. Integer.lcm = lcm;
  1269. Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger; };
  1270. Integer.randBetween = randBetween;
  1271. return Integer;
  1272. })();
  1273. // Node.js check
  1274. if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
  1275. module.exports = bigInt;
  1276. }
  1277. },{}],4:[function(require,module,exports){
  1278. },{}],5:[function(require,module,exports){
  1279. (function (global){
  1280. 'use strict';
  1281. var buffer = require('buffer');
  1282. var Buffer = buffer.Buffer;
  1283. var SlowBuffer = buffer.SlowBuffer;
  1284. var MAX_LEN = buffer.kMaxLength || 2147483647;
  1285. exports.alloc = function alloc(size, fill, encoding) {
  1286. if (typeof Buffer.alloc === 'function') {
  1287. return Buffer.alloc(size, fill, encoding);
  1288. }
  1289. if (typeof encoding === 'number') {
  1290. throw new TypeError('encoding must not be number');
  1291. }
  1292. if (typeof size !== 'number') {
  1293. throw new TypeError('size must be a number');
  1294. }
  1295. if (size > MAX_LEN) {
  1296. throw new RangeError('size is too large');
  1297. }
  1298. var enc = encoding;
  1299. var _fill = fill;
  1300. if (_fill === undefined) {
  1301. enc = undefined;
  1302. _fill = 0;
  1303. }
  1304. var buf = new Buffer(size);
  1305. if (typeof _fill === 'string') {
  1306. var fillBuf = new Buffer(_fill, enc);
  1307. var flen = fillBuf.length;
  1308. var i = -1;
  1309. while (++i < size) {
  1310. buf[i] = fillBuf[i % flen];
  1311. }
  1312. } else {
  1313. buf.fill(_fill);
  1314. }
  1315. return buf;
  1316. }
  1317. exports.allocUnsafe = function allocUnsafe(size) {
  1318. if (typeof Buffer.allocUnsafe === 'function') {
  1319. return Buffer.allocUnsafe(size);
  1320. }
  1321. if (typeof size !== 'number') {
  1322. throw new TypeError('size must be a number');
  1323. }
  1324. if (size > MAX_LEN) {
  1325. throw new RangeError('size is too large');
  1326. }
  1327. return new Buffer(size);
  1328. }
  1329. exports.from = function from(value, encodingOrOffset, length) {
  1330. if (typeof Buffer.from === 'function' && (!global.Uint8Array || Uint8Array.from !== Buffer.from)) {
  1331. return Buffer.from(value, encodingOrOffset, length);
  1332. }
  1333. if (typeof value === 'number') {
  1334. throw new TypeError('"value" argument must not be a number');
  1335. }
  1336. if (typeof value === 'string') {
  1337. return new Buffer(value, encodingOrOffset);
  1338. }
  1339. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  1340. var offset = encodingOrOffset;
  1341. if (arguments.length === 1) {
  1342. return new Buffer(value);
  1343. }
  1344. if (typeof offset === 'undefined') {
  1345. offset = 0;
  1346. }
  1347. var len = length;
  1348. if (typeof len === 'undefined') {
  1349. len = value.byteLength - offset;
  1350. }
  1351. if (offset >= value.byteLength) {
  1352. throw new RangeError('\'offset\' is out of bounds');
  1353. }
  1354. if (len > value.byteLength - offset) {
  1355. throw new RangeError('\'length\' is out of bounds');
  1356. }
  1357. return new Buffer(value.slice(offset, offset + len));
  1358. }
  1359. if (Buffer.isBuffer(value)) {
  1360. var out = new Buffer(value.length);
  1361. value.copy(out, 0, 0, value.length);
  1362. return out;
  1363. }
  1364. if (value) {
  1365. if (Array.isArray(value) || (typeof ArrayBuffer !== 'undefined' && value.buffer instanceof ArrayBuffer) || 'length' in value) {
  1366. return new Buffer(value);
  1367. }
  1368. if (value.type === 'Buffer' && Array.isArray(value.data)) {
  1369. return new Buffer(value.data);
  1370. }
  1371. }
  1372. throw new TypeError('First argument must be a string, Buffer, ' + 'ArrayBuffer, Array, or array-like object.');
  1373. }
  1374. exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
  1375. if (typeof Buffer.allocUnsafeSlow === 'function') {
  1376. return Buffer.allocUnsafeSlow(size);
  1377. }
  1378. if (typeof size !== 'number') {
  1379. throw new TypeError('size must be a number');
  1380. }
  1381. if (size >= MAX_LEN) {
  1382. throw new RangeError('size is too large');
  1383. }
  1384. return new SlowBuffer(size);
  1385. }
  1386. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1387. },{"buffer":6}],6:[function(require,module,exports){
  1388. (function (global){
  1389. /*!
  1390. * The buffer module from node.js, for the browser.
  1391. *
  1392. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  1393. * @license MIT
  1394. */
  1395. /* eslint-disable no-proto */
  1396. 'use strict'
  1397. var base64 = require('base64-js')
  1398. var ieee754 = require('ieee754')
  1399. var isArray = require('isarray')
  1400. exports.Buffer = Buffer
  1401. exports.SlowBuffer = SlowBuffer
  1402. exports.INSPECT_MAX_BYTES = 50
  1403. /**
  1404. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  1405. * === true Use Uint8Array implementation (fastest)
  1406. * === false Use Object implementation (most compatible, even IE6)
  1407. *
  1408. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  1409. * Opera 11.6+, iOS 4.2+.
  1410. *
  1411. * Due to various browser bugs, sometimes the Object implementation will be used even
  1412. * when the browser supports typed arrays.
  1413. *
  1414. * Note:
  1415. *
  1416. * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
  1417. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  1418. *
  1419. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  1420. *
  1421. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  1422. * incorrect length in some situations.
  1423. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
  1424. * get the Object implementation, which is slower but behaves correctly.
  1425. */
  1426. Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  1427. ? global.TYPED_ARRAY_SUPPORT
  1428. : typedArraySupport()
  1429. /*
  1430. * Export kMaxLength after typed array support is determined.
  1431. */
  1432. exports.kMaxLength = kMaxLength()
  1433. function typedArraySupport () {
  1434. try {
  1435. var arr = new Uint8Array(1)
  1436. arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
  1437. return arr.foo() === 42 && // typed array instances can be augmented
  1438. typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
  1439. arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
  1440. } catch (e) {
  1441. return false
  1442. }
  1443. }
  1444. function kMaxLength () {
  1445. return Buffer.TYPED_ARRAY_SUPPORT
  1446. ? 0x7fffffff
  1447. : 0x3fffffff
  1448. }
  1449. function createBuffer (that, length) {
  1450. if (kMaxLength() < length) {
  1451. throw new RangeError('Invalid typed array length')
  1452. }
  1453. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1454. // Return an augmented `Uint8Array` instance, for best performance
  1455. that = new Uint8Array(length)
  1456. that.__proto__ = Buffer.prototype
  1457. } else {
  1458. // Fallback: Return an object instance of the Buffer class
  1459. if (that === null) {
  1460. that = new Buffer(length)
  1461. }
  1462. that.length = length
  1463. }
  1464. return that
  1465. }
  1466. /**
  1467. * The Buffer constructor returns instances of `Uint8Array` that have their
  1468. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  1469. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  1470. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  1471. * returns a single octet.
  1472. *
  1473. * The `Uint8Array` prototype remains unmodified.
  1474. */
  1475. function Buffer (arg, encodingOrOffset, length) {
  1476. if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
  1477. return new Buffer(arg, encodingOrOffset, length)
  1478. }
  1479. // Common case.
  1480. if (typeof arg === 'number') {
  1481. if (typeof encodingOrOffset === 'string') {
  1482. throw new Error(
  1483. 'If encoding is specified then the first argument must be a string'
  1484. )
  1485. }
  1486. return allocUnsafe(this, arg)
  1487. }
  1488. return from(this, arg, encodingOrOffset, length)
  1489. }
  1490. Buffer.poolSize = 8192 // not used by this implementation
  1491. // TODO: Legacy, not needed anymore. Remove in next major version.
  1492. Buffer._augment = function (arr) {
  1493. arr.__proto__ = Buffer.prototype
  1494. return arr
  1495. }
  1496. function from (that, value, encodingOrOffset, length) {
  1497. if (typeof value === 'number') {
  1498. throw new TypeError('"value" argument must not be a number')
  1499. }
  1500. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  1501. return fromArrayBuffer(that, value, encodingOrOffset, length)
  1502. }
  1503. if (typeof value === 'string') {
  1504. return fromString(that, value, encodingOrOffset)
  1505. }
  1506. return fromObject(that, value)
  1507. }
  1508. /**
  1509. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  1510. * if value is a number.
  1511. * Buffer.from(str[, encoding])
  1512. * Buffer.from(array)
  1513. * Buffer.from(buffer)
  1514. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  1515. **/
  1516. Buffer.from = function (value, encodingOrOffset, length) {
  1517. return from(null, value, encodingOrOffset, length)
  1518. }
  1519. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1520. Buffer.prototype.__proto__ = Uint8Array.prototype
  1521. Buffer.__proto__ = Uint8Array
  1522. if (typeof Symbol !== 'undefined' && Symbol.species &&
  1523. Buffer[Symbol.species] === Buffer) {
  1524. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  1525. Object.defineProperty(Buffer, Symbol.species, {
  1526. value: null,
  1527. configurable: true
  1528. })
  1529. }
  1530. }
  1531. function assertSize (size) {
  1532. if (typeof size !== 'number') {
  1533. throw new TypeError('"size" argument must be a number')
  1534. } else if (size < 0) {
  1535. throw new RangeError('"size" argument must not be negative')
  1536. }
  1537. }
  1538. function alloc (that, size, fill, encoding) {
  1539. assertSize(size)
  1540. if (size <= 0) {
  1541. return createBuffer(that, size)
  1542. }
  1543. if (fill !== undefined) {
  1544. // Only pay attention to encoding if it's a string. This
  1545. // prevents accidentally sending in a number that would
  1546. // be interpretted as a start offset.
  1547. return typeof encoding === 'string'
  1548. ? createBuffer(that, size).fill(fill, encoding)
  1549. : createBuffer(that, size).fill(fill)
  1550. }
  1551. return createBuffer(that, size)
  1552. }
  1553. /**
  1554. * Creates a new filled Buffer instance.
  1555. * alloc(size[, fill[, encoding]])
  1556. **/
  1557. Buffer.alloc = function (size, fill, encoding) {
  1558. return alloc(null, size, fill, encoding)
  1559. }
  1560. function allocUnsafe (that, size) {
  1561. assertSize(size)
  1562. that = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
  1563. if (!Buffer.TYPED_ARRAY_SUPPORT) {
  1564. for (var i = 0; i < size; ++i) {
  1565. that[i] = 0
  1566. }
  1567. }
  1568. return that
  1569. }
  1570. /**
  1571. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  1572. * */
  1573. Buffer.allocUnsafe = function (size) {
  1574. return allocUnsafe(null, size)
  1575. }
  1576. /**
  1577. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  1578. */
  1579. Buffer.allocUnsafeSlow = function (size) {
  1580. return allocUnsafe(null, size)
  1581. }
  1582. function fromString (that, string, encoding) {
  1583. if (typeof encoding !== 'string' || encoding === '') {
  1584. encoding = 'utf8'
  1585. }
  1586. if (!Buffer.isEncoding(encoding)) {
  1587. throw new TypeError('"encoding" must be a valid string encoding')
  1588. }
  1589. var length = byteLength(string, encoding) | 0
  1590. that = createBuffer(that, length)
  1591. var actual = that.write(string, encoding)
  1592. if (actual !== length) {
  1593. // Writing a hex string, for example, that contains invalid characters will
  1594. // cause everything after the first invalid character to be ignored. (e.g.
  1595. // 'abxxcd' will be treated as 'ab')
  1596. that = that.slice(0, actual)
  1597. }
  1598. return that
  1599. }
  1600. function fromArrayLike (that, array) {
  1601. var length = array.length < 0 ? 0 : checked(array.length) | 0
  1602. that = createBuffer(that, length)
  1603. for (var i = 0; i < length; i += 1) {
  1604. that[i] = array[i] & 255
  1605. }
  1606. return that
  1607. }
  1608. function fromArrayBuffer (that, array, byteOffset, length) {
  1609. array.byteLength // this throws if `array` is not a valid ArrayBuffer
  1610. if (byteOffset < 0 || array.byteLength < byteOffset) {
  1611. throw new RangeError('\'offset\' is out of bounds')
  1612. }
  1613. if (array.byteLength < byteOffset + (length || 0)) {
  1614. throw new RangeError('\'length\' is out of bounds')
  1615. }
  1616. if (byteOffset === undefined && length === undefined) {
  1617. array = new Uint8Array(array)
  1618. } else if (length === undefined) {
  1619. array = new Uint8Array(array, byteOffset)
  1620. } else {
  1621. array = new Uint8Array(array, byteOffset, length)
  1622. }
  1623. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1624. // Return an augmented `Uint8Array` instance, for best performance
  1625. that = array
  1626. that.__proto__ = Buffer.prototype
  1627. } else {
  1628. // Fallback: Return an object instance of the Buffer class
  1629. that = fromArrayLike(that, array)
  1630. }
  1631. return that
  1632. }
  1633. function fromObject (that, obj) {
  1634. if (Buffer.isBuffer(obj)) {
  1635. var len = checked(obj.length) | 0
  1636. that = createBuffer(that, len)
  1637. if (that.length === 0) {
  1638. return that
  1639. }
  1640. obj.copy(that, 0, 0, len)
  1641. return that
  1642. }
  1643. if (obj) {
  1644. if ((typeof ArrayBuffer !== 'undefined' &&
  1645. obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
  1646. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  1647. return createBuffer(that, 0)
  1648. }
  1649. return fromArrayLike(that, obj)
  1650. }
  1651. if (obj.type === 'Buffer' && isArray(obj.data)) {
  1652. return fromArrayLike(that, obj.data)
  1653. }
  1654. }
  1655. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  1656. }
  1657. function checked (length) {
  1658. // Note: cannot use `length < kMaxLength()` here because that fails when
  1659. // length is NaN (which is otherwise coerced to zero.)
  1660. if (length >= kMaxLength()) {
  1661. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  1662. 'size: 0x' + kMaxLength().toString(16) + ' bytes')
  1663. }
  1664. return length | 0
  1665. }
  1666. function SlowBuffer (length) {
  1667. if (+length != length) { // eslint-disable-line eqeqeq
  1668. length = 0
  1669. }
  1670. return Buffer.alloc(+length)
  1671. }
  1672. Buffer.isBuffer = function isBuffer (b) {
  1673. return !!(b != null && b._isBuffer)
  1674. }
  1675. Buffer.compare = function compare (a, b) {
  1676. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  1677. throw new TypeError('Arguments must be Buffers')
  1678. }
  1679. if (a === b) return 0
  1680. var x = a.length
  1681. var y = b.length
  1682. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  1683. if (a[i] !== b[i]) {
  1684. x = a[i]
  1685. y = b[i]
  1686. break
  1687. }
  1688. }
  1689. if (x < y) return -1
  1690. if (y < x) return 1
  1691. return 0
  1692. }
  1693. Buffer.isEncoding = function isEncoding (encoding) {
  1694. switch (String(encoding).toLowerCase()) {
  1695. case 'hex':
  1696. case 'utf8':
  1697. case 'utf-8':
  1698. case 'ascii':
  1699. case 'latin1':
  1700. case 'binary':
  1701. case 'base64':
  1702. case 'ucs2':
  1703. case 'ucs-2':
  1704. case 'utf16le':
  1705. case 'utf-16le':
  1706. return true
  1707. default:
  1708. return false
  1709. }
  1710. }
  1711. Buffer.concat = function concat (list, length) {
  1712. if (!isArray(list)) {
  1713. throw new TypeError('"list" argument must be an Array of Buffers')
  1714. }
  1715. if (list.length === 0) {
  1716. return Buffer.alloc(0)
  1717. }
  1718. var i
  1719. if (length === undefined) {
  1720. length = 0
  1721. for (i = 0; i < list.length; ++i) {
  1722. length += list[i].length
  1723. }
  1724. }
  1725. var buffer = Buffer.allocUnsafe(length)
  1726. var pos = 0
  1727. for (i = 0; i < list.length; ++i) {
  1728. var buf = list[i]
  1729. if (!Buffer.isBuffer(buf)) {
  1730. throw new TypeError('"list" argument must be an Array of Buffers')
  1731. }
  1732. buf.copy(buffer, pos)
  1733. pos += buf.length
  1734. }
  1735. return buffer
  1736. }
  1737. function byteLength (string, encoding) {
  1738. if (Buffer.isBuffer(string)) {
  1739. return string.length
  1740. }
  1741. if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
  1742. (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
  1743. return string.byteLength
  1744. }
  1745. if (typeof string !== 'string') {
  1746. string = '' + string
  1747. }
  1748. var len = string.length
  1749. if (len === 0) return 0
  1750. // Use a for loop to avoid recursion
  1751. var loweredCase = false
  1752. for (;;) {
  1753. switch (encoding) {
  1754. case 'ascii':
  1755. case 'latin1':
  1756. case 'binary':
  1757. return len
  1758. case 'utf8':
  1759. case 'utf-8':
  1760. case undefined:
  1761. return utf8ToBytes(string).length
  1762. case 'ucs2':
  1763. case 'ucs-2':
  1764. case 'utf16le':
  1765. case 'utf-16le':
  1766. return len * 2
  1767. case 'hex':
  1768. return len >>> 1
  1769. case 'base64':
  1770. return base64ToBytes(string).length
  1771. default:
  1772. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  1773. encoding = ('' + encoding).toLowerCase()
  1774. loweredCase = true
  1775. }
  1776. }
  1777. }
  1778. Buffer.byteLength = byteLength
  1779. function slowToString (encoding, start, end) {
  1780. var loweredCase = false
  1781. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  1782. // property of a typed array.
  1783. // This behaves neither like String nor Uint8Array in that we set start/end
  1784. // to their upper/lower bounds if the value passed is out of range.
  1785. // undefined is handled specially as per ECMA-262 6th Edition,
  1786. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  1787. if (start === undefined || start < 0) {
  1788. start = 0
  1789. }
  1790. // Return early if start > this.length. Done here to prevent potential uint32
  1791. // coercion fail below.
  1792. if (start > this.length) {
  1793. return ''
  1794. }
  1795. if (end === undefined || end > this.length) {
  1796. end = this.length
  1797. }
  1798. if (end <= 0) {
  1799. return ''
  1800. }
  1801. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  1802. end >>>= 0
  1803. start >>>= 0
  1804. if (end <= start) {
  1805. return ''
  1806. }
  1807. if (!encoding) encoding = 'utf8'
  1808. while (true) {
  1809. switch (encoding) {
  1810. case 'hex':
  1811. return hexSlice(this, start, end)
  1812. case 'utf8':
  1813. case 'utf-8':
  1814. return utf8Slice(this, start, end)
  1815. case 'ascii':
  1816. return asciiSlice(this, start, end)
  1817. case 'latin1':
  1818. case 'binary':
  1819. return latin1Slice(this, start, end)
  1820. case 'base64':
  1821. return base64Slice(this, start, end)
  1822. case 'ucs2':
  1823. case 'ucs-2':
  1824. case 'utf16le':
  1825. case 'utf-16le':
  1826. return utf16leSlice(this, start, end)
  1827. default:
  1828. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  1829. encoding = (encoding + '').toLowerCase()
  1830. loweredCase = true
  1831. }
  1832. }
  1833. }
  1834. // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
  1835. // Buffer instances.
  1836. Buffer.prototype._isBuffer = true
  1837. function swap (b, n, m) {
  1838. var i = b[n]
  1839. b[n] = b[m]
  1840. b[m] = i
  1841. }
  1842. Buffer.prototype.swap16 = function swap16 () {
  1843. var len = this.length
  1844. if (len % 2 !== 0) {
  1845. throw new RangeError('Buffer size must be a multiple of 16-bits')
  1846. }
  1847. for (var i = 0; i < len; i += 2) {
  1848. swap(this, i, i + 1)
  1849. }
  1850. return this
  1851. }
  1852. Buffer.prototype.swap32 = function swap32 () {
  1853. var len = this.length
  1854. if (len % 4 !== 0) {
  1855. throw new RangeError('Buffer size must be a multiple of 32-bits')
  1856. }
  1857. for (var i = 0; i < len; i += 4) {
  1858. swap(this, i, i + 3)
  1859. swap(this, i + 1, i + 2)
  1860. }
  1861. return this
  1862. }
  1863. Buffer.prototype.swap64 = function swap64 () {
  1864. var len = this.length
  1865. if (len % 8 !== 0) {
  1866. throw new RangeError('Buffer size must be a multiple of 64-bits')
  1867. }
  1868. for (var i = 0; i < len; i += 8) {
  1869. swap(this, i, i + 7)
  1870. swap(this, i + 1, i + 6)
  1871. swap(this, i + 2, i + 5)
  1872. swap(this, i + 3, i + 4)
  1873. }
  1874. return this
  1875. }
  1876. Buffer.prototype.toString = function toString () {
  1877. var length = this.length | 0
  1878. if (length === 0) return ''
  1879. if (arguments.length === 0) return utf8Slice(this, 0, length)
  1880. return slowToString.apply(this, arguments)
  1881. }
  1882. Buffer.prototype.equals = function equals (b) {
  1883. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  1884. if (this === b) return true
  1885. return Buffer.compare(this, b) === 0
  1886. }
  1887. Buffer.prototype.inspect = function inspect () {
  1888. var str = ''
  1889. var max = exports.INSPECT_MAX_BYTES
  1890. if (this.length > 0) {
  1891. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
  1892. if (this.length > max) str += ' ... '
  1893. }
  1894. return '<Buffer ' + str + '>'
  1895. }
  1896. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  1897. if (!Buffer.isBuffer(target)) {
  1898. throw new TypeError('Argument must be a Buffer')
  1899. }
  1900. if (start === undefined) {
  1901. start = 0
  1902. }
  1903. if (end === undefined) {
  1904. end = target ? target.length : 0
  1905. }
  1906. if (thisStart === undefined) {
  1907. thisStart = 0
  1908. }
  1909. if (thisEnd === undefined) {
  1910. thisEnd = this.length
  1911. }
  1912. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  1913. throw new RangeError('out of range index')
  1914. }
  1915. if (thisStart >= thisEnd && start >= end) {
  1916. return 0
  1917. }
  1918. if (thisStart >= thisEnd) {
  1919. return -1
  1920. }
  1921. if (start >= end) {
  1922. return 1
  1923. }
  1924. start >>>= 0
  1925. end >>>= 0
  1926. thisStart >>>= 0
  1927. thisEnd >>>= 0
  1928. if (this === target) return 0
  1929. var x = thisEnd - thisStart
  1930. var y = end - start
  1931. var len = Math.min(x, y)
  1932. var thisCopy = this.slice(thisStart, thisEnd)
  1933. var targetCopy = target.slice(start, end)
  1934. for (var i = 0; i < len; ++i) {
  1935. if (thisCopy[i] !== targetCopy[i]) {
  1936. x = thisCopy[i]
  1937. y = targetCopy[i]
  1938. break
  1939. }
  1940. }
  1941. if (x < y) return -1
  1942. if (y < x) return 1
  1943. return 0
  1944. }
  1945. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  1946. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  1947. //
  1948. // Arguments:
  1949. // - buffer - a Buffer to search
  1950. // - val - a string, Buffer, or number
  1951. // - byteOffset - an index into `buffer`; will be clamped to an int32
  1952. // - encoding - an optional encoding, relevant is val is a string
  1953. // - dir - true for indexOf, false for lastIndexOf
  1954. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  1955. // Empty buffer means no match
  1956. if (buffer.length === 0) return -1
  1957. // Normalize byteOffset
  1958. if (typeof byteOffset === 'string') {
  1959. encoding = byteOffset
  1960. byteOffset = 0
  1961. } else if (byteOffset > 0x7fffffff) {
  1962. byteOffset = 0x7fffffff
  1963. } else if (byteOffset < -0x80000000) {
  1964. byteOffset = -0x80000000
  1965. }
  1966. byteOffset = +byteOffset // Coerce to Number.
  1967. if (isNaN(byteOffset)) {
  1968. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  1969. byteOffset = dir ? 0 : (buffer.length - 1)
  1970. }
  1971. // Normalize byteOffset: negative offsets start from the end of the buffer
  1972. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  1973. if (byteOffset >= buffer.length) {
  1974. if (dir) return -1
  1975. else byteOffset = buffer.length - 1
  1976. } else if (byteOffset < 0) {
  1977. if (dir) byteOffset = 0
  1978. else return -1
  1979. }
  1980. // Normalize val
  1981. if (typeof val === 'string') {
  1982. val = Buffer.from(val, encoding)
  1983. }
  1984. // Finally, search either indexOf (if dir is true) or lastIndexOf
  1985. if (Buffer.isBuffer(val)) {
  1986. // Special case: looking for empty string/buffer always fails
  1987. if (val.length === 0) {
  1988. return -1
  1989. }
  1990. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  1991. } else if (typeof val === 'number') {
  1992. val = val & 0xFF // Search for a byte value [0-255]
  1993. if (Buffer.TYPED_ARRAY_SUPPORT &&
  1994. typeof Uint8Array.prototype.indexOf === 'function') {
  1995. if (dir) {
  1996. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  1997. } else {
  1998. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  1999. }
  2000. }
  2001. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  2002. }
  2003. throw new TypeError('val must be string, number or Buffer')
  2004. }
  2005. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  2006. var indexSize = 1
  2007. var arrLength = arr.length
  2008. var valLength = val.length
  2009. if (encoding !== undefined) {
  2010. encoding = String(encoding).toLowerCase()
  2011. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  2012. encoding === 'utf16le' || encoding === 'utf-16le') {
  2013. if (arr.length < 2 || val.length < 2) {
  2014. return -1
  2015. }
  2016. indexSize = 2
  2017. arrLength /= 2
  2018. valLength /= 2
  2019. byteOffset /= 2
  2020. }
  2021. }
  2022. function read (buf, i) {
  2023. if (indexSize === 1) {
  2024. return buf[i]
  2025. } else {
  2026. return buf.readUInt16BE(i * indexSize)
  2027. }
  2028. }
  2029. var i
  2030. if (dir) {
  2031. var foundIndex = -1
  2032. for (i = byteOffset; i < arrLength; i++) {
  2033. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  2034. if (foundIndex === -1) foundIndex = i
  2035. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  2036. } else {
  2037. if (foundIndex !== -1) i -= i - foundIndex
  2038. foundIndex = -1
  2039. }
  2040. }
  2041. } else {
  2042. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  2043. for (i = byteOffset; i >= 0; i--) {
  2044. var found = true
  2045. for (var j = 0; j < valLength; j++) {
  2046. if (read(arr, i + j) !== read(val, j)) {
  2047. found = false
  2048. break
  2049. }
  2050. }
  2051. if (found) return i
  2052. }
  2053. }
  2054. return -1
  2055. }
  2056. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  2057. return this.indexOf(val, byteOffset, encoding) !== -1
  2058. }
  2059. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  2060. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  2061. }
  2062. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  2063. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  2064. }
  2065. function hexWrite (buf, string, offset, length) {
  2066. offset = Number(offset) || 0
  2067. var remaining = buf.length - offset
  2068. if (!length) {
  2069. length = remaining
  2070. } else {
  2071. length = Number(length)
  2072. if (length > remaining) {
  2073. length = remaining
  2074. }
  2075. }
  2076. // must be an even number of digits
  2077. var strLen = string.length
  2078. if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
  2079. if (length > strLen / 2) {
  2080. length = strLen / 2
  2081. }
  2082. for (var i = 0; i < length; ++i) {
  2083. var parsed = parseInt(string.substr(i * 2, 2), 16)
  2084. if (isNaN(parsed)) return i
  2085. buf[offset + i] = parsed
  2086. }
  2087. return i
  2088. }
  2089. function utf8Write (buf, string, offset, length) {
  2090. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  2091. }
  2092. function asciiWrite (buf, string, offset, length) {
  2093. return blitBuffer(asciiToBytes(string), buf, offset, length)
  2094. }
  2095. function latin1Write (buf, string, offset, length) {
  2096. return asciiWrite(buf, string, offset, length)
  2097. }
  2098. function base64Write (buf, string, offset, length) {
  2099. return blitBuffer(base64ToBytes(string), buf, offset, length)
  2100. }
  2101. function ucs2Write (buf, string, offset, length) {
  2102. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  2103. }
  2104. Buffer.prototype.write = function write (string, offset, length, encoding) {
  2105. // Buffer#write(string)
  2106. if (offset === undefined) {
  2107. encoding = 'utf8'
  2108. length = this.length
  2109. offset = 0
  2110. // Buffer#write(string, encoding)
  2111. } else if (length === undefined && typeof offset === 'string') {
  2112. encoding = offset
  2113. length = this.length
  2114. offset = 0
  2115. // Buffer#write(string, offset[, length][, encoding])
  2116. } else if (isFinite(offset)) {
  2117. offset = offset | 0
  2118. if (isFinite(length)) {
  2119. length = length | 0
  2120. if (encoding === undefined) encoding = 'utf8'
  2121. } else {
  2122. encoding = length
  2123. length = undefined
  2124. }
  2125. // legacy write(string, encoding, offset, length) - remove in v0.13
  2126. } else {
  2127. throw new Error(
  2128. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  2129. )
  2130. }
  2131. var remaining = this.length - offset
  2132. if (length === undefined || length > remaining) length = remaining
  2133. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  2134. throw new RangeError('Attempt to write outside buffer bounds')
  2135. }
  2136. if (!encoding) encoding = 'utf8'
  2137. var loweredCase = false
  2138. for (;;) {
  2139. switch (encoding) {
  2140. case 'hex':
  2141. return hexWrite(this, string, offset, length)
  2142. case 'utf8':
  2143. case 'utf-8':
  2144. return utf8Write(this, string, offset, length)
  2145. case 'ascii':
  2146. return asciiWrite(this, string, offset, length)
  2147. case 'latin1':
  2148. case 'binary':
  2149. return latin1Write(this, string, offset, length)
  2150. case 'base64':
  2151. // Warning: maxLength not taken into account in base64Write
  2152. return base64Write(this, string, offset, length)
  2153. case 'ucs2':
  2154. case 'ucs-2':
  2155. case 'utf16le':
  2156. case 'utf-16le':
  2157. return ucs2Write(this, string, offset, length)
  2158. default:
  2159. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  2160. encoding = ('' + encoding).toLowerCase()
  2161. loweredCase = true
  2162. }
  2163. }
  2164. }
  2165. Buffer.prototype.toJSON = function toJSON () {
  2166. return {
  2167. type: 'Buffer',
  2168. data: Array.prototype.slice.call(this._arr || this, 0)
  2169. }
  2170. }
  2171. function base64Slice (buf, start, end) {
  2172. if (start === 0 && end === buf.length) {
  2173. return base64.fromByteArray(buf)
  2174. } else {
  2175. return base64.fromByteArray(buf.slice(start, end))
  2176. }
  2177. }
  2178. function utf8Slice (buf, start, end) {
  2179. end = Math.min(buf.length, end)
  2180. var res = []
  2181. var i = start
  2182. while (i < end) {
  2183. var firstByte = buf[i]
  2184. var codePoint = null
  2185. var bytesPerSequence = (firstByte > 0xEF) ? 4
  2186. : (firstByte > 0xDF) ? 3
  2187. : (firstByte > 0xBF) ? 2
  2188. : 1
  2189. if (i + bytesPerSequence <= end) {
  2190. var secondByte, thirdByte, fourthByte, tempCodePoint
  2191. switch (bytesPerSequence) {
  2192. case 1:
  2193. if (firstByte < 0x80) {
  2194. codePoint = firstByte
  2195. }
  2196. break
  2197. case 2:
  2198. secondByte = buf[i + 1]
  2199. if ((secondByte & 0xC0) === 0x80) {
  2200. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  2201. if (tempCodePoint > 0x7F) {
  2202. codePoint = tempCodePoint
  2203. }
  2204. }
  2205. break
  2206. case 3:
  2207. secondByte = buf[i + 1]
  2208. thirdByte = buf[i + 2]
  2209. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  2210. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  2211. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  2212. codePoint = tempCodePoint
  2213. }
  2214. }
  2215. break
  2216. case 4:
  2217. secondByte = buf[i + 1]
  2218. thirdByte = buf[i + 2]
  2219. fourthByte = buf[i + 3]
  2220. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  2221. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  2222. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  2223. codePoint = tempCodePoint
  2224. }
  2225. }
  2226. }
  2227. }
  2228. if (codePoint === null) {
  2229. // we did not generate a valid codePoint so insert a
  2230. // replacement char (U+FFFD) and advance only 1 byte
  2231. codePoint = 0xFFFD
  2232. bytesPerSequence = 1
  2233. } else if (codePoint > 0xFFFF) {
  2234. // encode to utf16 (surrogate pair dance)
  2235. codePoint -= 0x10000
  2236. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  2237. codePoint = 0xDC00 | codePoint & 0x3FF
  2238. }
  2239. res.push(codePoint)
  2240. i += bytesPerSequence
  2241. }
  2242. return decodeCodePointsArray(res)
  2243. }
  2244. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  2245. // the lowest limit is Chrome, with 0x10000 args.
  2246. // We go 1 magnitude less, for safety
  2247. var MAX_ARGUMENTS_LENGTH = 0x1000
  2248. function decodeCodePointsArray (codePoints) {
  2249. var len = codePoints.length
  2250. if (len <= MAX_ARGUMENTS_LENGTH) {
  2251. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  2252. }
  2253. // Decode in chunks to avoid "call stack size exceeded".
  2254. var res = ''
  2255. var i = 0
  2256. while (i < len) {
  2257. res += String.fromCharCode.apply(
  2258. String,
  2259. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  2260. )
  2261. }
  2262. return res
  2263. }
  2264. function asciiSlice (buf, start, end) {
  2265. var ret = ''
  2266. end = Math.min(buf.length, end)
  2267. for (var i = start; i < end; ++i) {
  2268. ret += String.fromCharCode(buf[i] & 0x7F)
  2269. }
  2270. return ret
  2271. }
  2272. function latin1Slice (buf, start, end) {
  2273. var ret = ''
  2274. end = Math.min(buf.length, end)
  2275. for (var i = start; i < end; ++i) {
  2276. ret += String.fromCharCode(buf[i])
  2277. }
  2278. return ret
  2279. }
  2280. function hexSlice (buf, start, end) {
  2281. var len = buf.length
  2282. if (!start || start < 0) start = 0
  2283. if (!end || end < 0 || end > len) end = len
  2284. var out = ''
  2285. for (var i = start; i < end; ++i) {
  2286. out += toHex(buf[i])
  2287. }
  2288. return out
  2289. }
  2290. function utf16leSlice (buf, start, end) {
  2291. var bytes = buf.slice(start, end)
  2292. var res = ''
  2293. for (var i = 0; i < bytes.length; i += 2) {
  2294. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
  2295. }
  2296. return res
  2297. }
  2298. Buffer.prototype.slice = function slice (start, end) {
  2299. var len = this.length
  2300. start = ~~start
  2301. end = end === undefined ? len : ~~end
  2302. if (start < 0) {
  2303. start += len
  2304. if (start < 0) start = 0
  2305. } else if (start > len) {
  2306. start = len
  2307. }
  2308. if (end < 0) {
  2309. end += len
  2310. if (end < 0) end = 0
  2311. } else if (end > len) {
  2312. end = len
  2313. }
  2314. if (end < start) end = start
  2315. var newBuf
  2316. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2317. newBuf = this.subarray(start, end)
  2318. newBuf.__proto__ = Buffer.prototype
  2319. } else {
  2320. var sliceLen = end - start
  2321. newBuf = new Buffer(sliceLen, undefined)
  2322. for (var i = 0; i < sliceLen; ++i) {
  2323. newBuf[i] = this[i + start]
  2324. }
  2325. }
  2326. return newBuf
  2327. }
  2328. /*
  2329. * Need to make sure that buffer isn't trying to write out of bounds.
  2330. */
  2331. function checkOffset (offset, ext, length) {
  2332. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  2333. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  2334. }
  2335. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  2336. offset = offset | 0
  2337. byteLength = byteLength | 0
  2338. if (!noAssert) checkOffset(offset, byteLength, this.length)
  2339. var val = this[offset]
  2340. var mul = 1
  2341. var i = 0
  2342. while (++i < byteLength && (mul *= 0x100)) {
  2343. val += this[offset + i] * mul
  2344. }
  2345. return val
  2346. }
  2347. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  2348. offset = offset | 0
  2349. byteLength = byteLength | 0
  2350. if (!noAssert) {
  2351. checkOffset(offset, byteLength, this.length)
  2352. }
  2353. var val = this[offset + --byteLength]
  2354. var mul = 1
  2355. while (byteLength > 0 && (mul *= 0x100)) {
  2356. val += this[offset + --byteLength] * mul
  2357. }
  2358. return val
  2359. }
  2360. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  2361. if (!noAssert) checkOffset(offset, 1, this.length)
  2362. return this[offset]
  2363. }
  2364. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  2365. if (!noAssert) checkOffset(offset, 2, this.length)
  2366. return this[offset] | (this[offset + 1] << 8)
  2367. }
  2368. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  2369. if (!noAssert) checkOffset(offset, 2, this.length)
  2370. return (this[offset] << 8) | this[offset + 1]
  2371. }
  2372. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  2373. if (!noAssert) checkOffset(offset, 4, this.length)
  2374. return ((this[offset]) |
  2375. (this[offset + 1] << 8) |
  2376. (this[offset + 2] << 16)) +
  2377. (this[offset + 3] * 0x1000000)
  2378. }
  2379. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  2380. if (!noAssert) checkOffset(offset, 4, this.length)
  2381. return (this[offset] * 0x1000000) +
  2382. ((this[offset + 1] << 16) |
  2383. (this[offset + 2] << 8) |
  2384. this[offset + 3])
  2385. }
  2386. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  2387. offset = offset | 0
  2388. byteLength = byteLength | 0
  2389. if (!noAssert) checkOffset(offset, byteLength, this.length)
  2390. var val = this[offset]
  2391. var mul = 1
  2392. var i = 0
  2393. while (++i < byteLength && (mul *= 0x100)) {
  2394. val += this[offset + i] * mul
  2395. }
  2396. mul *= 0x80
  2397. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  2398. return val
  2399. }
  2400. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  2401. offset = offset | 0
  2402. byteLength = byteLength | 0
  2403. if (!noAssert) checkOffset(offset, byteLength, this.length)
  2404. var i = byteLength
  2405. var mul = 1
  2406. var val = this[offset + --i]
  2407. while (i > 0 && (mul *= 0x100)) {
  2408. val += this[offset + --i] * mul
  2409. }
  2410. mul *= 0x80
  2411. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  2412. return val
  2413. }
  2414. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  2415. if (!noAssert) checkOffset(offset, 1, this.length)
  2416. if (!(this[offset] & 0x80)) return (this[offset])
  2417. return ((0xff - this[offset] + 1) * -1)
  2418. }
  2419. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  2420. if (!noAssert) checkOffset(offset, 2, this.length)
  2421. var val = this[offset] | (this[offset + 1] << 8)
  2422. return (val & 0x8000) ? val | 0xFFFF0000 : val
  2423. }
  2424. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  2425. if (!noAssert) checkOffset(offset, 2, this.length)
  2426. var val = this[offset + 1] | (this[offset] << 8)
  2427. return (val & 0x8000) ? val | 0xFFFF0000 : val
  2428. }
  2429. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  2430. if (!noAssert) checkOffset(offset, 4, this.length)
  2431. return (this[offset]) |
  2432. (this[offset + 1] << 8) |
  2433. (this[offset + 2] << 16) |
  2434. (this[offset + 3] << 24)
  2435. }
  2436. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  2437. if (!noAssert) checkOffset(offset, 4, this.length)
  2438. return (this[offset] << 24) |
  2439. (this[offset + 1] << 16) |
  2440. (this[offset + 2] << 8) |
  2441. (this[offset + 3])
  2442. }
  2443. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  2444. if (!noAssert) checkOffset(offset, 4, this.length)
  2445. return ieee754.read(this, offset, true, 23, 4)
  2446. }
  2447. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  2448. if (!noAssert) checkOffset(offset, 4, this.length)
  2449. return ieee754.read(this, offset, false, 23, 4)
  2450. }
  2451. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  2452. if (!noAssert) checkOffset(offset, 8, this.length)
  2453. return ieee754.read(this, offset, true, 52, 8)
  2454. }
  2455. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  2456. if (!noAssert) checkOffset(offset, 8, this.length)
  2457. return ieee754.read(this, offset, false, 52, 8)
  2458. }
  2459. function checkInt (buf, value, offset, ext, max, min) {
  2460. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  2461. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  2462. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  2463. }
  2464. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  2465. value = +value
  2466. offset = offset | 0
  2467. byteLength = byteLength | 0
  2468. if (!noAssert) {
  2469. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  2470. checkInt(this, value, offset, byteLength, maxBytes, 0)
  2471. }
  2472. var mul = 1
  2473. var i = 0
  2474. this[offset] = value & 0xFF
  2475. while (++i < byteLength && (mul *= 0x100)) {
  2476. this[offset + i] = (value / mul) & 0xFF
  2477. }
  2478. return offset + byteLength
  2479. }
  2480. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  2481. value = +value
  2482. offset = offset | 0
  2483. byteLength = byteLength | 0
  2484. if (!noAssert) {
  2485. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  2486. checkInt(this, value, offset, byteLength, maxBytes, 0)
  2487. }
  2488. var i = byteLength - 1
  2489. var mul = 1
  2490. this[offset + i] = value & 0xFF
  2491. while (--i >= 0 && (mul *= 0x100)) {
  2492. this[offset + i] = (value / mul) & 0xFF
  2493. }
  2494. return offset + byteLength
  2495. }
  2496. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  2497. value = +value
  2498. offset = offset | 0
  2499. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  2500. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  2501. this[offset] = (value & 0xff)
  2502. return offset + 1
  2503. }
  2504. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  2505. if (value < 0) value = 0xffff + value + 1
  2506. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
  2507. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  2508. (littleEndian ? i : 1 - i) * 8
  2509. }
  2510. }
  2511. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  2512. value = +value
  2513. offset = offset | 0
  2514. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  2515. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2516. this[offset] = (value & 0xff)
  2517. this[offset + 1] = (value >>> 8)
  2518. } else {
  2519. objectWriteUInt16(this, value, offset, true)
  2520. }
  2521. return offset + 2
  2522. }
  2523. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  2524. value = +value
  2525. offset = offset | 0
  2526. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  2527. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2528. this[offset] = (value >>> 8)
  2529. this[offset + 1] = (value & 0xff)
  2530. } else {
  2531. objectWriteUInt16(this, value, offset, false)
  2532. }
  2533. return offset + 2
  2534. }
  2535. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  2536. if (value < 0) value = 0xffffffff + value + 1
  2537. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
  2538. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
  2539. }
  2540. }
  2541. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  2542. value = +value
  2543. offset = offset | 0
  2544. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  2545. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2546. this[offset + 3] = (value >>> 24)
  2547. this[offset + 2] = (value >>> 16)
  2548. this[offset + 1] = (value >>> 8)
  2549. this[offset] = (value & 0xff)
  2550. } else {
  2551. objectWriteUInt32(this, value, offset, true)
  2552. }
  2553. return offset + 4
  2554. }
  2555. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  2556. value = +value
  2557. offset = offset | 0
  2558. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  2559. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2560. this[offset] = (value >>> 24)
  2561. this[offset + 1] = (value >>> 16)
  2562. this[offset + 2] = (value >>> 8)
  2563. this[offset + 3] = (value & 0xff)
  2564. } else {
  2565. objectWriteUInt32(this, value, offset, false)
  2566. }
  2567. return offset + 4
  2568. }
  2569. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  2570. value = +value
  2571. offset = offset | 0
  2572. if (!noAssert) {
  2573. var limit = Math.pow(2, 8 * byteLength - 1)
  2574. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  2575. }
  2576. var i = 0
  2577. var mul = 1
  2578. var sub = 0
  2579. this[offset] = value & 0xFF
  2580. while (++i < byteLength && (mul *= 0x100)) {
  2581. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  2582. sub = 1
  2583. }
  2584. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  2585. }
  2586. return offset + byteLength
  2587. }
  2588. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  2589. value = +value
  2590. offset = offset | 0
  2591. if (!noAssert) {
  2592. var limit = Math.pow(2, 8 * byteLength - 1)
  2593. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  2594. }
  2595. var i = byteLength - 1
  2596. var mul = 1
  2597. var sub = 0
  2598. this[offset + i] = value & 0xFF
  2599. while (--i >= 0 && (mul *= 0x100)) {
  2600. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  2601. sub = 1
  2602. }
  2603. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  2604. }
  2605. return offset + byteLength
  2606. }
  2607. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  2608. value = +value
  2609. offset = offset | 0
  2610. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  2611. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  2612. if (value < 0) value = 0xff + value + 1
  2613. this[offset] = (value & 0xff)
  2614. return offset + 1
  2615. }
  2616. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  2617. value = +value
  2618. offset = offset | 0
  2619. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  2620. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2621. this[offset] = (value & 0xff)
  2622. this[offset + 1] = (value >>> 8)
  2623. } else {
  2624. objectWriteUInt16(this, value, offset, true)
  2625. }
  2626. return offset + 2
  2627. }
  2628. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  2629. value = +value
  2630. offset = offset | 0
  2631. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  2632. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2633. this[offset] = (value >>> 8)
  2634. this[offset + 1] = (value & 0xff)
  2635. } else {
  2636. objectWriteUInt16(this, value, offset, false)
  2637. }
  2638. return offset + 2
  2639. }
  2640. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  2641. value = +value
  2642. offset = offset | 0
  2643. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  2644. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2645. this[offset] = (value & 0xff)
  2646. this[offset + 1] = (value >>> 8)
  2647. this[offset + 2] = (value >>> 16)
  2648. this[offset + 3] = (value >>> 24)
  2649. } else {
  2650. objectWriteUInt32(this, value, offset, true)
  2651. }
  2652. return offset + 4
  2653. }
  2654. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  2655. value = +value
  2656. offset = offset | 0
  2657. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  2658. if (value < 0) value = 0xffffffff + value + 1
  2659. if (Buffer.TYPED_ARRAY_SUPPORT) {
  2660. this[offset] = (value >>> 24)
  2661. this[offset + 1] = (value >>> 16)
  2662. this[offset + 2] = (value >>> 8)
  2663. this[offset + 3] = (value & 0xff)
  2664. } else {
  2665. objectWriteUInt32(this, value, offset, false)
  2666. }
  2667. return offset + 4
  2668. }
  2669. function checkIEEE754 (buf, value, offset, ext, max, min) {
  2670. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  2671. if (offset < 0) throw new RangeError('Index out of range')
  2672. }
  2673. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  2674. if (!noAssert) {
  2675. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  2676. }
  2677. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  2678. return offset + 4
  2679. }
  2680. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  2681. return writeFloat(this, value, offset, true, noAssert)
  2682. }
  2683. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  2684. return writeFloat(this, value, offset, false, noAssert)
  2685. }
  2686. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  2687. if (!noAssert) {
  2688. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  2689. }
  2690. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  2691. return offset + 8
  2692. }
  2693. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  2694. return writeDouble(this, value, offset, true, noAssert)
  2695. }
  2696. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  2697. return writeDouble(this, value, offset, false, noAssert)
  2698. }
  2699. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  2700. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  2701. if (!start) start = 0
  2702. if (!end && end !== 0) end = this.length
  2703. if (targetStart >= target.length) targetStart = target.length
  2704. if (!targetStart) targetStart = 0
  2705. if (end > 0 && end < start) end = start
  2706. // Copy 0 bytes; we're done
  2707. if (end === start) return 0
  2708. if (target.length === 0 || this.length === 0) return 0
  2709. // Fatal error conditions
  2710. if (targetStart < 0) {
  2711. throw new RangeError('targetStart out of bounds')
  2712. }
  2713. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  2714. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  2715. // Are we oob?
  2716. if (end > this.length) end = this.length
  2717. if (target.length - targetStart < end - start) {
  2718. end = target.length - targetStart + start
  2719. }
  2720. var len = end - start
  2721. var i
  2722. if (this === target && start < targetStart && targetStart < end) {
  2723. // descending copy from end
  2724. for (i = len - 1; i >= 0; --i) {
  2725. target[i + targetStart] = this[i + start]
  2726. }
  2727. } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
  2728. // ascending copy from start
  2729. for (i = 0; i < len; ++i) {
  2730. target[i + targetStart] = this[i + start]
  2731. }
  2732. } else {
  2733. Uint8Array.prototype.set.call(
  2734. target,
  2735. this.subarray(start, start + len),
  2736. targetStart
  2737. )
  2738. }
  2739. return len
  2740. }
  2741. // Usage:
  2742. // buffer.fill(number[, offset[, end]])
  2743. // buffer.fill(buffer[, offset[, end]])
  2744. // buffer.fill(string[, offset[, end]][, encoding])
  2745. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  2746. // Handle string cases:
  2747. if (typeof val === 'string') {
  2748. if (typeof start === 'string') {
  2749. encoding = start
  2750. start = 0
  2751. end = this.length
  2752. } else if (typeof end === 'string') {
  2753. encoding = end
  2754. end = this.length
  2755. }
  2756. if (val.length === 1) {
  2757. var code = val.charCodeAt(0)
  2758. if (code < 256) {
  2759. val = code
  2760. }
  2761. }
  2762. if (encoding !== undefined && typeof encoding !== 'string') {
  2763. throw new TypeError('encoding must be a string')
  2764. }
  2765. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  2766. throw new TypeError('Unknown encoding: ' + encoding)
  2767. }
  2768. } else if (typeof val === 'number') {
  2769. val = val & 255
  2770. }
  2771. // Invalid ranges are not set to a default, so can range check early.
  2772. if (start < 0 || this.length < start || this.length < end) {
  2773. throw new RangeError('Out of range index')
  2774. }
  2775. if (end <= start) {
  2776. return this
  2777. }
  2778. start = start >>> 0
  2779. end = end === undefined ? this.length : end >>> 0
  2780. if (!val) val = 0
  2781. var i
  2782. if (typeof val === 'number') {
  2783. for (i = start; i < end; ++i) {
  2784. this[i] = val
  2785. }
  2786. } else {
  2787. var bytes = Buffer.isBuffer(val)
  2788. ? val
  2789. : utf8ToBytes(new Buffer(val, encoding).toString())
  2790. var len = bytes.length
  2791. for (i = 0; i < end - start; ++i) {
  2792. this[i + start] = bytes[i % len]
  2793. }
  2794. }
  2795. return this
  2796. }
  2797. // HELPER FUNCTIONS
  2798. // ================
  2799. var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g
  2800. function base64clean (str) {
  2801. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  2802. str = stringtrim(str).replace(INVALID_BASE64_RE, '')
  2803. // Node converts strings with length < 2 to ''
  2804. if (str.length < 2) return ''
  2805. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  2806. while (str.length % 4 !== 0) {
  2807. str = str + '='
  2808. }
  2809. return str
  2810. }
  2811. function stringtrim (str) {
  2812. if (str.trim) return str.trim()
  2813. return str.replace(/^\s+|\s+$/g, '')
  2814. }
  2815. function toHex (n) {
  2816. if (n < 16) return '0' + n.toString(16)
  2817. return n.toString(16)
  2818. }
  2819. function utf8ToBytes (string, units) {
  2820. units = units || Infinity
  2821. var codePoint
  2822. var length = string.length
  2823. var leadSurrogate = null
  2824. var bytes = []
  2825. for (var i = 0; i < length; ++i) {
  2826. codePoint = string.charCodeAt(i)
  2827. // is surrogate component
  2828. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  2829. // last char was a lead
  2830. if (!leadSurrogate) {
  2831. // no lead yet
  2832. if (codePoint > 0xDBFF) {
  2833. // unexpected trail
  2834. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  2835. continue
  2836. } else if (i + 1 === length) {
  2837. // unpaired lead
  2838. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  2839. continue
  2840. }
  2841. // valid lead
  2842. leadSurrogate = codePoint
  2843. continue
  2844. }
  2845. // 2 leads in a row
  2846. if (codePoint < 0xDC00) {
  2847. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  2848. leadSurrogate = codePoint
  2849. continue
  2850. }
  2851. // valid surrogate pair
  2852. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  2853. } else if (leadSurrogate) {
  2854. // valid bmp char, but last char was a lead
  2855. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  2856. }
  2857. leadSurrogate = null
  2858. // encode utf8
  2859. if (codePoint < 0x80) {
  2860. if ((units -= 1) < 0) break
  2861. bytes.push(codePoint)
  2862. } else if (codePoint < 0x800) {
  2863. if ((units -= 2) < 0) break
  2864. bytes.push(
  2865. codePoint >> 0x6 | 0xC0,
  2866. codePoint & 0x3F | 0x80
  2867. )
  2868. } else if (codePoint < 0x10000) {
  2869. if ((units -= 3) < 0) break
  2870. bytes.push(
  2871. codePoint >> 0xC | 0xE0,
  2872. codePoint >> 0x6 & 0x3F | 0x80,
  2873. codePoint & 0x3F | 0x80
  2874. )
  2875. } else if (codePoint < 0x110000) {
  2876. if ((units -= 4) < 0) break
  2877. bytes.push(
  2878. codePoint >> 0x12 | 0xF0,
  2879. codePoint >> 0xC & 0x3F | 0x80,
  2880. codePoint >> 0x6 & 0x3F | 0x80,
  2881. codePoint & 0x3F | 0x80
  2882. )
  2883. } else {
  2884. throw new Error('Invalid code point')
  2885. }
  2886. }
  2887. return bytes
  2888. }
  2889. function asciiToBytes (str) {
  2890. var byteArray = []
  2891. for (var i = 0; i < str.length; ++i) {
  2892. // Node's code seems to be doing this and not & 0x7F..
  2893. byteArray.push(str.charCodeAt(i) & 0xFF)
  2894. }
  2895. return byteArray
  2896. }
  2897. function utf16leToBytes (str, units) {
  2898. var c, hi, lo
  2899. var byteArray = []
  2900. for (var i = 0; i < str.length; ++i) {
  2901. if ((units -= 2) < 0) break
  2902. c = str.charCodeAt(i)
  2903. hi = c >> 8
  2904. lo = c % 256
  2905. byteArray.push(lo)
  2906. byteArray.push(hi)
  2907. }
  2908. return byteArray
  2909. }
  2910. function base64ToBytes (str) {
  2911. return base64.toByteArray(base64clean(str))
  2912. }
  2913. function blitBuffer (src, dst, offset, length) {
  2914. for (var i = 0; i < length; ++i) {
  2915. if ((i + offset >= dst.length) || (i >= src.length)) break
  2916. dst[i + offset] = src[i]
  2917. }
  2918. return i
  2919. }
  2920. function isnan (val) {
  2921. return val !== val // eslint-disable-line no-self-compare
  2922. }
  2923. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2924. },{"base64-js":2,"ieee754":14,"isarray":17}],7:[function(require,module,exports){
  2925. (function (Buffer){
  2926. var Transform = require('stream').Transform
  2927. var inherits = require('inherits')
  2928. var StringDecoder = require('string_decoder').StringDecoder
  2929. module.exports = CipherBase
  2930. inherits(CipherBase, Transform)
  2931. function CipherBase (hashMode) {
  2932. Transform.call(this)
  2933. this.hashMode = typeof hashMode === 'string'
  2934. if (this.hashMode) {
  2935. this[hashMode] = this._finalOrDigest
  2936. } else {
  2937. this.final = this._finalOrDigest
  2938. }
  2939. this._decoder = null
  2940. this._encoding = null
  2941. }
  2942. CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
  2943. if (typeof data === 'string') {
  2944. data = new Buffer(data, inputEnc)
  2945. }
  2946. var outData = this._update(data)
  2947. if (this.hashMode) {
  2948. return this
  2949. }
  2950. if (outputEnc) {
  2951. outData = this._toString(outData, outputEnc)
  2952. }
  2953. return outData
  2954. }
  2955. CipherBase.prototype.setAutoPadding = function () {}
  2956. CipherBase.prototype.getAuthTag = function () {
  2957. throw new Error('trying to get auth tag in unsupported state')
  2958. }
  2959. CipherBase.prototype.setAuthTag = function () {
  2960. throw new Error('trying to set auth tag in unsupported state')
  2961. }
  2962. CipherBase.prototype.setAAD = function () {
  2963. throw new Error('trying to set aad in unsupported state')
  2964. }
  2965. CipherBase.prototype._transform = function (data, _, next) {
  2966. var err
  2967. try {
  2968. if (this.hashMode) {
  2969. this._update(data)
  2970. } else {
  2971. this.push(this._update(data))
  2972. }
  2973. } catch (e) {
  2974. err = e
  2975. } finally {
  2976. next(err)
  2977. }
  2978. }
  2979. CipherBase.prototype._flush = function (done) {
  2980. var err
  2981. try {
  2982. this.push(this._final())
  2983. } catch (e) {
  2984. err = e
  2985. } finally {
  2986. done(err)
  2987. }
  2988. }
  2989. CipherBase.prototype._finalOrDigest = function (outputEnc) {
  2990. var outData = this._final() || new Buffer('')
  2991. if (outputEnc) {
  2992. outData = this._toString(outData, outputEnc, true)
  2993. }
  2994. return outData
  2995. }
  2996. CipherBase.prototype._toString = function (value, enc, fin) {
  2997. if (!this._decoder) {
  2998. this._decoder = new StringDecoder(enc)
  2999. this._encoding = enc
  3000. }
  3001. if (this._encoding !== enc) {
  3002. throw new Error('can\'t switch encodings')
  3003. }
  3004. var out = this._decoder.write(value)
  3005. if (fin) {
  3006. out += this._decoder.end()
  3007. }
  3008. return out
  3009. }
  3010. }).call(this,require("buffer").Buffer)
  3011. },{"buffer":6,"inherits":15,"stream":45,"string_decoder":46}],8:[function(require,module,exports){
  3012. (function (Buffer){
  3013. // Copyright Joyent, Inc. and other Node contributors.
  3014. //
  3015. // Permission is hereby granted, free of charge, to any person obtaining a
  3016. // copy of this software and associated documentation files (the
  3017. // "Software"), to deal in the Software without restriction, including
  3018. // without limitation the rights to use, copy, modify, merge, publish,
  3019. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3020. // persons to whom the Software is furnished to do so, subject to the
  3021. // following conditions:
  3022. //
  3023. // The above copyright notice and this permission notice shall be included
  3024. // in all copies or substantial portions of the Software.
  3025. //
  3026. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3027. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3028. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3029. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3030. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3031. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3032. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3033. // NOTE: These type checking functions intentionally don't use `instanceof`
  3034. // because it is fragile and can be easily faked with `Object.create()`.
  3035. function isArray(arg) {
  3036. if (Array.isArray) {
  3037. return Array.isArray(arg);
  3038. }
  3039. return objectToString(arg) === '[object Array]';
  3040. }
  3041. exports.isArray = isArray;
  3042. function isBoolean(arg) {
  3043. return typeof arg === 'boolean';
  3044. }
  3045. exports.isBoolean = isBoolean;
  3046. function isNull(arg) {
  3047. return arg === null;
  3048. }
  3049. exports.isNull = isNull;
  3050. function isNullOrUndefined(arg) {
  3051. return arg == null;
  3052. }
  3053. exports.isNullOrUndefined = isNullOrUndefined;
  3054. function isNumber(arg) {
  3055. return typeof arg === 'number';
  3056. }
  3057. exports.isNumber = isNumber;
  3058. function isString(arg) {
  3059. return typeof arg === 'string';
  3060. }
  3061. exports.isString = isString;
  3062. function isSymbol(arg) {
  3063. return typeof arg === 'symbol';
  3064. }
  3065. exports.isSymbol = isSymbol;
  3066. function isUndefined(arg) {
  3067. return arg === void 0;
  3068. }
  3069. exports.isUndefined = isUndefined;
  3070. function isRegExp(re) {
  3071. return objectToString(re) === '[object RegExp]';
  3072. }
  3073. exports.isRegExp = isRegExp;
  3074. function isObject(arg) {
  3075. return typeof arg === 'object' && arg !== null;
  3076. }
  3077. exports.isObject = isObject;
  3078. function isDate(d) {
  3079. return objectToString(d) === '[object Date]';
  3080. }
  3081. exports.isDate = isDate;
  3082. function isError(e) {
  3083. return (objectToString(e) === '[object Error]' || e instanceof Error);
  3084. }
  3085. exports.isError = isError;
  3086. function isFunction(arg) {
  3087. return typeof arg === 'function';
  3088. }
  3089. exports.isFunction = isFunction;
  3090. function isPrimitive(arg) {
  3091. return arg === null ||
  3092. typeof arg === 'boolean' ||
  3093. typeof arg === 'number' ||
  3094. typeof arg === 'string' ||
  3095. typeof arg === 'symbol' || // ES6 symbol
  3096. typeof arg === 'undefined';
  3097. }
  3098. exports.isPrimitive = isPrimitive;
  3099. exports.isBuffer = Buffer.isBuffer;
  3100. function objectToString(o) {
  3101. return Object.prototype.toString.call(o);
  3102. }
  3103. }).call(this,{"isBuffer":require("../../is-buffer/index.js")})
  3104. },{"../../is-buffer/index.js":16}],9:[function(require,module,exports){
  3105. (function (Buffer){
  3106. 'use strict';
  3107. var inherits = require('inherits')
  3108. var md5 = require('./md5')
  3109. var rmd160 = require('ripemd160')
  3110. var sha = require('sha.js')
  3111. var Base = require('cipher-base')
  3112. function HashNoConstructor(hash) {
  3113. Base.call(this, 'digest')
  3114. this._hash = hash
  3115. this.buffers = []
  3116. }
  3117. inherits(HashNoConstructor, Base)
  3118. HashNoConstructor.prototype._update = function (data) {
  3119. this.buffers.push(data)
  3120. }
  3121. HashNoConstructor.prototype._final = function () {
  3122. var buf = Buffer.concat(this.buffers)
  3123. var r = this._hash(buf)
  3124. this.buffers = null
  3125. return r
  3126. }
  3127. function Hash(hash) {
  3128. Base.call(this, 'digest')
  3129. this._hash = hash
  3130. }
  3131. inherits(Hash, Base)
  3132. Hash.prototype._update = function (data) {
  3133. this._hash.update(data)
  3134. }
  3135. Hash.prototype._final = function () {
  3136. return this._hash.digest()
  3137. }
  3138. module.exports = function createHash (alg) {
  3139. alg = alg.toLowerCase()
  3140. if ('md5' === alg) return new HashNoConstructor(md5)
  3141. if ('rmd160' === alg || 'ripemd160' === alg) return new HashNoConstructor(rmd160)
  3142. return new Hash(sha(alg))
  3143. }
  3144. }).call(this,require("buffer").Buffer)
  3145. },{"./md5":11,"buffer":6,"cipher-base":7,"inherits":15,"ripemd160":36,"sha.js":38}],10:[function(require,module,exports){
  3146. (function (Buffer){
  3147. 'use strict';
  3148. var intSize = 4;
  3149. var zeroBuffer = new Buffer(intSize); zeroBuffer.fill(0);
  3150. var chrsz = 8;
  3151. function toArray(buf, bigEndian) {
  3152. if ((buf.length % intSize) !== 0) {
  3153. var len = buf.length + (intSize - (buf.length % intSize));
  3154. buf = Buffer.concat([buf, zeroBuffer], len);
  3155. }
  3156. var arr = [];
  3157. var fn = bigEndian ? buf.readInt32BE : buf.readInt32LE;
  3158. for (var i = 0; i < buf.length; i += intSize) {
  3159. arr.push(fn.call(buf, i));
  3160. }
  3161. return arr;
  3162. }
  3163. function toBuffer(arr, size, bigEndian) {
  3164. var buf = new Buffer(size);
  3165. var fn = bigEndian ? buf.writeInt32BE : buf.writeInt32LE;
  3166. for (var i = 0; i < arr.length; i++) {
  3167. fn.call(buf, arr[i], i * 4, true);
  3168. }
  3169. return buf;
  3170. }
  3171. function hash(buf, fn, hashSize, bigEndian) {
  3172. if (!Buffer.isBuffer(buf)) buf = new Buffer(buf);
  3173. var arr = fn(toArray(buf, bigEndian), buf.length * chrsz);
  3174. return toBuffer(arr, hashSize, bigEndian);
  3175. }
  3176. exports.hash = hash;
  3177. }).call(this,require("buffer").Buffer)
  3178. },{"buffer":6}],11:[function(require,module,exports){
  3179. 'use strict';
  3180. /*
  3181. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3182. * Digest Algorithm, as defined in RFC 1321.
  3183. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  3184. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  3185. * Distributed under the BSD License
  3186. * See http://pajhome.org.uk/crypt/md5 for more info.
  3187. */
  3188. var helpers = require('./helpers');
  3189. /*
  3190. * Calculate the MD5 of an array of little-endian words, and a bit length
  3191. */
  3192. function core_md5(x, len)
  3193. {
  3194. /* append padding */
  3195. x[len >> 5] |= 0x80 << ((len) % 32);
  3196. x[(((len + 64) >>> 9) << 4) + 14] = len;
  3197. var a = 1732584193;
  3198. var b = -271733879;
  3199. var c = -1732584194;
  3200. var d = 271733878;
  3201. for(var i = 0; i < x.length; i += 16)
  3202. {
  3203. var olda = a;
  3204. var oldb = b;
  3205. var oldc = c;
  3206. var oldd = d;
  3207. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  3208. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  3209. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  3210. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  3211. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  3212. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  3213. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  3214. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  3215. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  3216. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  3217. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  3218. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  3219. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  3220. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  3221. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  3222. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  3223. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  3224. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  3225. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  3226. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  3227. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  3228. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  3229. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  3230. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  3231. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  3232. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  3233. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  3234. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  3235. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  3236. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  3237. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  3238. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  3239. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  3240. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  3241. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  3242. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  3243. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  3244. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  3245. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  3246. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  3247. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  3248. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  3249. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  3250. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  3251. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  3252. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  3253. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  3254. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  3255. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  3256. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  3257. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  3258. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  3259. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  3260. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  3261. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  3262. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  3263. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  3264. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  3265. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  3266. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  3267. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  3268. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  3269. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  3270. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  3271. a = safe_add(a, olda);
  3272. b = safe_add(b, oldb);
  3273. c = safe_add(c, oldc);
  3274. d = safe_add(d, oldd);
  3275. }
  3276. return Array(a, b, c, d);
  3277. }
  3278. /*
  3279. * These functions implement the four basic operations the algorithm uses.
  3280. */
  3281. function md5_cmn(q, a, b, x, s, t)
  3282. {
  3283. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  3284. }
  3285. function md5_ff(a, b, c, d, x, s, t)
  3286. {
  3287. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  3288. }
  3289. function md5_gg(a, b, c, d, x, s, t)
  3290. {
  3291. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  3292. }
  3293. function md5_hh(a, b, c, d, x, s, t)
  3294. {
  3295. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  3296. }
  3297. function md5_ii(a, b, c, d, x, s, t)
  3298. {
  3299. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  3300. }
  3301. /*
  3302. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  3303. * to work around bugs in some JS interpreters.
  3304. */
  3305. function safe_add(x, y)
  3306. {
  3307. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  3308. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  3309. return (msw << 16) | (lsw & 0xFFFF);
  3310. }
  3311. /*
  3312. * Bitwise rotate a 32-bit number to the left.
  3313. */
  3314. function bit_rol(num, cnt)
  3315. {
  3316. return (num << cnt) | (num >>> (32 - cnt));
  3317. }
  3318. module.exports = function md5(buf) {
  3319. return helpers.hash(buf, core_md5, 16);
  3320. };
  3321. },{"./helpers":10}],12:[function(require,module,exports){
  3322. (function (Buffer){
  3323. 'use strict';
  3324. var createHash = require('create-hash/browser');
  3325. var inherits = require('inherits')
  3326. var Transform = require('stream').Transform
  3327. var ZEROS = new Buffer(128)
  3328. ZEROS.fill(0)
  3329. function Hmac(alg, key) {
  3330. Transform.call(this)
  3331. alg = alg.toLowerCase()
  3332. if (typeof key === 'string') {
  3333. key = new Buffer(key)
  3334. }
  3335. var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64
  3336. this._alg = alg
  3337. this._key = key
  3338. if (key.length > blocksize) {
  3339. key = createHash(alg).update(key).digest()
  3340. } else if (key.length < blocksize) {
  3341. key = Buffer.concat([key, ZEROS], blocksize)
  3342. }
  3343. var ipad = this._ipad = new Buffer(blocksize)
  3344. var opad = this._opad = new Buffer(blocksize)
  3345. for (var i = 0; i < blocksize; i++) {
  3346. ipad[i] = key[i] ^ 0x36
  3347. opad[i] = key[i] ^ 0x5C
  3348. }
  3349. this._hash = createHash(alg).update(ipad)
  3350. }
  3351. inherits(Hmac, Transform)
  3352. Hmac.prototype.update = function (data, enc) {
  3353. this._hash.update(data, enc)
  3354. return this
  3355. }
  3356. Hmac.prototype._transform = function (data, _, next) {
  3357. this._hash.update(data)
  3358. next()
  3359. }
  3360. Hmac.prototype._flush = function (next) {
  3361. this.push(this.digest())
  3362. next()
  3363. }
  3364. Hmac.prototype.digest = function (enc) {
  3365. var h = this._hash.digest()
  3366. return createHash(this._alg).update(this._opad).update(h).digest(enc)
  3367. }
  3368. module.exports = function createHmac(alg, key) {
  3369. return new Hmac(alg, key)
  3370. }
  3371. }).call(this,require("buffer").Buffer)
  3372. },{"buffer":6,"create-hash/browser":9,"inherits":15,"stream":45}],13:[function(require,module,exports){
  3373. // Copyright Joyent, Inc. and other Node contributors.
  3374. //
  3375. // Permission is hereby granted, free of charge, to any person obtaining a
  3376. // copy of this software and associated documentation files (the
  3377. // "Software"), to deal in the Software without restriction, including
  3378. // without limitation the rights to use, copy, modify, merge, publish,
  3379. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3380. // persons to whom the Software is furnished to do so, subject to the
  3381. // following conditions:
  3382. //
  3383. // The above copyright notice and this permission notice shall be included
  3384. // in all copies or substantial portions of the Software.
  3385. //
  3386. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3387. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3388. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3389. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3390. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3391. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  3392. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  3393. function EventEmitter() {
  3394. this._events = this._events || {};
  3395. this._maxListeners = this._maxListeners || undefined;
  3396. }
  3397. module.exports = EventEmitter;
  3398. // Backwards-compat with node 0.10.x
  3399. EventEmitter.EventEmitter = EventEmitter;
  3400. EventEmitter.prototype._events = undefined;
  3401. EventEmitter.prototype._maxListeners = undefined;
  3402. // By default EventEmitters will print a warning if more than 10 listeners are
  3403. // added to it. This is a useful default which helps finding memory leaks.
  3404. EventEmitter.defaultMaxListeners = 10;
  3405. // Obviously not all Emitters should be limited to 10. This function allows
  3406. // that to be increased. Set to zero for unlimited.
  3407. EventEmitter.prototype.setMaxListeners = function(n) {
  3408. if (!isNumber(n) || n < 0 || isNaN(n))
  3409. throw TypeError('n must be a positive number');
  3410. this._maxListeners = n;
  3411. return this;
  3412. };
  3413. EventEmitter.prototype.emit = function(type) {
  3414. var er, handler, len, args, i, listeners;
  3415. if (!this._events)
  3416. this._events = {};
  3417. // If there is no 'error' event listener then throw.
  3418. if (type === 'error') {
  3419. if (!this._events.error ||
  3420. (isObject(this._events.error) && !this._events.error.length)) {
  3421. er = arguments[1];
  3422. if (er instanceof Error) {
  3423. throw er; // Unhandled 'error' event
  3424. } else {
  3425. // At least give some kind of context to the user
  3426. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  3427. err.context = er;
  3428. throw err;
  3429. }
  3430. }
  3431. }
  3432. handler = this._events[type];
  3433. if (isUndefined(handler))
  3434. return false;
  3435. if (isFunction(handler)) {
  3436. switch (arguments.length) {
  3437. // fast cases
  3438. case 1:
  3439. handler.call(this);
  3440. break;
  3441. case 2:
  3442. handler.call(this, arguments[1]);
  3443. break;
  3444. case 3:
  3445. handler.call(this, arguments[1], arguments[2]);
  3446. break;
  3447. // slower
  3448. default:
  3449. args = Array.prototype.slice.call(arguments, 1);
  3450. handler.apply(this, args);
  3451. }
  3452. } else if (isObject(handler)) {
  3453. args = Array.prototype.slice.call(arguments, 1);
  3454. listeners = handler.slice();
  3455. len = listeners.length;
  3456. for (i = 0; i < len; i++)
  3457. listeners[i].apply(this, args);
  3458. }
  3459. return true;
  3460. };
  3461. EventEmitter.prototype.addListener = function(type, listener) {
  3462. var m;
  3463. if (!isFunction(listener))
  3464. throw TypeError('listener must be a function');
  3465. if (!this._events)
  3466. this._events = {};
  3467. // To avoid recursion in the case that type === "newListener"! Before
  3468. // adding it to the listeners, first emit "newListener".
  3469. if (this._events.newListener)
  3470. this.emit('newListener', type,
  3471. isFunction(listener.listener) ?
  3472. listener.listener : listener);
  3473. if (!this._events[type])
  3474. // Optimize the case of one listener. Don't need the extra array object.
  3475. this._events[type] = listener;
  3476. else if (isObject(this._events[type]))
  3477. // If we've already got an array, just append.
  3478. this._events[type].push(listener);
  3479. else
  3480. // Adding the second element, need to change to array.
  3481. this._events[type] = [this._events[type], listener];
  3482. // Check for listener leak
  3483. if (isObject(this._events[type]) && !this._events[type].warned) {
  3484. if (!isUndefined(this._maxListeners)) {
  3485. m = this._maxListeners;
  3486. } else {
  3487. m = EventEmitter.defaultMaxListeners;
  3488. }
  3489. if (m && m > 0 && this._events[type].length > m) {
  3490. this._events[type].warned = true;
  3491. console.error('(node) warning: possible EventEmitter memory ' +
  3492. 'leak detected. %d listeners added. ' +
  3493. 'Use emitter.setMaxListeners() to increase limit.',
  3494. this._events[type].length);
  3495. if (typeof console.trace === 'function') {
  3496. // not supported in IE 10
  3497. console.trace();
  3498. }
  3499. }
  3500. }
  3501. return this;
  3502. };
  3503. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  3504. EventEmitter.prototype.once = function(type, listener) {
  3505. if (!isFunction(listener))
  3506. throw TypeError('listener must be a function');
  3507. var fired = false;
  3508. function g() {
  3509. this.removeListener(type, g);
  3510. if (!fired) {
  3511. fired = true;
  3512. listener.apply(this, arguments);
  3513. }
  3514. }
  3515. g.listener = listener;
  3516. this.on(type, g);
  3517. return this;
  3518. };
  3519. // emits a 'removeListener' event iff the listener was removed
  3520. EventEmitter.prototype.removeListener = function(type, listener) {
  3521. var list, position, length, i;
  3522. if (!isFunction(listener))
  3523. throw TypeError('listener must be a function');
  3524. if (!this._events || !this._events[type])
  3525. return this;
  3526. list = this._events[type];
  3527. length = list.length;
  3528. position = -1;
  3529. if (list === listener ||
  3530. (isFunction(list.listener) && list.listener === listener)) {
  3531. delete this._events[type];
  3532. if (this._events.removeListener)
  3533. this.emit('removeListener', type, listener);
  3534. } else if (isObject(list)) {
  3535. for (i = length; i-- > 0;) {
  3536. if (list[i] === listener ||
  3537. (list[i].listener && list[i].listener === listener)) {
  3538. position = i;
  3539. break;
  3540. }
  3541. }
  3542. if (position < 0)
  3543. return this;
  3544. if (list.length === 1) {
  3545. list.length = 0;
  3546. delete this._events[type];
  3547. } else {
  3548. list.splice(position, 1);
  3549. }
  3550. if (this._events.removeListener)
  3551. this.emit('removeListener', type, listener);
  3552. }
  3553. return this;
  3554. };
  3555. EventEmitter.prototype.removeAllListeners = function(type) {
  3556. var key, listeners;
  3557. if (!this._events)
  3558. return this;
  3559. // not listening for removeListener, no need to emit
  3560. if (!this._events.removeListener) {
  3561. if (arguments.length === 0)
  3562. this._events = {};
  3563. else if (this._events[type])
  3564. delete this._events[type];
  3565. return this;
  3566. }
  3567. // emit removeListener for all listeners on all events
  3568. if (arguments.length === 0) {
  3569. for (key in this._events) {
  3570. if (key === 'removeListener') continue;
  3571. this.removeAllListeners(key);
  3572. }
  3573. this.removeAllListeners('removeListener');
  3574. this._events = {};
  3575. return this;
  3576. }
  3577. listeners = this._events[type];
  3578. if (isFunction(listeners)) {
  3579. this.removeListener(type, listeners);
  3580. } else if (listeners) {
  3581. // LIFO order
  3582. while (listeners.length)
  3583. this.removeListener(type, listeners[listeners.length - 1]);
  3584. }
  3585. delete this._events[type];
  3586. return this;
  3587. };
  3588. EventEmitter.prototype.listeners = function(type) {
  3589. var ret;
  3590. if (!this._events || !this._events[type])
  3591. ret = [];
  3592. else if (isFunction(this._events[type]))
  3593. ret = [this._events[type]];
  3594. else
  3595. ret = this._events[type].slice();
  3596. return ret;
  3597. };
  3598. EventEmitter.prototype.listenerCount = function(type) {
  3599. if (this._events) {
  3600. var evlistener = this._events[type];
  3601. if (isFunction(evlistener))
  3602. return 1;
  3603. else if (evlistener)
  3604. return evlistener.length;
  3605. }
  3606. return 0;
  3607. };
  3608. EventEmitter.listenerCount = function(emitter, type) {
  3609. return emitter.listenerCount(type);
  3610. };
  3611. function isFunction(arg) {
  3612. return typeof arg === 'function';
  3613. }
  3614. function isNumber(arg) {
  3615. return typeof arg === 'number';
  3616. }
  3617. function isObject(arg) {
  3618. return typeof arg === 'object' && arg !== null;
  3619. }
  3620. function isUndefined(arg) {
  3621. return arg === void 0;
  3622. }
  3623. },{}],14:[function(require,module,exports){
  3624. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  3625. var e, m
  3626. var eLen = nBytes * 8 - mLen - 1
  3627. var eMax = (1 << eLen) - 1
  3628. var eBias = eMax >> 1
  3629. var nBits = -7
  3630. var i = isLE ? (nBytes - 1) : 0
  3631. var d = isLE ? -1 : 1
  3632. var s = buffer[offset + i]
  3633. i += d
  3634. e = s & ((1 << (-nBits)) - 1)
  3635. s >>= (-nBits)
  3636. nBits += eLen
  3637. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  3638. m = e & ((1 << (-nBits)) - 1)
  3639. e >>= (-nBits)
  3640. nBits += mLen
  3641. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  3642. if (e === 0) {
  3643. e = 1 - eBias
  3644. } else if (e === eMax) {
  3645. return m ? NaN : ((s ? -1 : 1) * Infinity)
  3646. } else {
  3647. m = m + Math.pow(2, mLen)
  3648. e = e - eBias
  3649. }
  3650. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  3651. }
  3652. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  3653. var e, m, c
  3654. var eLen = nBytes * 8 - mLen - 1
  3655. var eMax = (1 << eLen) - 1
  3656. var eBias = eMax >> 1
  3657. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  3658. var i = isLE ? 0 : (nBytes - 1)
  3659. var d = isLE ? 1 : -1
  3660. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  3661. value = Math.abs(value)
  3662. if (isNaN(value) || value === Infinity) {
  3663. m = isNaN(value) ? 1 : 0
  3664. e = eMax
  3665. } else {
  3666. e = Math.floor(Math.log(value) / Math.LN2)
  3667. if (value * (c = Math.pow(2, -e)) < 1) {
  3668. e--
  3669. c *= 2
  3670. }
  3671. if (e + eBias >= 1) {
  3672. value += rt / c
  3673. } else {
  3674. value += rt * Math.pow(2, 1 - eBias)
  3675. }
  3676. if (value * c >= 2) {
  3677. e++
  3678. c /= 2
  3679. }
  3680. if (e + eBias >= eMax) {
  3681. m = 0
  3682. e = eMax
  3683. } else if (e + eBias >= 1) {
  3684. m = (value * c - 1) * Math.pow(2, mLen)
  3685. e = e + eBias
  3686. } else {
  3687. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  3688. e = 0
  3689. }
  3690. }
  3691. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  3692. e = (e << mLen) | m
  3693. eLen += mLen
  3694. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  3695. buffer[offset + i - d] |= s * 128
  3696. }
  3697. },{}],15:[function(require,module,exports){
  3698. if (typeof Object.create === 'function') {
  3699. // implementation from standard node.js 'util' module
  3700. module.exports = function inherits(ctor, superCtor) {
  3701. ctor.super_ = superCtor
  3702. ctor.prototype = Object.create(superCtor.prototype, {
  3703. constructor: {
  3704. value: ctor,
  3705. enumerable: false,
  3706. writable: true,
  3707. configurable: true
  3708. }
  3709. });
  3710. };
  3711. } else {
  3712. // old school shim for old browsers
  3713. module.exports = function inherits(ctor, superCtor) {
  3714. ctor.super_ = superCtor
  3715. var TempCtor = function () {}
  3716. TempCtor.prototype = superCtor.prototype
  3717. ctor.prototype = new TempCtor()
  3718. ctor.prototype.constructor = ctor
  3719. }
  3720. }
  3721. },{}],16:[function(require,module,exports){
  3722. /*!
  3723. * Determine if an object is a Buffer
  3724. *
  3725. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  3726. * @license MIT
  3727. */
  3728. // The _isBuffer check is for Safari 5-7 support, because it's missing
  3729. // Object.prototype.constructor. Remove this eventually
  3730. module.exports = function (obj) {
  3731. return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer)
  3732. }
  3733. function isBuffer (obj) {
  3734. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  3735. }
  3736. // For Node v0.10 support. Remove this eventually.
  3737. function isSlowBuffer (obj) {
  3738. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0))
  3739. }
  3740. },{}],17:[function(require,module,exports){
  3741. var toString = {}.toString;
  3742. module.exports = Array.isArray || function (arr) {
  3743. return toString.call(arr) == '[object Array]';
  3744. };
  3745. },{}],18:[function(require,module,exports){
  3746. 'use strict';
  3747. /* eslint-disable no-unused-vars */
  3748. var hasOwnProperty = Object.prototype.hasOwnProperty;
  3749. var propIsEnumerable = Object.prototype.propertyIsEnumerable;
  3750. function toObject(val) {
  3751. if (val === null || val === undefined) {
  3752. throw new TypeError('Object.assign cannot be called with null or undefined');
  3753. }
  3754. return Object(val);
  3755. }
  3756. function shouldUseNative() {
  3757. try {
  3758. if (!Object.assign) {
  3759. return false;
  3760. }
  3761. // Detect buggy property enumeration order in older V8 versions.
  3762. // https://bugs.chromium.org/p/v8/issues/detail?id=4118
  3763. var test1 = new String('abc'); // eslint-disable-line
  3764. test1[5] = 'de';
  3765. if (Object.getOwnPropertyNames(test1)[0] === '5') {
  3766. return false;
  3767. }
  3768. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  3769. var test2 = {};
  3770. for (var i = 0; i < 10; i++) {
  3771. test2['_' + String.fromCharCode(i)] = i;
  3772. }
  3773. var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
  3774. return test2[n];
  3775. });
  3776. if (order2.join('') !== '0123456789') {
  3777. return false;
  3778. }
  3779. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  3780. var test3 = {};
  3781. 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
  3782. test3[letter] = letter;
  3783. });
  3784. if (Object.keys(Object.assign({}, test3)).join('') !==
  3785. 'abcdefghijklmnopqrst') {
  3786. return false;
  3787. }
  3788. return true;
  3789. } catch (e) {
  3790. // We don't expect any of the above to throw, but better to be safe.
  3791. return false;
  3792. }
  3793. }
  3794. module.exports = shouldUseNative() ? Object.assign : function (target, source) {
  3795. var from;
  3796. var to = toObject(target);
  3797. var symbols;
  3798. for (var s = 1; s < arguments.length; s++) {
  3799. from = Object(arguments[s]);
  3800. for (var key in from) {
  3801. if (hasOwnProperty.call(from, key)) {
  3802. to[key] = from[key];
  3803. }
  3804. }
  3805. if (Object.getOwnPropertySymbols) {
  3806. symbols = Object.getOwnPropertySymbols(from);
  3807. for (var i = 0; i < symbols.length; i++) {
  3808. if (propIsEnumerable.call(from, symbols[i])) {
  3809. to[symbols[i]] = from[symbols[i]];
  3810. }
  3811. }
  3812. }
  3813. }
  3814. return to;
  3815. };
  3816. },{}],19:[function(require,module,exports){
  3817. (function (process,Buffer){
  3818. var createHmac = require('create-hmac')
  3819. var checkParameters = require('./precondition')
  3820. exports.pbkdf2 = function (password, salt, iterations, keylen, digest, callback) {
  3821. if (typeof digest === 'function') {
  3822. callback = digest
  3823. digest = undefined
  3824. }
  3825. checkParameters(iterations, keylen)
  3826. if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2')
  3827. setTimeout(function () {
  3828. callback(null, exports.pbkdf2Sync(password, salt, iterations, keylen, digest))
  3829. })
  3830. }
  3831. var defaultEncoding
  3832. if (process.browser) {
  3833. defaultEncoding = 'utf-8'
  3834. } else {
  3835. var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10)
  3836. defaultEncoding = pVersionMajor >= 6 ? 'utf-8' : 'binary'
  3837. }
  3838. exports.pbkdf2Sync = function (password, salt, iterations, keylen, digest) {
  3839. if (!Buffer.isBuffer(password)) password = new Buffer(password, defaultEncoding)
  3840. if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, defaultEncoding)
  3841. checkParameters(iterations, keylen)
  3842. digest = digest || 'sha1'
  3843. var hLen
  3844. var l = 1
  3845. var DK = new Buffer(keylen)
  3846. var block1 = new Buffer(salt.length + 4)
  3847. salt.copy(block1, 0, 0, salt.length)
  3848. var r
  3849. var T
  3850. for (var i = 1; i <= l; i++) {
  3851. block1.writeUInt32BE(i, salt.length)
  3852. var U = createHmac(digest, password).update(block1).digest()
  3853. if (!hLen) {
  3854. hLen = U.length
  3855. T = new Buffer(hLen)
  3856. l = Math.ceil(keylen / hLen)
  3857. r = keylen - (l - 1) * hLen
  3858. }
  3859. U.copy(T, 0, 0, hLen)
  3860. for (var j = 1; j < iterations; j++) {
  3861. U = createHmac(digest, password).update(U).digest()
  3862. for (var k = 0; k < hLen; k++) T[k] ^= U[k]
  3863. }
  3864. var destPos = (i - 1) * hLen
  3865. var len = (i === l ? r : hLen)
  3866. T.copy(DK, destPos, 0, len)
  3867. }
  3868. return DK
  3869. }
  3870. }).call(this,require('_process'),require("buffer").Buffer)
  3871. },{"./precondition":20,"_process":24,"buffer":6,"create-hmac":12}],20:[function(require,module,exports){
  3872. var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs
  3873. module.exports = function (iterations, keylen) {
  3874. if (typeof iterations !== 'number') {
  3875. throw new TypeError('Iterations not a number')
  3876. }
  3877. if (iterations < 0) {
  3878. throw new TypeError('Bad iterations')
  3879. }
  3880. if (typeof keylen !== 'number') {
  3881. throw new TypeError('Key length not a number')
  3882. }
  3883. if (keylen < 0 || keylen > MAX_ALLOC || keylen !== keylen) { /* eslint no-self-compare: 0 */
  3884. throw new TypeError('Bad key length')
  3885. }
  3886. }
  3887. },{}],21:[function(require,module,exports){
  3888. 'use strict';
  3889. module.exports = typeof Promise === 'function' ? Promise : require('pinkie');
  3890. },{"pinkie":22}],22:[function(require,module,exports){
  3891. (function (global){
  3892. 'use strict';
  3893. var PENDING = 'pending';
  3894. var SETTLED = 'settled';
  3895. var FULFILLED = 'fulfilled';
  3896. var REJECTED = 'rejected';
  3897. var NOOP = function () {};
  3898. var isNode = typeof global !== 'undefined' && typeof global.process !== 'undefined' && typeof global.process.emit === 'function';
  3899. var asyncSetTimer = typeof setImmediate === 'undefined' ? setTimeout : setImmediate;
  3900. var asyncQueue = [];
  3901. var asyncTimer;
  3902. function asyncFlush() {
  3903. // run promise callbacks
  3904. for (var i = 0; i < asyncQueue.length; i++) {
  3905. asyncQueue[i][0](asyncQueue[i][1]);
  3906. }
  3907. // reset async asyncQueue
  3908. asyncQueue = [];
  3909. asyncTimer = false;
  3910. }
  3911. function asyncCall(callback, arg) {
  3912. asyncQueue.push([callback, arg]);
  3913. if (!asyncTimer) {
  3914. asyncTimer = true;
  3915. asyncSetTimer(asyncFlush, 0);
  3916. }
  3917. }
  3918. function invokeResolver(resolver, promise) {
  3919. function resolvePromise(value) {
  3920. resolve(promise, value);
  3921. }
  3922. function rejectPromise(reason) {
  3923. reject(promise, reason);
  3924. }
  3925. try {
  3926. resolver(resolvePromise, rejectPromise);
  3927. } catch (e) {
  3928. rejectPromise(e);
  3929. }
  3930. }
  3931. function invokeCallback(subscriber) {
  3932. var owner = subscriber.owner;
  3933. var settled = owner._state;
  3934. var value = owner._data;
  3935. var callback = subscriber[settled];
  3936. var promise = subscriber.then;
  3937. if (typeof callback === 'function') {
  3938. settled = FULFILLED;
  3939. try {
  3940. value = callback(value);
  3941. } catch (e) {
  3942. reject(promise, e);
  3943. }
  3944. }
  3945. if (!handleThenable(promise, value)) {
  3946. if (settled === FULFILLED) {
  3947. resolve(promise, value);
  3948. }
  3949. if (settled === REJECTED) {
  3950. reject(promise, value);
  3951. }
  3952. }
  3953. }
  3954. function handleThenable(promise, value) {
  3955. var resolved;
  3956. try {
  3957. if (promise === value) {
  3958. throw new TypeError('A promises callback cannot return that same promise.');
  3959. }
  3960. if (value && (typeof value === 'function' || typeof value === 'object')) {
  3961. // then should be retrieved only once
  3962. var then = value.then;
  3963. if (typeof then === 'function') {
  3964. then.call(value, function (val) {
  3965. if (!resolved) {
  3966. resolved = true;
  3967. if (value === val) {
  3968. fulfill(promise, val);
  3969. } else {
  3970. resolve(promise, val);
  3971. }
  3972. }
  3973. }, function (reason) {
  3974. if (!resolved) {
  3975. resolved = true;
  3976. reject(promise, reason);
  3977. }
  3978. });
  3979. return true;
  3980. }
  3981. }
  3982. } catch (e) {
  3983. if (!resolved) {
  3984. reject(promise, e);
  3985. }
  3986. return true;
  3987. }
  3988. return false;
  3989. }
  3990. function resolve(promise, value) {
  3991. if (promise === value || !handleThenable(promise, value)) {
  3992. fulfill(promise, value);
  3993. }
  3994. }
  3995. function fulfill(promise, value) {
  3996. if (promise._state === PENDING) {
  3997. promise._state = SETTLED;
  3998. promise._data = value;
  3999. asyncCall(publishFulfillment, promise);
  4000. }
  4001. }
  4002. function reject(promise, reason) {
  4003. if (promise._state === PENDING) {
  4004. promise._state = SETTLED;
  4005. promise._data = reason;
  4006. asyncCall(publishRejection, promise);
  4007. }
  4008. }
  4009. function publish(promise) {
  4010. promise._then = promise._then.forEach(invokeCallback);
  4011. }
  4012. function publishFulfillment(promise) {
  4013. promise._state = FULFILLED;
  4014. publish(promise);
  4015. }
  4016. function publishRejection(promise) {
  4017. promise._state = REJECTED;
  4018. publish(promise);
  4019. if (!promise._handled && isNode) {
  4020. global.process.emit('unhandledRejection', promise._data, promise);
  4021. }
  4022. }
  4023. function notifyRejectionHandled(promise) {
  4024. global.process.emit('rejectionHandled', promise);
  4025. }
  4026. /**
  4027. * @class
  4028. */
  4029. function Promise(resolver) {
  4030. if (typeof resolver !== 'function') {
  4031. throw new TypeError('Promise resolver ' + resolver + ' is not a function');
  4032. }
  4033. if (this instanceof Promise === false) {
  4034. throw new TypeError('Failed to construct \'Promise\': Please use the \'new\' operator, this object constructor cannot be called as a function.');
  4035. }
  4036. this._then = [];
  4037. invokeResolver(resolver, this);
  4038. }
  4039. Promise.prototype = {
  4040. constructor: Promise,
  4041. _state: PENDING,
  4042. _then: null,
  4043. _data: undefined,
  4044. _handled: false,
  4045. then: function (onFulfillment, onRejection) {
  4046. var subscriber = {
  4047. owner: this,
  4048. then: new this.constructor(NOOP),
  4049. fulfilled: onFulfillment,
  4050. rejected: onRejection
  4051. };
  4052. if ((onRejection || onFulfillment) && !this._handled) {
  4053. this._handled = true;
  4054. if (this._state === REJECTED && isNode) {
  4055. asyncCall(notifyRejectionHandled, this);
  4056. }
  4057. }
  4058. if (this._state === FULFILLED || this._state === REJECTED) {
  4059. // already resolved, call callback async
  4060. asyncCall(invokeCallback, subscriber);
  4061. } else {
  4062. // subscribe
  4063. this._then.push(subscriber);
  4064. }
  4065. return subscriber.then;
  4066. },
  4067. catch: function (onRejection) {
  4068. return this.then(null, onRejection);
  4069. }
  4070. };
  4071. Promise.all = function (promises) {
  4072. if (!Array.isArray(promises)) {
  4073. throw new TypeError('You must pass an array to Promise.all().');
  4074. }
  4075. return new Promise(function (resolve, reject) {
  4076. var results = [];
  4077. var remaining = 0;
  4078. function resolver(index) {
  4079. remaining++;
  4080. return function (value) {
  4081. results[index] = value;
  4082. if (!--remaining) {
  4083. resolve(results);
  4084. }
  4085. };
  4086. }
  4087. for (var i = 0, promise; i < promises.length; i++) {
  4088. promise = promises[i];
  4089. if (promise && typeof promise.then === 'function') {
  4090. promise.then(resolver(i), reject);
  4091. } else {
  4092. results[i] = promise;
  4093. }
  4094. }
  4095. if (!remaining) {
  4096. resolve(results);
  4097. }
  4098. });
  4099. };
  4100. Promise.race = function (promises) {
  4101. if (!Array.isArray(promises)) {
  4102. throw new TypeError('You must pass an array to Promise.race().');
  4103. }
  4104. return new Promise(function (resolve, reject) {
  4105. for (var i = 0, promise; i < promises.length; i++) {
  4106. promise = promises[i];
  4107. if (promise && typeof promise.then === 'function') {
  4108. promise.then(resolve, reject);
  4109. } else {
  4110. resolve(promise);
  4111. }
  4112. }
  4113. });
  4114. };
  4115. Promise.resolve = function (value) {
  4116. if (value && typeof value === 'object' && value.constructor === Promise) {
  4117. return value;
  4118. }
  4119. return new Promise(function (resolve) {
  4120. resolve(value);
  4121. });
  4122. };
  4123. Promise.reject = function (reason) {
  4124. return new Promise(function (resolve, reject) {
  4125. reject(reason);
  4126. });
  4127. };
  4128. module.exports = Promise;
  4129. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  4130. },{}],23:[function(require,module,exports){
  4131. (function (process){
  4132. 'use strict';
  4133. if (!process.version ||
  4134. process.version.indexOf('v0.') === 0 ||
  4135. process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
  4136. module.exports = nextTick;
  4137. } else {
  4138. module.exports = process.nextTick;
  4139. }
  4140. function nextTick(fn, arg1, arg2, arg3) {
  4141. if (typeof fn !== 'function') {
  4142. throw new TypeError('"callback" argument must be a function');
  4143. }
  4144. var len = arguments.length;
  4145. var args, i;
  4146. switch (len) {
  4147. case 0:
  4148. case 1:
  4149. return process.nextTick(fn);
  4150. case 2:
  4151. return process.nextTick(function afterTickOne() {
  4152. fn.call(null, arg1);
  4153. });
  4154. case 3:
  4155. return process.nextTick(function afterTickTwo() {
  4156. fn.call(null, arg1, arg2);
  4157. });
  4158. case 4:
  4159. return process.nextTick(function afterTickThree() {
  4160. fn.call(null, arg1, arg2, arg3);
  4161. });
  4162. default:
  4163. args = new Array(len - 1);
  4164. i = 0;
  4165. while (i < args.length) {
  4166. args[i++] = arguments[i];
  4167. }
  4168. return process.nextTick(function afterTick() {
  4169. fn.apply(null, args);
  4170. });
  4171. }
  4172. }
  4173. }).call(this,require('_process'))
  4174. },{"_process":24}],24:[function(require,module,exports){
  4175. // shim for using process in browser
  4176. var process = module.exports = {};
  4177. // cached from whatever global is present so that test runners that stub it
  4178. // don't break things. But we need to wrap it in a try catch in case it is
  4179. // wrapped in strict mode code which doesn't define any globals. It's inside a
  4180. // function because try/catches deoptimize in certain engines.
  4181. var cachedSetTimeout;
  4182. var cachedClearTimeout;
  4183. function defaultSetTimout() {
  4184. throw new Error('setTimeout has not been defined');
  4185. }
  4186. function defaultClearTimeout () {
  4187. throw new Error('clearTimeout has not been defined');
  4188. }
  4189. (function () {
  4190. try {
  4191. if (typeof setTimeout === 'function') {
  4192. cachedSetTimeout = setTimeout;
  4193. } else {
  4194. cachedSetTimeout = defaultSetTimout;
  4195. }
  4196. } catch (e) {
  4197. cachedSetTimeout = defaultSetTimout;
  4198. }
  4199. try {
  4200. if (typeof clearTimeout === 'function') {
  4201. cachedClearTimeout = clearTimeout;
  4202. } else {
  4203. cachedClearTimeout = defaultClearTimeout;
  4204. }
  4205. } catch (e) {
  4206. cachedClearTimeout = defaultClearTimeout;
  4207. }
  4208. } ())
  4209. function runTimeout(fun) {
  4210. if (cachedSetTimeout === setTimeout) {
  4211. //normal enviroments in sane situations
  4212. return setTimeout(fun, 0);
  4213. }
  4214. // if setTimeout wasn't available but was latter defined
  4215. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  4216. cachedSetTimeout = setTimeout;
  4217. return setTimeout(fun, 0);
  4218. }
  4219. try {
  4220. // when when somebody has screwed with setTimeout but no I.E. maddness
  4221. return cachedSetTimeout(fun, 0);
  4222. } catch(e){
  4223. try {
  4224. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  4225. return cachedSetTimeout.call(null, fun, 0);
  4226. } catch(e){
  4227. // 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
  4228. return cachedSetTimeout.call(this, fun, 0);
  4229. }
  4230. }
  4231. }
  4232. function runClearTimeout(marker) {
  4233. if (cachedClearTimeout === clearTimeout) {
  4234. //normal enviroments in sane situations
  4235. return clearTimeout(marker);
  4236. }
  4237. // if clearTimeout wasn't available but was latter defined
  4238. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  4239. cachedClearTimeout = clearTimeout;
  4240. return clearTimeout(marker);
  4241. }
  4242. try {
  4243. // when when somebody has screwed with setTimeout but no I.E. maddness
  4244. return cachedClearTimeout(marker);
  4245. } catch (e){
  4246. try {
  4247. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  4248. return cachedClearTimeout.call(null, marker);
  4249. } catch (e){
  4250. // 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.
  4251. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  4252. return cachedClearTimeout.call(this, marker);
  4253. }
  4254. }
  4255. }
  4256. var queue = [];
  4257. var draining = false;
  4258. var currentQueue;
  4259. var queueIndex = -1;
  4260. function cleanUpNextTick() {
  4261. if (!draining || !currentQueue) {
  4262. return;
  4263. }
  4264. draining = false;
  4265. if (currentQueue.length) {
  4266. queue = currentQueue.concat(queue);
  4267. } else {
  4268. queueIndex = -1;
  4269. }
  4270. if (queue.length) {
  4271. drainQueue();
  4272. }
  4273. }
  4274. function drainQueue() {
  4275. if (draining) {
  4276. return;
  4277. }
  4278. var timeout = runTimeout(cleanUpNextTick);
  4279. draining = true;
  4280. var len = queue.length;
  4281. while(len) {
  4282. currentQueue = queue;
  4283. queue = [];
  4284. while (++queueIndex < len) {
  4285. if (currentQueue) {
  4286. currentQueue[queueIndex].run();
  4287. }
  4288. }
  4289. queueIndex = -1;
  4290. len = queue.length;
  4291. }
  4292. currentQueue = null;
  4293. draining = false;
  4294. runClearTimeout(timeout);
  4295. }
  4296. process.nextTick = function (fun) {
  4297. var args = new Array(arguments.length - 1);
  4298. if (arguments.length > 1) {
  4299. for (var i = 1; i < arguments.length; i++) {
  4300. args[i - 1] = arguments[i];
  4301. }
  4302. }
  4303. queue.push(new Item(fun, args));
  4304. if (queue.length === 1 && !draining) {
  4305. runTimeout(drainQueue);
  4306. }
  4307. };
  4308. // v8 likes predictible objects
  4309. function Item(fun, array) {
  4310. this.fun = fun;
  4311. this.array = array;
  4312. }
  4313. Item.prototype.run = function () {
  4314. this.fun.apply(null, this.array);
  4315. };
  4316. process.title = 'browser';
  4317. process.browser = true;
  4318. process.env = {};
  4319. process.argv = [];
  4320. process.version = ''; // empty string to avoid regexp issues
  4321. process.versions = {};
  4322. function noop() {}
  4323. process.on = noop;
  4324. process.addListener = noop;
  4325. process.once = noop;
  4326. process.off = noop;
  4327. process.removeListener = noop;
  4328. process.removeAllListeners = noop;
  4329. process.emit = noop;
  4330. process.binding = function (name) {
  4331. throw new Error('process.binding is not supported');
  4332. };
  4333. process.cwd = function () { return '/' };
  4334. process.chdir = function (dir) {
  4335. throw new Error('process.chdir is not supported');
  4336. };
  4337. process.umask = function() { return 0; };
  4338. },{}],25:[function(require,module,exports){
  4339. module.exports = require("./lib/_stream_duplex.js")
  4340. },{"./lib/_stream_duplex.js":26}],26:[function(require,module,exports){
  4341. // a duplex stream is just a stream that is both readable and writable.
  4342. // Since JS doesn't have multiple prototypal inheritance, this class
  4343. // prototypally inherits from Readable, and then parasitically from
  4344. // Writable.
  4345. 'use strict';
  4346. /*<replacement>*/
  4347. var objectKeys = Object.keys || function (obj) {
  4348. var keys = [];
  4349. for (var key in obj) {
  4350. keys.push(key);
  4351. }return keys;
  4352. };
  4353. /*</replacement>*/
  4354. module.exports = Duplex;
  4355. /*<replacement>*/
  4356. var processNextTick = require('process-nextick-args');
  4357. /*</replacement>*/
  4358. /*<replacement>*/
  4359. var util = require('core-util-is');
  4360. util.inherits = require('inherits');
  4361. /*</replacement>*/
  4362. var Readable = require('./_stream_readable');
  4363. var Writable = require('./_stream_writable');
  4364. util.inherits(Duplex, Readable);
  4365. var keys = objectKeys(Writable.prototype);
  4366. for (var v = 0; v < keys.length; v++) {
  4367. var method = keys[v];
  4368. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  4369. }
  4370. function Duplex(options) {
  4371. if (!(this instanceof Duplex)) return new Duplex(options);
  4372. Readable.call(this, options);
  4373. Writable.call(this, options);
  4374. if (options && options.readable === false) this.readable = false;
  4375. if (options && options.writable === false) this.writable = false;
  4376. this.allowHalfOpen = true;
  4377. if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
  4378. this.once('end', onend);
  4379. }
  4380. // the no-half-open enforcer
  4381. function onend() {
  4382. // if we allow half-open state, or if the writable side ended,
  4383. // then we're ok.
  4384. if (this.allowHalfOpen || this._writableState.ended) return;
  4385. // no more data can be written.
  4386. // But allow more writes to happen in this tick.
  4387. processNextTick(onEndNT, this);
  4388. }
  4389. function onEndNT(self) {
  4390. self.end();
  4391. }
  4392. function forEach(xs, f) {
  4393. for (var i = 0, l = xs.length; i < l; i++) {
  4394. f(xs[i], i);
  4395. }
  4396. }
  4397. },{"./_stream_readable":28,"./_stream_writable":30,"core-util-is":8,"inherits":15,"process-nextick-args":23}],27:[function(require,module,exports){
  4398. // a passthrough stream.
  4399. // basically just the most minimal sort of Transform stream.
  4400. // Every written chunk gets output as-is.
  4401. 'use strict';
  4402. module.exports = PassThrough;
  4403. var Transform = require('./_stream_transform');
  4404. /*<replacement>*/
  4405. var util = require('core-util-is');
  4406. util.inherits = require('inherits');
  4407. /*</replacement>*/
  4408. util.inherits(PassThrough, Transform);
  4409. function PassThrough(options) {
  4410. if (!(this instanceof PassThrough)) return new PassThrough(options);
  4411. Transform.call(this, options);
  4412. }
  4413. PassThrough.prototype._transform = function (chunk, encoding, cb) {
  4414. cb(null, chunk);
  4415. };
  4416. },{"./_stream_transform":29,"core-util-is":8,"inherits":15}],28:[function(require,module,exports){
  4417. (function (process){
  4418. 'use strict';
  4419. module.exports = Readable;
  4420. /*<replacement>*/
  4421. var processNextTick = require('process-nextick-args');
  4422. /*</replacement>*/
  4423. /*<replacement>*/
  4424. var isArray = require('isarray');
  4425. /*</replacement>*/
  4426. /*<replacement>*/
  4427. var Duplex;
  4428. /*</replacement>*/
  4429. Readable.ReadableState = ReadableState;
  4430. /*<replacement>*/
  4431. var EE = require('events').EventEmitter;
  4432. var EElistenerCount = function (emitter, type) {
  4433. return emitter.listeners(type).length;
  4434. };
  4435. /*</replacement>*/
  4436. /*<replacement>*/
  4437. var Stream;
  4438. (function () {
  4439. try {
  4440. Stream = require('st' + 'ream');
  4441. } catch (_) {} finally {
  4442. if (!Stream) Stream = require('events').EventEmitter;
  4443. }
  4444. })();
  4445. /*</replacement>*/
  4446. var Buffer = require('buffer').Buffer;
  4447. /*<replacement>*/
  4448. var bufferShim = require('buffer-shims');
  4449. /*</replacement>*/
  4450. /*<replacement>*/
  4451. var util = require('core-util-is');
  4452. util.inherits = require('inherits');
  4453. /*</replacement>*/
  4454. /*<replacement>*/
  4455. var debugUtil = require('util');
  4456. var debug = void 0;
  4457. if (debugUtil && debugUtil.debuglog) {
  4458. debug = debugUtil.debuglog('stream');
  4459. } else {
  4460. debug = function () {};
  4461. }
  4462. /*</replacement>*/
  4463. var BufferList = require('./internal/streams/BufferList');
  4464. var StringDecoder;
  4465. util.inherits(Readable, Stream);
  4466. function prependListener(emitter, event, fn) {
  4467. // Sadly this is not cacheable as some libraries bundle their own
  4468. // event emitter implementation with them.
  4469. if (typeof emitter.prependListener === 'function') {
  4470. return emitter.prependListener(event, fn);
  4471. } else {
  4472. // This is a hack to make sure that our error handler is attached before any
  4473. // userland ones. NEVER DO THIS. This is here only because this code needs
  4474. // to continue to work with older versions of Node.js that do not include
  4475. // the prependListener() method. The goal is to eventually remove this hack.
  4476. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
  4477. }
  4478. }
  4479. function ReadableState(options, stream) {
  4480. Duplex = Duplex || require('./_stream_duplex');
  4481. options = options || {};
  4482. // object stream flag. Used to make read(n) ignore n and to
  4483. // make all the buffer merging and length checks go away
  4484. this.objectMode = !!options.objectMode;
  4485. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
  4486. // the point at which it stops calling _read() to fill the buffer
  4487. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  4488. var hwm = options.highWaterMark;
  4489. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  4490. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  4491. // cast to ints.
  4492. this.highWaterMark = ~ ~this.highWaterMark;
  4493. // A linked list is used to store data chunks instead of an array because the
  4494. // linked list can remove elements from the beginning faster than
  4495. // array.shift()
  4496. this.buffer = new BufferList();
  4497. this.length = 0;
  4498. this.pipes = null;
  4499. this.pipesCount = 0;
  4500. this.flowing = null;
  4501. this.ended = false;
  4502. this.endEmitted = false;
  4503. this.reading = false;
  4504. // a flag to be able to tell if the onwrite cb is called immediately,
  4505. // or on a later tick. We set this to true at first, because any
  4506. // actions that shouldn't happen until "later" should generally also
  4507. // not happen before the first write call.
  4508. this.sync = true;
  4509. // whenever we return null, then we set a flag to say
  4510. // that we're awaiting a 'readable' event emission.
  4511. this.needReadable = false;
  4512. this.emittedReadable = false;
  4513. this.readableListening = false;
  4514. this.resumeScheduled = false;
  4515. // Crypto is kind of old and crusty. Historically, its default string
  4516. // encoding is 'binary' so we have to make this configurable.
  4517. // Everything else in the universe uses 'utf8', though.
  4518. this.defaultEncoding = options.defaultEncoding || 'utf8';
  4519. // when piping, we only care about 'readable' events that happen
  4520. // after read()ing all the bytes and not getting any pushback.
  4521. this.ranOut = false;
  4522. // the number of writers that are awaiting a drain event in .pipe()s
  4523. this.awaitDrain = 0;
  4524. // if true, a maybeReadMore has been scheduled
  4525. this.readingMore = false;
  4526. this.decoder = null;
  4527. this.encoding = null;
  4528. if (options.encoding) {
  4529. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  4530. this.decoder = new StringDecoder(options.encoding);
  4531. this.encoding = options.encoding;
  4532. }
  4533. }
  4534. function Readable(options) {
  4535. Duplex = Duplex || require('./_stream_duplex');
  4536. if (!(this instanceof Readable)) return new Readable(options);
  4537. this._readableState = new ReadableState(options, this);
  4538. // legacy
  4539. this.readable = true;
  4540. if (options && typeof options.read === 'function') this._read = options.read;
  4541. Stream.call(this);
  4542. }
  4543. // Manually shove something into the read() buffer.
  4544. // This returns true if the highWaterMark has not been hit yet,
  4545. // similar to how Writable.write() returns true if you should
  4546. // write() some more.
  4547. Readable.prototype.push = function (chunk, encoding) {
  4548. var state = this._readableState;
  4549. if (!state.objectMode && typeof chunk === 'string') {
  4550. encoding = encoding || state.defaultEncoding;
  4551. if (encoding !== state.encoding) {
  4552. chunk = bufferShim.from(chunk, encoding);
  4553. encoding = '';
  4554. }
  4555. }
  4556. return readableAddChunk(this, state, chunk, encoding, false);
  4557. };
  4558. // Unshift should *always* be something directly out of read()
  4559. Readable.prototype.unshift = function (chunk) {
  4560. var state = this._readableState;
  4561. return readableAddChunk(this, state, chunk, '', true);
  4562. };
  4563. Readable.prototype.isPaused = function () {
  4564. return this._readableState.flowing === false;
  4565. };
  4566. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  4567. var er = chunkInvalid(state, chunk);
  4568. if (er) {
  4569. stream.emit('error', er);
  4570. } else if (chunk === null) {
  4571. state.reading = false;
  4572. onEofChunk(stream, state);
  4573. } else if (state.objectMode || chunk && chunk.length > 0) {
  4574. if (state.ended && !addToFront) {
  4575. var e = new Error('stream.push() after EOF');
  4576. stream.emit('error', e);
  4577. } else if (state.endEmitted && addToFront) {
  4578. var _e = new Error('stream.unshift() after end event');
  4579. stream.emit('error', _e);
  4580. } else {
  4581. var skipAdd;
  4582. if (state.decoder && !addToFront && !encoding) {
  4583. chunk = state.decoder.write(chunk);
  4584. skipAdd = !state.objectMode && chunk.length === 0;
  4585. }
  4586. if (!addToFront) state.reading = false;
  4587. // Don't add to the buffer if we've decoded to an empty string chunk and
  4588. // we're not in object mode
  4589. if (!skipAdd) {
  4590. // if we want the data now, just emit it.
  4591. if (state.flowing && state.length === 0 && !state.sync) {
  4592. stream.emit('data', chunk);
  4593. stream.read(0);
  4594. } else {
  4595. // update the buffer info.
  4596. state.length += state.objectMode ? 1 : chunk.length;
  4597. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  4598. if (state.needReadable) emitReadable(stream);
  4599. }
  4600. }
  4601. maybeReadMore(stream, state);
  4602. }
  4603. } else if (!addToFront) {
  4604. state.reading = false;
  4605. }
  4606. return needMoreData(state);
  4607. }
  4608. // if it's past the high water mark, we can push in some more.
  4609. // Also, if we have no data yet, we can stand some
  4610. // more bytes. This is to work around cases where hwm=0,
  4611. // such as the repl. Also, if the push() triggered a
  4612. // readable event, and the user called read(largeNumber) such that
  4613. // needReadable was set, then we ought to push more, so that another
  4614. // 'readable' event will be triggered.
  4615. function needMoreData(state) {
  4616. return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
  4617. }
  4618. // backwards compatibility.
  4619. Readable.prototype.setEncoding = function (enc) {
  4620. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  4621. this._readableState.decoder = new StringDecoder(enc);
  4622. this._readableState.encoding = enc;
  4623. return this;
  4624. };
  4625. // Don't raise the hwm > 8MB
  4626. var MAX_HWM = 0x800000;
  4627. function computeNewHighWaterMark(n) {
  4628. if (n >= MAX_HWM) {
  4629. n = MAX_HWM;
  4630. } else {
  4631. // Get the next highest power of 2 to prevent increasing hwm excessively in
  4632. // tiny amounts
  4633. n--;
  4634. n |= n >>> 1;
  4635. n |= n >>> 2;
  4636. n |= n >>> 4;
  4637. n |= n >>> 8;
  4638. n |= n >>> 16;
  4639. n++;
  4640. }
  4641. return n;
  4642. }
  4643. // This function is designed to be inlinable, so please take care when making
  4644. // changes to the function body.
  4645. function howMuchToRead(n, state) {
  4646. if (n <= 0 || state.length === 0 && state.ended) return 0;
  4647. if (state.objectMode) return 1;
  4648. if (n !== n) {
  4649. // Only flow one buffer at a time
  4650. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  4651. }
  4652. // If we're asking for more than the current hwm, then raise the hwm.
  4653. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  4654. if (n <= state.length) return n;
  4655. // Don't have enough
  4656. if (!state.ended) {
  4657. state.needReadable = true;
  4658. return 0;
  4659. }
  4660. return state.length;
  4661. }
  4662. // you can override either this method, or the async _read(n) below.
  4663. Readable.prototype.read = function (n) {
  4664. debug('read', n);
  4665. n = parseInt(n, 10);
  4666. var state = this._readableState;
  4667. var nOrig = n;
  4668. if (n !== 0) state.emittedReadable = false;
  4669. // if we're doing read(0) to trigger a readable event, but we
  4670. // already have a bunch of data in the buffer, then just trigger
  4671. // the 'readable' event and move on.
  4672. if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
  4673. debug('read: emitReadable', state.length, state.ended);
  4674. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  4675. return null;
  4676. }
  4677. n = howMuchToRead(n, state);
  4678. // if we've ended, and we're now clear, then finish it up.
  4679. if (n === 0 && state.ended) {
  4680. if (state.length === 0) endReadable(this);
  4681. return null;
  4682. }
  4683. // All the actual chunk generation logic needs to be
  4684. // *below* the call to _read. The reason is that in certain
  4685. // synthetic stream cases, such as passthrough streams, _read
  4686. // may be a completely synchronous operation which may change
  4687. // the state of the read buffer, providing enough data when
  4688. // before there was *not* enough.
  4689. //
  4690. // So, the steps are:
  4691. // 1. Figure out what the state of things will be after we do
  4692. // a read from the buffer.
  4693. //
  4694. // 2. If that resulting state will trigger a _read, then call _read.
  4695. // Note that this may be asynchronous, or synchronous. Yes, it is
  4696. // deeply ugly to write APIs this way, but that still doesn't mean
  4697. // that the Readable class should behave improperly, as streams are
  4698. // designed to be sync/async agnostic.
  4699. // Take note if the _read call is sync or async (ie, if the read call
  4700. // has returned yet), so that we know whether or not it's safe to emit
  4701. // 'readable' etc.
  4702. //
  4703. // 3. Actually pull the requested chunks out of the buffer and return.
  4704. // if we need a readable event, then we need to do some reading.
  4705. var doRead = state.needReadable;
  4706. debug('need readable', doRead);
  4707. // if we currently have less than the highWaterMark, then also read some
  4708. if (state.length === 0 || state.length - n < state.highWaterMark) {
  4709. doRead = true;
  4710. debug('length less than watermark', doRead);
  4711. }
  4712. // however, if we've ended, then there's no point, and if we're already
  4713. // reading, then it's unnecessary.
  4714. if (state.ended || state.reading) {
  4715. doRead = false;
  4716. debug('reading or ended', doRead);
  4717. } else if (doRead) {
  4718. debug('do read');
  4719. state.reading = true;
  4720. state.sync = true;
  4721. // if the length is currently zero, then we *need* a readable event.
  4722. if (state.length === 0) state.needReadable = true;
  4723. // call internal read method
  4724. this._read(state.highWaterMark);
  4725. state.sync = false;
  4726. // If _read pushed data synchronously, then `reading` will be false,
  4727. // and we need to re-evaluate how much data we can return to the user.
  4728. if (!state.reading) n = howMuchToRead(nOrig, state);
  4729. }
  4730. var ret;
  4731. if (n > 0) ret = fromList(n, state);else ret = null;
  4732. if (ret === null) {
  4733. state.needReadable = true;
  4734. n = 0;
  4735. } else {
  4736. state.length -= n;
  4737. }
  4738. if (state.length === 0) {
  4739. // If we have nothing in the buffer, then we want to know
  4740. // as soon as we *do* get something into the buffer.
  4741. if (!state.ended) state.needReadable = true;
  4742. // If we tried to read() past the EOF, then emit end on the next tick.
  4743. if (nOrig !== n && state.ended) endReadable(this);
  4744. }
  4745. if (ret !== null) this.emit('data', ret);
  4746. return ret;
  4747. };
  4748. function chunkInvalid(state, chunk) {
  4749. var er = null;
  4750. if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
  4751. er = new TypeError('Invalid non-string/buffer chunk');
  4752. }
  4753. return er;
  4754. }
  4755. function onEofChunk(stream, state) {
  4756. if (state.ended) return;
  4757. if (state.decoder) {
  4758. var chunk = state.decoder.end();
  4759. if (chunk && chunk.length) {
  4760. state.buffer.push(chunk);
  4761. state.length += state.objectMode ? 1 : chunk.length;
  4762. }
  4763. }
  4764. state.ended = true;
  4765. // emit 'readable' now to make sure it gets picked up.
  4766. emitReadable(stream);
  4767. }
  4768. // Don't emit readable right away in sync mode, because this can trigger
  4769. // another read() call => stack overflow. This way, it might trigger
  4770. // a nextTick recursion warning, but that's not so bad.
  4771. function emitReadable(stream) {
  4772. var state = stream._readableState;
  4773. state.needReadable = false;
  4774. if (!state.emittedReadable) {
  4775. debug('emitReadable', state.flowing);
  4776. state.emittedReadable = true;
  4777. if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream);
  4778. }
  4779. }
  4780. function emitReadable_(stream) {
  4781. debug('emit readable');
  4782. stream.emit('readable');
  4783. flow(stream);
  4784. }
  4785. // at this point, the user has presumably seen the 'readable' event,
  4786. // and called read() to consume some data. that may have triggered
  4787. // in turn another _read(n) call, in which case reading = true if
  4788. // it's in progress.
  4789. // However, if we're not ended, or reading, and the length < hwm,
  4790. // then go ahead and try to read some more preemptively.
  4791. function maybeReadMore(stream, state) {
  4792. if (!state.readingMore) {
  4793. state.readingMore = true;
  4794. processNextTick(maybeReadMore_, stream, state);
  4795. }
  4796. }
  4797. function maybeReadMore_(stream, state) {
  4798. var len = state.length;
  4799. while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
  4800. debug('maybeReadMore read 0');
  4801. stream.read(0);
  4802. if (len === state.length)
  4803. // didn't get any data, stop spinning.
  4804. break;else len = state.length;
  4805. }
  4806. state.readingMore = false;
  4807. }
  4808. // abstract method. to be overridden in specific implementation classes.
  4809. // call cb(er, data) where data is <= n in length.
  4810. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  4811. // arbitrary, and perhaps not very meaningful.
  4812. Readable.prototype._read = function (n) {
  4813. this.emit('error', new Error('_read() is not implemented'));
  4814. };
  4815. Readable.prototype.pipe = function (dest, pipeOpts) {
  4816. var src = this;
  4817. var state = this._readableState;
  4818. switch (state.pipesCount) {
  4819. case 0:
  4820. state.pipes = dest;
  4821. break;
  4822. case 1:
  4823. state.pipes = [state.pipes, dest];
  4824. break;
  4825. default:
  4826. state.pipes.push(dest);
  4827. break;
  4828. }
  4829. state.pipesCount += 1;
  4830. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  4831. var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
  4832. var endFn = doEnd ? onend : cleanup;
  4833. if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn);
  4834. dest.on('unpipe', onunpipe);
  4835. function onunpipe(readable) {
  4836. debug('onunpipe');
  4837. if (readable === src) {
  4838. cleanup();
  4839. }
  4840. }
  4841. function onend() {
  4842. debug('onend');
  4843. dest.end();
  4844. }
  4845. // when the dest drains, it reduces the awaitDrain counter
  4846. // on the source. This would be more elegant with a .once()
  4847. // handler in flow(), but adding and removing repeatedly is
  4848. // too slow.
  4849. var ondrain = pipeOnDrain(src);
  4850. dest.on('drain', ondrain);
  4851. var cleanedUp = false;
  4852. function cleanup() {
  4853. debug('cleanup');
  4854. // cleanup event handlers once the pipe is broken
  4855. dest.removeListener('close', onclose);
  4856. dest.removeListener('finish', onfinish);
  4857. dest.removeListener('drain', ondrain);
  4858. dest.removeListener('error', onerror);
  4859. dest.removeListener('unpipe', onunpipe);
  4860. src.removeListener('end', onend);
  4861. src.removeListener('end', cleanup);
  4862. src.removeListener('data', ondata);
  4863. cleanedUp = true;
  4864. // if the reader is waiting for a drain event from this
  4865. // specific writer, then it would cause it to never start
  4866. // flowing again.
  4867. // So, if this is awaiting a drain, then we just call it now.
  4868. // If we don't know, then assume that we are waiting for one.
  4869. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  4870. }
  4871. // If the user pushes more data while we're writing to dest then we'll end up
  4872. // in ondata again. However, we only want to increase awaitDrain once because
  4873. // dest will only emit one 'drain' event for the multiple writes.
  4874. // => Introduce a guard on increasing awaitDrain.
  4875. var increasedAwaitDrain = false;
  4876. src.on('data', ondata);
  4877. function ondata(chunk) {
  4878. debug('ondata');
  4879. increasedAwaitDrain = false;
  4880. var ret = dest.write(chunk);
  4881. if (false === ret && !increasedAwaitDrain) {
  4882. // If the user unpiped during `dest.write()`, it is possible
  4883. // to get stuck in a permanently paused state if that write
  4884. // also returned false.
  4885. // => Check whether `dest` is still a piping destination.
  4886. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  4887. debug('false write response, pause', src._readableState.awaitDrain);
  4888. src._readableState.awaitDrain++;
  4889. increasedAwaitDrain = true;
  4890. }
  4891. src.pause();
  4892. }
  4893. }
  4894. // if the dest has an error, then stop piping into it.
  4895. // however, don't suppress the throwing behavior for this.
  4896. function onerror(er) {
  4897. debug('onerror', er);
  4898. unpipe();
  4899. dest.removeListener('error', onerror);
  4900. if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
  4901. }
  4902. // Make sure our error handler is attached before userland ones.
  4903. prependListener(dest, 'error', onerror);
  4904. // Both close and finish should trigger unpipe, but only once.
  4905. function onclose() {
  4906. dest.removeListener('finish', onfinish);
  4907. unpipe();
  4908. }
  4909. dest.once('close', onclose);
  4910. function onfinish() {
  4911. debug('onfinish');
  4912. dest.removeListener('close', onclose);
  4913. unpipe();
  4914. }
  4915. dest.once('finish', onfinish);
  4916. function unpipe() {
  4917. debug('unpipe');
  4918. src.unpipe(dest);
  4919. }
  4920. // tell the dest that it's being piped to
  4921. dest.emit('pipe', src);
  4922. // start the flow if it hasn't been started already.
  4923. if (!state.flowing) {
  4924. debug('pipe resume');
  4925. src.resume();
  4926. }
  4927. return dest;
  4928. };
  4929. function pipeOnDrain(src) {
  4930. return function () {
  4931. var state = src._readableState;
  4932. debug('pipeOnDrain', state.awaitDrain);
  4933. if (state.awaitDrain) state.awaitDrain--;
  4934. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  4935. state.flowing = true;
  4936. flow(src);
  4937. }
  4938. };
  4939. }
  4940. Readable.prototype.unpipe = function (dest) {
  4941. var state = this._readableState;
  4942. // if we're not piping anywhere, then do nothing.
  4943. if (state.pipesCount === 0) return this;
  4944. // just one destination. most common case.
  4945. if (state.pipesCount === 1) {
  4946. // passed in one, but it's not the right one.
  4947. if (dest && dest !== state.pipes) return this;
  4948. if (!dest) dest = state.pipes;
  4949. // got a match.
  4950. state.pipes = null;
  4951. state.pipesCount = 0;
  4952. state.flowing = false;
  4953. if (dest) dest.emit('unpipe', this);
  4954. return this;
  4955. }
  4956. // slow case. multiple pipe destinations.
  4957. if (!dest) {
  4958. // remove all.
  4959. var dests = state.pipes;
  4960. var len = state.pipesCount;
  4961. state.pipes = null;
  4962. state.pipesCount = 0;
  4963. state.flowing = false;
  4964. for (var i = 0; i < len; i++) {
  4965. dests[i].emit('unpipe', this);
  4966. }return this;
  4967. }
  4968. // try to find the right one.
  4969. var index = indexOf(state.pipes, dest);
  4970. if (index === -1) return this;
  4971. state.pipes.splice(index, 1);
  4972. state.pipesCount -= 1;
  4973. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  4974. dest.emit('unpipe', this);
  4975. return this;
  4976. };
  4977. // set up data events if they are asked for
  4978. // Ensure readable listeners eventually get something
  4979. Readable.prototype.on = function (ev, fn) {
  4980. var res = Stream.prototype.on.call(this, ev, fn);
  4981. if (ev === 'data') {
  4982. // Start flowing on next tick if stream isn't explicitly paused
  4983. if (this._readableState.flowing !== false) this.resume();
  4984. } else if (ev === 'readable') {
  4985. var state = this._readableState;
  4986. if (!state.endEmitted && !state.readableListening) {
  4987. state.readableListening = state.needReadable = true;
  4988. state.emittedReadable = false;
  4989. if (!state.reading) {
  4990. processNextTick(nReadingNextTick, this);
  4991. } else if (state.length) {
  4992. emitReadable(this, state);
  4993. }
  4994. }
  4995. }
  4996. return res;
  4997. };
  4998. Readable.prototype.addListener = Readable.prototype.on;
  4999. function nReadingNextTick(self) {
  5000. debug('readable nexttick read 0');
  5001. self.read(0);
  5002. }
  5003. // pause() and resume() are remnants of the legacy readable stream API
  5004. // If the user uses them, then switch into old mode.
  5005. Readable.prototype.resume = function () {
  5006. var state = this._readableState;
  5007. if (!state.flowing) {
  5008. debug('resume');
  5009. state.flowing = true;
  5010. resume(this, state);
  5011. }
  5012. return this;
  5013. };
  5014. function resume(stream, state) {
  5015. if (!state.resumeScheduled) {
  5016. state.resumeScheduled = true;
  5017. processNextTick(resume_, stream, state);
  5018. }
  5019. }
  5020. function resume_(stream, state) {
  5021. if (!state.reading) {
  5022. debug('resume read 0');
  5023. stream.read(0);
  5024. }
  5025. state.resumeScheduled = false;
  5026. state.awaitDrain = 0;
  5027. stream.emit('resume');
  5028. flow(stream);
  5029. if (state.flowing && !state.reading) stream.read(0);
  5030. }
  5031. Readable.prototype.pause = function () {
  5032. debug('call pause flowing=%j', this._readableState.flowing);
  5033. if (false !== this._readableState.flowing) {
  5034. debug('pause');
  5035. this._readableState.flowing = false;
  5036. this.emit('pause');
  5037. }
  5038. return this;
  5039. };
  5040. function flow(stream) {
  5041. var state = stream._readableState;
  5042. debug('flow', state.flowing);
  5043. while (state.flowing && stream.read() !== null) {}
  5044. }
  5045. // wrap an old-style stream as the async data source.
  5046. // This is *not* part of the readable stream interface.
  5047. // It is an ugly unfortunate mess of history.
  5048. Readable.prototype.wrap = function (stream) {
  5049. var state = this._readableState;
  5050. var paused = false;
  5051. var self = this;
  5052. stream.on('end', function () {
  5053. debug('wrapped end');
  5054. if (state.decoder && !state.ended) {
  5055. var chunk = state.decoder.end();
  5056. if (chunk && chunk.length) self.push(chunk);
  5057. }
  5058. self.push(null);
  5059. });
  5060. stream.on('data', function (chunk) {
  5061. debug('wrapped data');
  5062. if (state.decoder) chunk = state.decoder.write(chunk);
  5063. // don't skip over falsy values in objectMode
  5064. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  5065. var ret = self.push(chunk);
  5066. if (!ret) {
  5067. paused = true;
  5068. stream.pause();
  5069. }
  5070. });
  5071. // proxy all the other methods.
  5072. // important when wrapping filters and duplexes.
  5073. for (var i in stream) {
  5074. if (this[i] === undefined && typeof stream[i] === 'function') {
  5075. this[i] = function (method) {
  5076. return function () {
  5077. return stream[method].apply(stream, arguments);
  5078. };
  5079. }(i);
  5080. }
  5081. }
  5082. // proxy certain important events.
  5083. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  5084. forEach(events, function (ev) {
  5085. stream.on(ev, self.emit.bind(self, ev));
  5086. });
  5087. // when we try to consume some more bytes, simply unpause the
  5088. // underlying stream.
  5089. self._read = function (n) {
  5090. debug('wrapped _read', n);
  5091. if (paused) {
  5092. paused = false;
  5093. stream.resume();
  5094. }
  5095. };
  5096. return self;
  5097. };
  5098. // exposed for testing purposes only.
  5099. Readable._fromList = fromList;
  5100. // Pluck off n bytes from an array of buffers.
  5101. // Length is the combined lengths of all the buffers in the list.
  5102. // This function is designed to be inlinable, so please take care when making
  5103. // changes to the function body.
  5104. function fromList(n, state) {
  5105. // nothing buffered
  5106. if (state.length === 0) return null;
  5107. var ret;
  5108. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  5109. // read it all, truncate the list
  5110. 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);
  5111. state.buffer.clear();
  5112. } else {
  5113. // read part of list
  5114. ret = fromListPartial(n, state.buffer, state.decoder);
  5115. }
  5116. return ret;
  5117. }
  5118. // Extracts only enough buffered data to satisfy the amount requested.
  5119. // This function is designed to be inlinable, so please take care when making
  5120. // changes to the function body.
  5121. function fromListPartial(n, list, hasStrings) {
  5122. var ret;
  5123. if (n < list.head.data.length) {
  5124. // slice is the same for buffers and strings
  5125. ret = list.head.data.slice(0, n);
  5126. list.head.data = list.head.data.slice(n);
  5127. } else if (n === list.head.data.length) {
  5128. // first chunk is a perfect match
  5129. ret = list.shift();
  5130. } else {
  5131. // result spans more than one buffer
  5132. ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
  5133. }
  5134. return ret;
  5135. }
  5136. // Copies a specified amount of characters from the list of buffered data
  5137. // chunks.
  5138. // This function is designed to be inlinable, so please take care when making
  5139. // changes to the function body.
  5140. function copyFromBufferString(n, list) {
  5141. var p = list.head;
  5142. var c = 1;
  5143. var ret = p.data;
  5144. n -= ret.length;
  5145. while (p = p.next) {
  5146. var str = p.data;
  5147. var nb = n > str.length ? str.length : n;
  5148. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  5149. n -= nb;
  5150. if (n === 0) {
  5151. if (nb === str.length) {
  5152. ++c;
  5153. if (p.next) list.head = p.next;else list.head = list.tail = null;
  5154. } else {
  5155. list.head = p;
  5156. p.data = str.slice(nb);
  5157. }
  5158. break;
  5159. }
  5160. ++c;
  5161. }
  5162. list.length -= c;
  5163. return ret;
  5164. }
  5165. // Copies a specified amount of bytes from the list of buffered data chunks.
  5166. // This function is designed to be inlinable, so please take care when making
  5167. // changes to the function body.
  5168. function copyFromBuffer(n, list) {
  5169. var ret = bufferShim.allocUnsafe(n);
  5170. var p = list.head;
  5171. var c = 1;
  5172. p.data.copy(ret);
  5173. n -= p.data.length;
  5174. while (p = p.next) {
  5175. var buf = p.data;
  5176. var nb = n > buf.length ? buf.length : n;
  5177. buf.copy(ret, ret.length - n, 0, nb);
  5178. n -= nb;
  5179. if (n === 0) {
  5180. if (nb === buf.length) {
  5181. ++c;
  5182. if (p.next) list.head = p.next;else list.head = list.tail = null;
  5183. } else {
  5184. list.head = p;
  5185. p.data = buf.slice(nb);
  5186. }
  5187. break;
  5188. }
  5189. ++c;
  5190. }
  5191. list.length -= c;
  5192. return ret;
  5193. }
  5194. function endReadable(stream) {
  5195. var state = stream._readableState;
  5196. // If we get here before consuming all the bytes, then that is a
  5197. // bug in node. Should never happen.
  5198. if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
  5199. if (!state.endEmitted) {
  5200. state.ended = true;
  5201. processNextTick(endReadableNT, state, stream);
  5202. }
  5203. }
  5204. function endReadableNT(state, stream) {
  5205. // Check that we didn't get one last unshift.
  5206. if (!state.endEmitted && state.length === 0) {
  5207. state.endEmitted = true;
  5208. stream.readable = false;
  5209. stream.emit('end');
  5210. }
  5211. }
  5212. function forEach(xs, f) {
  5213. for (var i = 0, l = xs.length; i < l; i++) {
  5214. f(xs[i], i);
  5215. }
  5216. }
  5217. function indexOf(xs, x) {
  5218. for (var i = 0, l = xs.length; i < l; i++) {
  5219. if (xs[i] === x) return i;
  5220. }
  5221. return -1;
  5222. }
  5223. }).call(this,require('_process'))
  5224. },{"./_stream_duplex":26,"./internal/streams/BufferList":31,"_process":24,"buffer":6,"buffer-shims":5,"core-util-is":8,"events":13,"inherits":15,"isarray":17,"process-nextick-args":23,"string_decoder/":46,"util":4}],29:[function(require,module,exports){
  5225. // a transform stream is a readable/writable stream where you do
  5226. // something with the data. Sometimes it's called a "filter",
  5227. // but that's not a great name for it, since that implies a thing where
  5228. // some bits pass through, and others are simply ignored. (That would
  5229. // be a valid example of a transform, of course.)
  5230. //
  5231. // While the output is causally related to the input, it's not a
  5232. // necessarily symmetric or synchronous transformation. For example,
  5233. // a zlib stream might take multiple plain-text writes(), and then
  5234. // emit a single compressed chunk some time in the future.
  5235. //
  5236. // Here's how this works:
  5237. //
  5238. // The Transform stream has all the aspects of the readable and writable
  5239. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  5240. // internally, and returns false if there's a lot of pending writes
  5241. // buffered up. When you call read(), that calls _read(n) until
  5242. // there's enough pending readable data buffered up.
  5243. //
  5244. // In a transform stream, the written data is placed in a buffer. When
  5245. // _read(n) is called, it transforms the queued up data, calling the
  5246. // buffered _write cb's as it consumes chunks. If consuming a single
  5247. // written chunk would result in multiple output chunks, then the first
  5248. // outputted bit calls the readcb, and subsequent chunks just go into
  5249. // the read buffer, and will cause it to emit 'readable' if necessary.
  5250. //
  5251. // This way, back-pressure is actually determined by the reading side,
  5252. // since _read has to be called to start processing a new chunk. However,
  5253. // a pathological inflate type of transform can cause excessive buffering
  5254. // here. For example, imagine a stream where every byte of input is
  5255. // interpreted as an integer from 0-255, and then results in that many
  5256. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  5257. // 1kb of data being output. In this case, you could write a very small
  5258. // amount of input, and end up with a very large amount of output. In
  5259. // such a pathological inflating mechanism, there'd be no way to tell
  5260. // the system to stop doing the transform. A single 4MB write could
  5261. // cause the system to run out of memory.
  5262. //
  5263. // However, even in such a pathological case, only a single written chunk
  5264. // would be consumed, and then the rest would wait (un-transformed) until
  5265. // the results of the previous transformed chunk were consumed.
  5266. 'use strict';
  5267. module.exports = Transform;
  5268. var Duplex = require('./_stream_duplex');
  5269. /*<replacement>*/
  5270. var util = require('core-util-is');
  5271. util.inherits = require('inherits');
  5272. /*</replacement>*/
  5273. util.inherits(Transform, Duplex);
  5274. function TransformState(stream) {
  5275. this.afterTransform = function (er, data) {
  5276. return afterTransform(stream, er, data);
  5277. };
  5278. this.needTransform = false;
  5279. this.transforming = false;
  5280. this.writecb = null;
  5281. this.writechunk = null;
  5282. this.writeencoding = null;
  5283. }
  5284. function afterTransform(stream, er, data) {
  5285. var ts = stream._transformState;
  5286. ts.transforming = false;
  5287. var cb = ts.writecb;
  5288. if (!cb) return stream.emit('error', new Error('no writecb in Transform class'));
  5289. ts.writechunk = null;
  5290. ts.writecb = null;
  5291. if (data !== null && data !== undefined) stream.push(data);
  5292. cb(er);
  5293. var rs = stream._readableState;
  5294. rs.reading = false;
  5295. if (rs.needReadable || rs.length < rs.highWaterMark) {
  5296. stream._read(rs.highWaterMark);
  5297. }
  5298. }
  5299. function Transform(options) {
  5300. if (!(this instanceof Transform)) return new Transform(options);
  5301. Duplex.call(this, options);
  5302. this._transformState = new TransformState(this);
  5303. var stream = this;
  5304. // start out asking for a readable event once data is transformed.
  5305. this._readableState.needReadable = true;
  5306. // we have implemented the _read method, and done the other things
  5307. // that Readable wants before the first _read call, so unset the
  5308. // sync guard flag.
  5309. this._readableState.sync = false;
  5310. if (options) {
  5311. if (typeof options.transform === 'function') this._transform = options.transform;
  5312. if (typeof options.flush === 'function') this._flush = options.flush;
  5313. }
  5314. // When the writable side finishes, then flush out anything remaining.
  5315. this.once('prefinish', function () {
  5316. if (typeof this._flush === 'function') this._flush(function (er, data) {
  5317. done(stream, er, data);
  5318. });else done(stream);
  5319. });
  5320. }
  5321. Transform.prototype.push = function (chunk, encoding) {
  5322. this._transformState.needTransform = false;
  5323. return Duplex.prototype.push.call(this, chunk, encoding);
  5324. };
  5325. // This is the part where you do stuff!
  5326. // override this function in implementation classes.
  5327. // 'chunk' is an input chunk.
  5328. //
  5329. // Call `push(newChunk)` to pass along transformed output
  5330. // to the readable side. You may call 'push' zero or more times.
  5331. //
  5332. // Call `cb(err)` when you are done with this chunk. If you pass
  5333. // an error, then that'll put the hurt on the whole operation. If you
  5334. // never call cb(), then you'll never get another chunk.
  5335. Transform.prototype._transform = function (chunk, encoding, cb) {
  5336. throw new Error('_transform() is not implemented');
  5337. };
  5338. Transform.prototype._write = function (chunk, encoding, cb) {
  5339. var ts = this._transformState;
  5340. ts.writecb = cb;
  5341. ts.writechunk = chunk;
  5342. ts.writeencoding = encoding;
  5343. if (!ts.transforming) {
  5344. var rs = this._readableState;
  5345. if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
  5346. }
  5347. };
  5348. // Doesn't matter what the args are here.
  5349. // _transform does all the work.
  5350. // That we got here means that the readable side wants more data.
  5351. Transform.prototype._read = function (n) {
  5352. var ts = this._transformState;
  5353. if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
  5354. ts.transforming = true;
  5355. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  5356. } else {
  5357. // mark that we need a transform, so that any data that comes in
  5358. // will get processed, now that we've asked for it.
  5359. ts.needTransform = true;
  5360. }
  5361. };
  5362. function done(stream, er, data) {
  5363. if (er) return stream.emit('error', er);
  5364. if (data !== null && data !== undefined) stream.push(data);
  5365. // if there's nothing in the write buffer, then that means
  5366. // that nothing more will ever be provided
  5367. var ws = stream._writableState;
  5368. var ts = stream._transformState;
  5369. if (ws.length) throw new Error('Calling transform done when ws.length != 0');
  5370. if (ts.transforming) throw new Error('Calling transform done when still transforming');
  5371. return stream.push(null);
  5372. }
  5373. },{"./_stream_duplex":26,"core-util-is":8,"inherits":15}],30:[function(require,module,exports){
  5374. (function (process){
  5375. // A bit simpler than readable streams.
  5376. // Implement an async ._write(chunk, encoding, cb), and it'll handle all
  5377. // the drain event emission and buffering.
  5378. 'use strict';
  5379. module.exports = Writable;
  5380. /*<replacement>*/
  5381. var processNextTick = require('process-nextick-args');
  5382. /*</replacement>*/
  5383. /*<replacement>*/
  5384. var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick;
  5385. /*</replacement>*/
  5386. /*<replacement>*/
  5387. var Duplex;
  5388. /*</replacement>*/
  5389. Writable.WritableState = WritableState;
  5390. /*<replacement>*/
  5391. var util = require('core-util-is');
  5392. util.inherits = require('inherits');
  5393. /*</replacement>*/
  5394. /*<replacement>*/
  5395. var internalUtil = {
  5396. deprecate: require('util-deprecate')
  5397. };
  5398. /*</replacement>*/
  5399. /*<replacement>*/
  5400. var Stream;
  5401. (function () {
  5402. try {
  5403. Stream = require('st' + 'ream');
  5404. } catch (_) {} finally {
  5405. if (!Stream) Stream = require('events').EventEmitter;
  5406. }
  5407. })();
  5408. /*</replacement>*/
  5409. var Buffer = require('buffer').Buffer;
  5410. /*<replacement>*/
  5411. var bufferShim = require('buffer-shims');
  5412. /*</replacement>*/
  5413. util.inherits(Writable, Stream);
  5414. function nop() {}
  5415. function WriteReq(chunk, encoding, cb) {
  5416. this.chunk = chunk;
  5417. this.encoding = encoding;
  5418. this.callback = cb;
  5419. this.next = null;
  5420. }
  5421. function WritableState(options, stream) {
  5422. Duplex = Duplex || require('./_stream_duplex');
  5423. options = options || {};
  5424. // object stream flag to indicate whether or not this stream
  5425. // contains buffers or objects.
  5426. this.objectMode = !!options.objectMode;
  5427. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode;
  5428. // the point at which write() starts returning false
  5429. // Note: 0 is a valid value, means that we always return false if
  5430. // the entire buffer is not flushed immediately on write()
  5431. var hwm = options.highWaterMark;
  5432. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  5433. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  5434. // cast to ints.
  5435. this.highWaterMark = ~ ~this.highWaterMark;
  5436. // drain event flag.
  5437. this.needDrain = false;
  5438. // at the start of calling end()
  5439. this.ending = false;
  5440. // when end() has been called, and returned
  5441. this.ended = false;
  5442. // when 'finish' is emitted
  5443. this.finished = false;
  5444. // should we decode strings into buffers before passing to _write?
  5445. // this is here so that some node-core streams can optimize string
  5446. // handling at a lower level.
  5447. var noDecode = options.decodeStrings === false;
  5448. this.decodeStrings = !noDecode;
  5449. // Crypto is kind of old and crusty. Historically, its default string
  5450. // encoding is 'binary' so we have to make this configurable.
  5451. // Everything else in the universe uses 'utf8', though.
  5452. this.defaultEncoding = options.defaultEncoding || 'utf8';
  5453. // not an actual buffer we keep track of, but a measurement
  5454. // of how much we're waiting to get pushed to some underlying
  5455. // socket or file.
  5456. this.length = 0;
  5457. // a flag to see when we're in the middle of a write.
  5458. this.writing = false;
  5459. // when true all writes will be buffered until .uncork() call
  5460. this.corked = 0;
  5461. // a flag to be able to tell if the onwrite cb is called immediately,
  5462. // or on a later tick. We set this to true at first, because any
  5463. // actions that shouldn't happen until "later" should generally also
  5464. // not happen before the first write call.
  5465. this.sync = true;
  5466. // a flag to know if we're processing previously buffered items, which
  5467. // may call the _write() callback in the same tick, so that we don't
  5468. // end up in an overlapped onwrite situation.
  5469. this.bufferProcessing = false;
  5470. // the callback that's passed to _write(chunk,cb)
  5471. this.onwrite = function (er) {
  5472. onwrite(stream, er);
  5473. };
  5474. // the callback that the user supplies to write(chunk,encoding,cb)
  5475. this.writecb = null;
  5476. // the amount that is being written when _write is called.
  5477. this.writelen = 0;
  5478. this.bufferedRequest = null;
  5479. this.lastBufferedRequest = null;
  5480. // number of pending user-supplied write callbacks
  5481. // this must be 0 before 'finish' can be emitted
  5482. this.pendingcb = 0;
  5483. // emit prefinish if the only thing we're waiting for is _write cbs
  5484. // This is relevant for synchronous Transform streams
  5485. this.prefinished = false;
  5486. // True if the error was already emitted and should not be thrown again
  5487. this.errorEmitted = false;
  5488. // count buffered requests
  5489. this.bufferedRequestCount = 0;
  5490. // allocate the first CorkedRequest, there is always
  5491. // one allocated and free to use, and we maintain at most two
  5492. this.corkedRequestsFree = new CorkedRequest(this);
  5493. }
  5494. WritableState.prototype.getBuffer = function getBuffer() {
  5495. var current = this.bufferedRequest;
  5496. var out = [];
  5497. while (current) {
  5498. out.push(current);
  5499. current = current.next;
  5500. }
  5501. return out;
  5502. };
  5503. (function () {
  5504. try {
  5505. Object.defineProperty(WritableState.prototype, 'buffer', {
  5506. get: internalUtil.deprecate(function () {
  5507. return this.getBuffer();
  5508. }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.')
  5509. });
  5510. } catch (_) {}
  5511. })();
  5512. // Test _writableState for inheritance to account for Duplex streams,
  5513. // whose prototype chain only points to Readable.
  5514. var realHasInstance;
  5515. if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
  5516. realHasInstance = Function.prototype[Symbol.hasInstance];
  5517. Object.defineProperty(Writable, Symbol.hasInstance, {
  5518. value: function (object) {
  5519. if (realHasInstance.call(this, object)) return true;
  5520. return object && object._writableState instanceof WritableState;
  5521. }
  5522. });
  5523. } else {
  5524. realHasInstance = function (object) {
  5525. return object instanceof this;
  5526. };
  5527. }
  5528. function Writable(options) {
  5529. Duplex = Duplex || require('./_stream_duplex');
  5530. // Writable ctor is applied to Duplexes, too.
  5531. // `realHasInstance` is necessary because using plain `instanceof`
  5532. // would return false, as no `_writableState` property is attached.
  5533. // Trying to use the custom `instanceof` for Writable here will also break the
  5534. // Node.js LazyTransform implementation, which has a non-trivial getter for
  5535. // `_writableState` that would lead to infinite recursion.
  5536. if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) {
  5537. return new Writable(options);
  5538. }
  5539. this._writableState = new WritableState(options, this);
  5540. // legacy.
  5541. this.writable = true;
  5542. if (options) {
  5543. if (typeof options.write === 'function') this._write = options.write;
  5544. if (typeof options.writev === 'function') this._writev = options.writev;
  5545. }
  5546. Stream.call(this);
  5547. }
  5548. // Otherwise people can pipe Writable streams, which is just wrong.
  5549. Writable.prototype.pipe = function () {
  5550. this.emit('error', new Error('Cannot pipe, not readable'));
  5551. };
  5552. function writeAfterEnd(stream, cb) {
  5553. var er = new Error('write after end');
  5554. // TODO: defer error events consistently everywhere, not just the cb
  5555. stream.emit('error', er);
  5556. processNextTick(cb, er);
  5557. }
  5558. // If we get something that is not a buffer, string, null, or undefined,
  5559. // and we're not in objectMode, then that's an error.
  5560. // Otherwise stream chunks are all considered to be of length=1, and the
  5561. // watermarks determine how many objects to keep in the buffer, rather than
  5562. // how many bytes or characters.
  5563. function validChunk(stream, state, chunk, cb) {
  5564. var valid = true;
  5565. var er = false;
  5566. // Always throw error if a null is written
  5567. // if we are not in object mode then throw
  5568. // if it is not a buffer, string, or undefined.
  5569. if (chunk === null) {
  5570. er = new TypeError('May not write null values to stream');
  5571. } else if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
  5572. er = new TypeError('Invalid non-string/buffer chunk');
  5573. }
  5574. if (er) {
  5575. stream.emit('error', er);
  5576. processNextTick(cb, er);
  5577. valid = false;
  5578. }
  5579. return valid;
  5580. }
  5581. Writable.prototype.write = function (chunk, encoding, cb) {
  5582. var state = this._writableState;
  5583. var ret = false;
  5584. if (typeof encoding === 'function') {
  5585. cb = encoding;
  5586. encoding = null;
  5587. }
  5588. if (Buffer.isBuffer(chunk)) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
  5589. if (typeof cb !== 'function') cb = nop;
  5590. if (state.ended) writeAfterEnd(this, cb);else if (validChunk(this, state, chunk, cb)) {
  5591. state.pendingcb++;
  5592. ret = writeOrBuffer(this, state, chunk, encoding, cb);
  5593. }
  5594. return ret;
  5595. };
  5596. Writable.prototype.cork = function () {
  5597. var state = this._writableState;
  5598. state.corked++;
  5599. };
  5600. Writable.prototype.uncork = function () {
  5601. var state = this._writableState;
  5602. if (state.corked) {
  5603. state.corked--;
  5604. if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
  5605. }
  5606. };
  5607. Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
  5608. // node::ParseEncoding() requires lower case.
  5609. if (typeof encoding === 'string') encoding = encoding.toLowerCase();
  5610. 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);
  5611. this._writableState.defaultEncoding = encoding;
  5612. return this;
  5613. };
  5614. function decodeChunk(state, chunk, encoding) {
  5615. if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
  5616. chunk = bufferShim.from(chunk, encoding);
  5617. }
  5618. return chunk;
  5619. }
  5620. // if we're already writing something, then just put this
  5621. // in the queue, and wait our turn. Otherwise, call _write
  5622. // If we return false, then we need a drain event, so set that flag.
  5623. function writeOrBuffer(stream, state, chunk, encoding, cb) {
  5624. chunk = decodeChunk(state, chunk, encoding);
  5625. if (Buffer.isBuffer(chunk)) encoding = 'buffer';
  5626. var len = state.objectMode ? 1 : chunk.length;
  5627. state.length += len;
  5628. var ret = state.length < state.highWaterMark;
  5629. // we must ensure that previous needDrain will not be reset to false.
  5630. if (!ret) state.needDrain = true;
  5631. if (state.writing || state.corked) {
  5632. var last = state.lastBufferedRequest;
  5633. state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
  5634. if (last) {
  5635. last.next = state.lastBufferedRequest;
  5636. } else {
  5637. state.bufferedRequest = state.lastBufferedRequest;
  5638. }
  5639. state.bufferedRequestCount += 1;
  5640. } else {
  5641. doWrite(stream, state, false, len, chunk, encoding, cb);
  5642. }
  5643. return ret;
  5644. }
  5645. function doWrite(stream, state, writev, len, chunk, encoding, cb) {
  5646. state.writelen = len;
  5647. state.writecb = cb;
  5648. state.writing = true;
  5649. state.sync = true;
  5650. if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
  5651. state.sync = false;
  5652. }
  5653. function onwriteError(stream, state, sync, er, cb) {
  5654. --state.pendingcb;
  5655. if (sync) processNextTick(cb, er);else cb(er);
  5656. stream._writableState.errorEmitted = true;
  5657. stream.emit('error', er);
  5658. }
  5659. function onwriteStateUpdate(state) {
  5660. state.writing = false;
  5661. state.writecb = null;
  5662. state.length -= state.writelen;
  5663. state.writelen = 0;
  5664. }
  5665. function onwrite(stream, er) {
  5666. var state = stream._writableState;
  5667. var sync = state.sync;
  5668. var cb = state.writecb;
  5669. onwriteStateUpdate(state);
  5670. if (er) onwriteError(stream, state, sync, er, cb);else {
  5671. // Check if we're actually ready to finish, but don't emit yet
  5672. var finished = needFinish(state);
  5673. if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
  5674. clearBuffer(stream, state);
  5675. }
  5676. if (sync) {
  5677. /*<replacement>*/
  5678. asyncWrite(afterWrite, stream, state, finished, cb);
  5679. /*</replacement>*/
  5680. } else {
  5681. afterWrite(stream, state, finished, cb);
  5682. }
  5683. }
  5684. }
  5685. function afterWrite(stream, state, finished, cb) {
  5686. if (!finished) onwriteDrain(stream, state);
  5687. state.pendingcb--;
  5688. cb();
  5689. finishMaybe(stream, state);
  5690. }
  5691. // Must force callback to be called on nextTick, so that we don't
  5692. // emit 'drain' before the write() consumer gets the 'false' return
  5693. // value, and has a chance to attach a 'drain' listener.
  5694. function onwriteDrain(stream, state) {
  5695. if (state.length === 0 && state.needDrain) {
  5696. state.needDrain = false;
  5697. stream.emit('drain');
  5698. }
  5699. }
  5700. // if there's something in the buffer waiting, then process it
  5701. function clearBuffer(stream, state) {
  5702. state.bufferProcessing = true;
  5703. var entry = state.bufferedRequest;
  5704. if (stream._writev && entry && entry.next) {
  5705. // Fast case, write everything using _writev()
  5706. var l = state.bufferedRequestCount;
  5707. var buffer = new Array(l);
  5708. var holder = state.corkedRequestsFree;
  5709. holder.entry = entry;
  5710. var count = 0;
  5711. while (entry) {
  5712. buffer[count] = entry;
  5713. entry = entry.next;
  5714. count += 1;
  5715. }
  5716. doWrite(stream, state, true, state.length, buffer, '', holder.finish);
  5717. // doWrite is almost always async, defer these to save a bit of time
  5718. // as the hot path ends with doWrite
  5719. state.pendingcb++;
  5720. state.lastBufferedRequest = null;
  5721. if (holder.next) {
  5722. state.corkedRequestsFree = holder.next;
  5723. holder.next = null;
  5724. } else {
  5725. state.corkedRequestsFree = new CorkedRequest(state);
  5726. }
  5727. } else {
  5728. // Slow case, write chunks one-by-one
  5729. while (entry) {
  5730. var chunk = entry.chunk;
  5731. var encoding = entry.encoding;
  5732. var cb = entry.callback;
  5733. var len = state.objectMode ? 1 : chunk.length;
  5734. doWrite(stream, state, false, len, chunk, encoding, cb);
  5735. entry = entry.next;
  5736. // if we didn't call the onwrite immediately, then
  5737. // it means that we need to wait until it does.
  5738. // also, that means that the chunk and cb are currently
  5739. // being processed, so move the buffer counter past them.
  5740. if (state.writing) {
  5741. break;
  5742. }
  5743. }
  5744. if (entry === null) state.lastBufferedRequest = null;
  5745. }
  5746. state.bufferedRequestCount = 0;
  5747. state.bufferedRequest = entry;
  5748. state.bufferProcessing = false;
  5749. }
  5750. Writable.prototype._write = function (chunk, encoding, cb) {
  5751. cb(new Error('_write() is not implemented'));
  5752. };
  5753. Writable.prototype._writev = null;
  5754. Writable.prototype.end = function (chunk, encoding, cb) {
  5755. var state = this._writableState;
  5756. if (typeof chunk === 'function') {
  5757. cb = chunk;
  5758. chunk = null;
  5759. encoding = null;
  5760. } else if (typeof encoding === 'function') {
  5761. cb = encoding;
  5762. encoding = null;
  5763. }
  5764. if (chunk !== null && chunk !== undefined) this.write(chunk, encoding);
  5765. // .end() fully uncorks
  5766. if (state.corked) {
  5767. state.corked = 1;
  5768. this.uncork();
  5769. }
  5770. // ignore unnecessary end() calls.
  5771. if (!state.ending && !state.finished) endWritable(this, state, cb);
  5772. };
  5773. function needFinish(state) {
  5774. return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
  5775. }
  5776. function prefinish(stream, state) {
  5777. if (!state.prefinished) {
  5778. state.prefinished = true;
  5779. stream.emit('prefinish');
  5780. }
  5781. }
  5782. function finishMaybe(stream, state) {
  5783. var need = needFinish(state);
  5784. if (need) {
  5785. if (state.pendingcb === 0) {
  5786. prefinish(stream, state);
  5787. state.finished = true;
  5788. stream.emit('finish');
  5789. } else {
  5790. prefinish(stream, state);
  5791. }
  5792. }
  5793. return need;
  5794. }
  5795. function endWritable(stream, state, cb) {
  5796. state.ending = true;
  5797. finishMaybe(stream, state);
  5798. if (cb) {
  5799. if (state.finished) processNextTick(cb);else stream.once('finish', cb);
  5800. }
  5801. state.ended = true;
  5802. stream.writable = false;
  5803. }
  5804. // It seems a linked list but it is not
  5805. // there will be only 2 of these for each stream
  5806. function CorkedRequest(state) {
  5807. var _this = this;
  5808. this.next = null;
  5809. this.entry = null;
  5810. this.finish = function (err) {
  5811. var entry = _this.entry;
  5812. _this.entry = null;
  5813. while (entry) {
  5814. var cb = entry.callback;
  5815. state.pendingcb--;
  5816. cb(err);
  5817. entry = entry.next;
  5818. }
  5819. if (state.corkedRequestsFree) {
  5820. state.corkedRequestsFree.next = _this;
  5821. } else {
  5822. state.corkedRequestsFree = _this;
  5823. }
  5824. };
  5825. }
  5826. }).call(this,require('_process'))
  5827. },{"./_stream_duplex":26,"_process":24,"buffer":6,"buffer-shims":5,"core-util-is":8,"events":13,"inherits":15,"process-nextick-args":23,"util-deprecate":47}],31:[function(require,module,exports){
  5828. 'use strict';
  5829. var Buffer = require('buffer').Buffer;
  5830. /*<replacement>*/
  5831. var bufferShim = require('buffer-shims');
  5832. /*</replacement>*/
  5833. module.exports = BufferList;
  5834. function BufferList() {
  5835. this.head = null;
  5836. this.tail = null;
  5837. this.length = 0;
  5838. }
  5839. BufferList.prototype.push = function (v) {
  5840. var entry = { data: v, next: null };
  5841. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  5842. this.tail = entry;
  5843. ++this.length;
  5844. };
  5845. BufferList.prototype.unshift = function (v) {
  5846. var entry = { data: v, next: this.head };
  5847. if (this.length === 0) this.tail = entry;
  5848. this.head = entry;
  5849. ++this.length;
  5850. };
  5851. BufferList.prototype.shift = function () {
  5852. if (this.length === 0) return;
  5853. var ret = this.head.data;
  5854. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  5855. --this.length;
  5856. return ret;
  5857. };
  5858. BufferList.prototype.clear = function () {
  5859. this.head = this.tail = null;
  5860. this.length = 0;
  5861. };
  5862. BufferList.prototype.join = function (s) {
  5863. if (this.length === 0) return '';
  5864. var p = this.head;
  5865. var ret = '' + p.data;
  5866. while (p = p.next) {
  5867. ret += s + p.data;
  5868. }return ret;
  5869. };
  5870. BufferList.prototype.concat = function (n) {
  5871. if (this.length === 0) return bufferShim.alloc(0);
  5872. if (this.length === 1) return this.head.data;
  5873. var ret = bufferShim.allocUnsafe(n >>> 0);
  5874. var p = this.head;
  5875. var i = 0;
  5876. while (p) {
  5877. p.data.copy(ret, i);
  5878. i += p.data.length;
  5879. p = p.next;
  5880. }
  5881. return ret;
  5882. };
  5883. },{"buffer":6,"buffer-shims":5}],32:[function(require,module,exports){
  5884. module.exports = require("./lib/_stream_passthrough.js")
  5885. },{"./lib/_stream_passthrough.js":27}],33:[function(require,module,exports){
  5886. (function (process){
  5887. var Stream = (function (){
  5888. try {
  5889. return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify
  5890. } catch(_){}
  5891. }());
  5892. exports = module.exports = require('./lib/_stream_readable.js');
  5893. exports.Stream = Stream || exports;
  5894. exports.Readable = exports;
  5895. exports.Writable = require('./lib/_stream_writable.js');
  5896. exports.Duplex = require('./lib/_stream_duplex.js');
  5897. exports.Transform = require('./lib/_stream_transform.js');
  5898. exports.PassThrough = require('./lib/_stream_passthrough.js');
  5899. if (!process.browser && process.env.READABLE_STREAM === 'disable' && Stream) {
  5900. module.exports = Stream;
  5901. }
  5902. }).call(this,require('_process'))
  5903. },{"./lib/_stream_duplex.js":26,"./lib/_stream_passthrough.js":27,"./lib/_stream_readable.js":28,"./lib/_stream_transform.js":29,"./lib/_stream_writable.js":30,"_process":24}],34:[function(require,module,exports){
  5904. module.exports = require("./lib/_stream_transform.js")
  5905. },{"./lib/_stream_transform.js":29}],35:[function(require,module,exports){
  5906. module.exports = require("./lib/_stream_writable.js")
  5907. },{"./lib/_stream_writable.js":30}],36:[function(require,module,exports){
  5908. (function (Buffer){
  5909. /*
  5910. CryptoJS v3.1.2
  5911. code.google.com/p/crypto-js
  5912. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5913. code.google.com/p/crypto-js/wiki/License
  5914. */
  5915. /** @preserve
  5916. (c) 2012 by Cédric Mesnil. All rights reserved.
  5917. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5918. - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5919. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  5920. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5921. */
  5922. // constants table
  5923. var zl = [
  5924. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  5925. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  5926. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  5927. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  5928. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  5929. ]
  5930. var zr = [
  5931. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  5932. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  5933. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  5934. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  5935. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  5936. ]
  5937. var sl = [
  5938. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  5939. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  5940. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  5941. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  5942. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  5943. ]
  5944. var sr = [
  5945. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  5946. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  5947. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  5948. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  5949. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  5950. ]
  5951. var hl = [0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]
  5952. var hr = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]
  5953. function bytesToWords (bytes) {
  5954. var words = []
  5955. for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
  5956. words[b >>> 5] |= bytes[i] << (24 - b % 32)
  5957. }
  5958. return words
  5959. }
  5960. function wordsToBytes (words) {
  5961. var bytes = []
  5962. for (var b = 0; b < words.length * 32; b += 8) {
  5963. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
  5964. }
  5965. return bytes
  5966. }
  5967. function processBlock (H, M, offset) {
  5968. // swap endian
  5969. for (var i = 0; i < 16; i++) {
  5970. var offset_i = offset + i
  5971. var M_offset_i = M[offset_i]
  5972. // Swap
  5973. M[offset_i] = (
  5974. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
  5975. (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  5976. )
  5977. }
  5978. // Working variables
  5979. var al, bl, cl, dl, el
  5980. var ar, br, cr, dr, er
  5981. ar = al = H[0]
  5982. br = bl = H[1]
  5983. cr = cl = H[2]
  5984. dr = dl = H[3]
  5985. er = el = H[4]
  5986. // computation
  5987. var t
  5988. for (i = 0; i < 80; i += 1) {
  5989. t = (al + M[offset + zl[i]]) | 0
  5990. if (i < 16) {
  5991. t += f1(bl, cl, dl) + hl[0]
  5992. } else if (i < 32) {
  5993. t += f2(bl, cl, dl) + hl[1]
  5994. } else if (i < 48) {
  5995. t += f3(bl, cl, dl) + hl[2]
  5996. } else if (i < 64) {
  5997. t += f4(bl, cl, dl) + hl[3]
  5998. } else {// if (i<80) {
  5999. t += f5(bl, cl, dl) + hl[4]
  6000. }
  6001. t = t | 0
  6002. t = rotl(t, sl[i])
  6003. t = (t + el) | 0
  6004. al = el
  6005. el = dl
  6006. dl = rotl(cl, 10)
  6007. cl = bl
  6008. bl = t
  6009. t = (ar + M[offset + zr[i]]) | 0
  6010. if (i < 16) {
  6011. t += f5(br, cr, dr) + hr[0]
  6012. } else if (i < 32) {
  6013. t += f4(br, cr, dr) + hr[1]
  6014. } else if (i < 48) {
  6015. t += f3(br, cr, dr) + hr[2]
  6016. } else if (i < 64) {
  6017. t += f2(br, cr, dr) + hr[3]
  6018. } else {// if (i<80) {
  6019. t += f1(br, cr, dr) + hr[4]
  6020. }
  6021. t = t | 0
  6022. t = rotl(t, sr[i])
  6023. t = (t + er) | 0
  6024. ar = er
  6025. er = dr
  6026. dr = rotl(cr, 10)
  6027. cr = br
  6028. br = t
  6029. }
  6030. // intermediate hash value
  6031. t = (H[1] + cl + dr) | 0
  6032. H[1] = (H[2] + dl + er) | 0
  6033. H[2] = (H[3] + el + ar) | 0
  6034. H[3] = (H[4] + al + br) | 0
  6035. H[4] = (H[0] + bl + cr) | 0
  6036. H[0] = t
  6037. }
  6038. function f1 (x, y, z) {
  6039. return ((x) ^ (y) ^ (z))
  6040. }
  6041. function f2 (x, y, z) {
  6042. return (((x) & (y)) | ((~x) & (z)))
  6043. }
  6044. function f3 (x, y, z) {
  6045. return (((x) | (~(y))) ^ (z))
  6046. }
  6047. function f4 (x, y, z) {
  6048. return (((x) & (z)) | ((y) & (~(z))))
  6049. }
  6050. function f5 (x, y, z) {
  6051. return ((x) ^ ((y) | (~(z))))
  6052. }
  6053. function rotl (x, n) {
  6054. return (x << n) | (x >>> (32 - n))
  6055. }
  6056. function ripemd160 (message) {
  6057. var H = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]
  6058. if (typeof message === 'string') {
  6059. message = new Buffer(message, 'utf8')
  6060. }
  6061. var m = bytesToWords(message)
  6062. var nBitsLeft = message.length * 8
  6063. var nBitsTotal = message.length * 8
  6064. // Add padding
  6065. m[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32)
  6066. m[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  6067. (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
  6068. (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
  6069. )
  6070. for (var i = 0; i < m.length; i += 16) {
  6071. processBlock(H, m, i)
  6072. }
  6073. // swap endian
  6074. for (i = 0; i < 5; i++) {
  6075. // shortcut
  6076. var H_i = H[i]
  6077. // Swap
  6078. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  6079. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00)
  6080. }
  6081. var digestbytes = wordsToBytes(H)
  6082. return new Buffer(digestbytes)
  6083. }
  6084. module.exports = ripemd160
  6085. }).call(this,require("buffer").Buffer)
  6086. },{"buffer":6}],37:[function(require,module,exports){
  6087. (function (Buffer){
  6088. // prototype class for hash functions
  6089. function Hash (blockSize, finalSize) {
  6090. this._block = new Buffer(blockSize)
  6091. this._finalSize = finalSize
  6092. this._blockSize = blockSize
  6093. this._len = 0
  6094. this._s = 0
  6095. }
  6096. Hash.prototype.update = function (data, enc) {
  6097. if (typeof data === 'string') {
  6098. enc = enc || 'utf8'
  6099. data = new Buffer(data, enc)
  6100. }
  6101. var l = this._len += data.length
  6102. var s = this._s || 0
  6103. var f = 0
  6104. var buffer = this._block
  6105. while (s < l) {
  6106. var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize))
  6107. var ch = (t - f)
  6108. for (var i = 0; i < ch; i++) {
  6109. buffer[(s % this._blockSize) + i] = data[i + f]
  6110. }
  6111. s += ch
  6112. f += ch
  6113. if ((s % this._blockSize) === 0) {
  6114. this._update(buffer)
  6115. }
  6116. }
  6117. this._s = s
  6118. return this
  6119. }
  6120. Hash.prototype.digest = function (enc) {
  6121. // Suppose the length of the message M, in bits, is l
  6122. var l = this._len * 8
  6123. // Append the bit 1 to the end of the message
  6124. this._block[this._len % this._blockSize] = 0x80
  6125. // and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
  6126. this._block.fill(0, this._len % this._blockSize + 1)
  6127. if (l % (this._blockSize * 8) >= this._finalSize * 8) {
  6128. this._update(this._block)
  6129. this._block.fill(0)
  6130. }
  6131. // to this append the block which is equal to the number l written in binary
  6132. // TODO: handle case where l is > Math.pow(2, 29)
  6133. this._block.writeInt32BE(l, this._blockSize - 4)
  6134. var hash = this._update(this._block) || this._hash()
  6135. return enc ? hash.toString(enc) : hash
  6136. }
  6137. Hash.prototype._update = function () {
  6138. throw new Error('_update must be implemented by subclass')
  6139. }
  6140. module.exports = Hash
  6141. }).call(this,require("buffer").Buffer)
  6142. },{"buffer":6}],38:[function(require,module,exports){
  6143. var exports = module.exports = function SHA (algorithm) {
  6144. algorithm = algorithm.toLowerCase()
  6145. var Algorithm = exports[algorithm]
  6146. if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
  6147. return new Algorithm()
  6148. }
  6149. exports.sha = require('./sha')
  6150. exports.sha1 = require('./sha1')
  6151. exports.sha224 = require('./sha224')
  6152. exports.sha256 = require('./sha256')
  6153. exports.sha384 = require('./sha384')
  6154. exports.sha512 = require('./sha512')
  6155. },{"./sha":39,"./sha1":40,"./sha224":41,"./sha256":42,"./sha384":43,"./sha512":44}],39:[function(require,module,exports){
  6156. (function (Buffer){
  6157. /*
  6158. * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined
  6159. * in FIPS PUB 180-1
  6160. * This source code is derived from sha1.js of the same repository.
  6161. * The difference between SHA-0 and SHA-1 is just a bitwise rotate left
  6162. * operation was added.
  6163. */
  6164. var inherits = require('inherits')
  6165. var Hash = require('./hash')
  6166. var K = [
  6167. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  6168. ]
  6169. var W = new Array(80)
  6170. function Sha () {
  6171. this.init()
  6172. this._w = W
  6173. Hash.call(this, 64, 56)
  6174. }
  6175. inherits(Sha, Hash)
  6176. Sha.prototype.init = function () {
  6177. this._a = 0x67452301
  6178. this._b = 0xefcdab89
  6179. this._c = 0x98badcfe
  6180. this._d = 0x10325476
  6181. this._e = 0xc3d2e1f0
  6182. return this
  6183. }
  6184. function rotl5 (num) {
  6185. return (num << 5) | (num >>> 27)
  6186. }
  6187. function rotl30 (num) {
  6188. return (num << 30) | (num >>> 2)
  6189. }
  6190. function ft (s, b, c, d) {
  6191. if (s === 0) return (b & c) | ((~b) & d)
  6192. if (s === 2) return (b & c) | (b & d) | (c & d)
  6193. return b ^ c ^ d
  6194. }
  6195. Sha.prototype._update = function (M) {
  6196. var W = this._w
  6197. var a = this._a | 0
  6198. var b = this._b | 0
  6199. var c = this._c | 0
  6200. var d = this._d | 0
  6201. var e = this._e | 0
  6202. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  6203. for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]
  6204. for (var j = 0; j < 80; ++j) {
  6205. var s = ~~(j / 20)
  6206. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  6207. e = d
  6208. d = c
  6209. c = rotl30(b)
  6210. b = a
  6211. a = t
  6212. }
  6213. this._a = (a + this._a) | 0
  6214. this._b = (b + this._b) | 0
  6215. this._c = (c + this._c) | 0
  6216. this._d = (d + this._d) | 0
  6217. this._e = (e + this._e) | 0
  6218. }
  6219. Sha.prototype._hash = function () {
  6220. var H = new Buffer(20)
  6221. H.writeInt32BE(this._a | 0, 0)
  6222. H.writeInt32BE(this._b | 0, 4)
  6223. H.writeInt32BE(this._c | 0, 8)
  6224. H.writeInt32BE(this._d | 0, 12)
  6225. H.writeInt32BE(this._e | 0, 16)
  6226. return H
  6227. }
  6228. module.exports = Sha
  6229. }).call(this,require("buffer").Buffer)
  6230. },{"./hash":37,"buffer":6,"inherits":15}],40:[function(require,module,exports){
  6231. (function (Buffer){
  6232. /*
  6233. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  6234. * in FIPS PUB 180-1
  6235. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  6236. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6237. * Distributed under the BSD License
  6238. * See http://pajhome.org.uk/crypt/md5 for details.
  6239. */
  6240. var inherits = require('inherits')
  6241. var Hash = require('./hash')
  6242. var K = [
  6243. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  6244. ]
  6245. var W = new Array(80)
  6246. function Sha1 () {
  6247. this.init()
  6248. this._w = W
  6249. Hash.call(this, 64, 56)
  6250. }
  6251. inherits(Sha1, Hash)
  6252. Sha1.prototype.init = function () {
  6253. this._a = 0x67452301
  6254. this._b = 0xefcdab89
  6255. this._c = 0x98badcfe
  6256. this._d = 0x10325476
  6257. this._e = 0xc3d2e1f0
  6258. return this
  6259. }
  6260. function rotl1 (num) {
  6261. return (num << 1) | (num >>> 31)
  6262. }
  6263. function rotl5 (num) {
  6264. return (num << 5) | (num >>> 27)
  6265. }
  6266. function rotl30 (num) {
  6267. return (num << 30) | (num >>> 2)
  6268. }
  6269. function ft (s, b, c, d) {
  6270. if (s === 0) return (b & c) | ((~b) & d)
  6271. if (s === 2) return (b & c) | (b & d) | (c & d)
  6272. return b ^ c ^ d
  6273. }
  6274. Sha1.prototype._update = function (M) {
  6275. var W = this._w
  6276. var a = this._a | 0
  6277. var b = this._b | 0
  6278. var c = this._c | 0
  6279. var d = this._d | 0
  6280. var e = this._e | 0
  6281. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  6282. for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
  6283. for (var j = 0; j < 80; ++j) {
  6284. var s = ~~(j / 20)
  6285. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  6286. e = d
  6287. d = c
  6288. c = rotl30(b)
  6289. b = a
  6290. a = t
  6291. }
  6292. this._a = (a + this._a) | 0
  6293. this._b = (b + this._b) | 0
  6294. this._c = (c + this._c) | 0
  6295. this._d = (d + this._d) | 0
  6296. this._e = (e + this._e) | 0
  6297. }
  6298. Sha1.prototype._hash = function () {
  6299. var H = new Buffer(20)
  6300. H.writeInt32BE(this._a | 0, 0)
  6301. H.writeInt32BE(this._b | 0, 4)
  6302. H.writeInt32BE(this._c | 0, 8)
  6303. H.writeInt32BE(this._d | 0, 12)
  6304. H.writeInt32BE(this._e | 0, 16)
  6305. return H
  6306. }
  6307. module.exports = Sha1
  6308. }).call(this,require("buffer").Buffer)
  6309. },{"./hash":37,"buffer":6,"inherits":15}],41:[function(require,module,exports){
  6310. (function (Buffer){
  6311. /**
  6312. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  6313. * in FIPS 180-2
  6314. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  6315. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6316. *
  6317. */
  6318. var inherits = require('inherits')
  6319. var Sha256 = require('./sha256')
  6320. var Hash = require('./hash')
  6321. var W = new Array(64)
  6322. function Sha224 () {
  6323. this.init()
  6324. this._w = W // new Array(64)
  6325. Hash.call(this, 64, 56)
  6326. }
  6327. inherits(Sha224, Sha256)
  6328. Sha224.prototype.init = function () {
  6329. this._a = 0xc1059ed8
  6330. this._b = 0x367cd507
  6331. this._c = 0x3070dd17
  6332. this._d = 0xf70e5939
  6333. this._e = 0xffc00b31
  6334. this._f = 0x68581511
  6335. this._g = 0x64f98fa7
  6336. this._h = 0xbefa4fa4
  6337. return this
  6338. }
  6339. Sha224.prototype._hash = function () {
  6340. var H = new Buffer(28)
  6341. H.writeInt32BE(this._a, 0)
  6342. H.writeInt32BE(this._b, 4)
  6343. H.writeInt32BE(this._c, 8)
  6344. H.writeInt32BE(this._d, 12)
  6345. H.writeInt32BE(this._e, 16)
  6346. H.writeInt32BE(this._f, 20)
  6347. H.writeInt32BE(this._g, 24)
  6348. return H
  6349. }
  6350. module.exports = Sha224
  6351. }).call(this,require("buffer").Buffer)
  6352. },{"./hash":37,"./sha256":42,"buffer":6,"inherits":15}],42:[function(require,module,exports){
  6353. (function (Buffer){
  6354. /**
  6355. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  6356. * in FIPS 180-2
  6357. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  6358. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6359. *
  6360. */
  6361. var inherits = require('inherits')
  6362. var Hash = require('./hash')
  6363. var K = [
  6364. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  6365. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  6366. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  6367. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  6368. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  6369. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  6370. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  6371. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  6372. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  6373. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  6374. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  6375. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  6376. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  6377. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  6378. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  6379. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  6380. ]
  6381. var W = new Array(64)
  6382. function Sha256 () {
  6383. this.init()
  6384. this._w = W // new Array(64)
  6385. Hash.call(this, 64, 56)
  6386. }
  6387. inherits(Sha256, Hash)
  6388. Sha256.prototype.init = function () {
  6389. this._a = 0x6a09e667
  6390. this._b = 0xbb67ae85
  6391. this._c = 0x3c6ef372
  6392. this._d = 0xa54ff53a
  6393. this._e = 0x510e527f
  6394. this._f = 0x9b05688c
  6395. this._g = 0x1f83d9ab
  6396. this._h = 0x5be0cd19
  6397. return this
  6398. }
  6399. function ch (x, y, z) {
  6400. return z ^ (x & (y ^ z))
  6401. }
  6402. function maj (x, y, z) {
  6403. return (x & y) | (z & (x | y))
  6404. }
  6405. function sigma0 (x) {
  6406. return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
  6407. }
  6408. function sigma1 (x) {
  6409. return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
  6410. }
  6411. function gamma0 (x) {
  6412. return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
  6413. }
  6414. function gamma1 (x) {
  6415. return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
  6416. }
  6417. Sha256.prototype._update = function (M) {
  6418. var W = this._w
  6419. var a = this._a | 0
  6420. var b = this._b | 0
  6421. var c = this._c | 0
  6422. var d = this._d | 0
  6423. var e = this._e | 0
  6424. var f = this._f | 0
  6425. var g = this._g | 0
  6426. var h = this._h | 0
  6427. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  6428. for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
  6429. for (var j = 0; j < 64; ++j) {
  6430. var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
  6431. var T2 = (sigma0(a) + maj(a, b, c)) | 0
  6432. h = g
  6433. g = f
  6434. f = e
  6435. e = (d + T1) | 0
  6436. d = c
  6437. c = b
  6438. b = a
  6439. a = (T1 + T2) | 0
  6440. }
  6441. this._a = (a + this._a) | 0
  6442. this._b = (b + this._b) | 0
  6443. this._c = (c + this._c) | 0
  6444. this._d = (d + this._d) | 0
  6445. this._e = (e + this._e) | 0
  6446. this._f = (f + this._f) | 0
  6447. this._g = (g + this._g) | 0
  6448. this._h = (h + this._h) | 0
  6449. }
  6450. Sha256.prototype._hash = function () {
  6451. var H = new Buffer(32)
  6452. H.writeInt32BE(this._a, 0)
  6453. H.writeInt32BE(this._b, 4)
  6454. H.writeInt32BE(this._c, 8)
  6455. H.writeInt32BE(this._d, 12)
  6456. H.writeInt32BE(this._e, 16)
  6457. H.writeInt32BE(this._f, 20)
  6458. H.writeInt32BE(this._g, 24)
  6459. H.writeInt32BE(this._h, 28)
  6460. return H
  6461. }
  6462. module.exports = Sha256
  6463. }).call(this,require("buffer").Buffer)
  6464. },{"./hash":37,"buffer":6,"inherits":15}],43:[function(require,module,exports){
  6465. (function (Buffer){
  6466. var inherits = require('inherits')
  6467. var SHA512 = require('./sha512')
  6468. var Hash = require('./hash')
  6469. var W = new Array(160)
  6470. function Sha384 () {
  6471. this.init()
  6472. this._w = W
  6473. Hash.call(this, 128, 112)
  6474. }
  6475. inherits(Sha384, SHA512)
  6476. Sha384.prototype.init = function () {
  6477. this._ah = 0xcbbb9d5d
  6478. this._bh = 0x629a292a
  6479. this._ch = 0x9159015a
  6480. this._dh = 0x152fecd8
  6481. this._eh = 0x67332667
  6482. this._fh = 0x8eb44a87
  6483. this._gh = 0xdb0c2e0d
  6484. this._hh = 0x47b5481d
  6485. this._al = 0xc1059ed8
  6486. this._bl = 0x367cd507
  6487. this._cl = 0x3070dd17
  6488. this._dl = 0xf70e5939
  6489. this._el = 0xffc00b31
  6490. this._fl = 0x68581511
  6491. this._gl = 0x64f98fa7
  6492. this._hl = 0xbefa4fa4
  6493. return this
  6494. }
  6495. Sha384.prototype._hash = function () {
  6496. var H = new Buffer(48)
  6497. function writeInt64BE (h, l, offset) {
  6498. H.writeInt32BE(h, offset)
  6499. H.writeInt32BE(l, offset + 4)
  6500. }
  6501. writeInt64BE(this._ah, this._al, 0)
  6502. writeInt64BE(this._bh, this._bl, 8)
  6503. writeInt64BE(this._ch, this._cl, 16)
  6504. writeInt64BE(this._dh, this._dl, 24)
  6505. writeInt64BE(this._eh, this._el, 32)
  6506. writeInt64BE(this._fh, this._fl, 40)
  6507. return H
  6508. }
  6509. module.exports = Sha384
  6510. }).call(this,require("buffer").Buffer)
  6511. },{"./hash":37,"./sha512":44,"buffer":6,"inherits":15}],44:[function(require,module,exports){
  6512. (function (Buffer){
  6513. var inherits = require('inherits')
  6514. var Hash = require('./hash')
  6515. var K = [
  6516. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  6517. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  6518. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  6519. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  6520. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  6521. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  6522. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  6523. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  6524. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  6525. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  6526. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  6527. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  6528. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  6529. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  6530. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  6531. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  6532. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  6533. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  6534. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  6535. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  6536. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  6537. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  6538. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  6539. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  6540. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  6541. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  6542. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  6543. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  6544. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  6545. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  6546. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  6547. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  6548. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  6549. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  6550. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  6551. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  6552. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  6553. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  6554. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  6555. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  6556. ]
  6557. var W = new Array(160)
  6558. function Sha512 () {
  6559. this.init()
  6560. this._w = W
  6561. Hash.call(this, 128, 112)
  6562. }
  6563. inherits(Sha512, Hash)
  6564. Sha512.prototype.init = function () {
  6565. this._ah = 0x6a09e667
  6566. this._bh = 0xbb67ae85
  6567. this._ch = 0x3c6ef372
  6568. this._dh = 0xa54ff53a
  6569. this._eh = 0x510e527f
  6570. this._fh = 0x9b05688c
  6571. this._gh = 0x1f83d9ab
  6572. this._hh = 0x5be0cd19
  6573. this._al = 0xf3bcc908
  6574. this._bl = 0x84caa73b
  6575. this._cl = 0xfe94f82b
  6576. this._dl = 0x5f1d36f1
  6577. this._el = 0xade682d1
  6578. this._fl = 0x2b3e6c1f
  6579. this._gl = 0xfb41bd6b
  6580. this._hl = 0x137e2179
  6581. return this
  6582. }
  6583. function Ch (x, y, z) {
  6584. return z ^ (x & (y ^ z))
  6585. }
  6586. function maj (x, y, z) {
  6587. return (x & y) | (z & (x | y))
  6588. }
  6589. function sigma0 (x, xl) {
  6590. return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
  6591. }
  6592. function sigma1 (x, xl) {
  6593. return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
  6594. }
  6595. function Gamma0 (x, xl) {
  6596. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
  6597. }
  6598. function Gamma0l (x, xl) {
  6599. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
  6600. }
  6601. function Gamma1 (x, xl) {
  6602. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
  6603. }
  6604. function Gamma1l (x, xl) {
  6605. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
  6606. }
  6607. function getCarry (a, b) {
  6608. return (a >>> 0) < (b >>> 0) ? 1 : 0
  6609. }
  6610. Sha512.prototype._update = function (M) {
  6611. var W = this._w
  6612. var ah = this._ah | 0
  6613. var bh = this._bh | 0
  6614. var ch = this._ch | 0
  6615. var dh = this._dh | 0
  6616. var eh = this._eh | 0
  6617. var fh = this._fh | 0
  6618. var gh = this._gh | 0
  6619. var hh = this._hh | 0
  6620. var al = this._al | 0
  6621. var bl = this._bl | 0
  6622. var cl = this._cl | 0
  6623. var dl = this._dl | 0
  6624. var el = this._el | 0
  6625. var fl = this._fl | 0
  6626. var gl = this._gl | 0
  6627. var hl = this._hl | 0
  6628. for (var i = 0; i < 32; i += 2) {
  6629. W[i] = M.readInt32BE(i * 4)
  6630. W[i + 1] = M.readInt32BE(i * 4 + 4)
  6631. }
  6632. for (; i < 160; i += 2) {
  6633. var xh = W[i - 15 * 2]
  6634. var xl = W[i - 15 * 2 + 1]
  6635. var gamma0 = Gamma0(xh, xl)
  6636. var gamma0l = Gamma0l(xl, xh)
  6637. xh = W[i - 2 * 2]
  6638. xl = W[i - 2 * 2 + 1]
  6639. var gamma1 = Gamma1(xh, xl)
  6640. var gamma1l = Gamma1l(xl, xh)
  6641. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  6642. var Wi7h = W[i - 7 * 2]
  6643. var Wi7l = W[i - 7 * 2 + 1]
  6644. var Wi16h = W[i - 16 * 2]
  6645. var Wi16l = W[i - 16 * 2 + 1]
  6646. var Wil = (gamma0l + Wi7l) | 0
  6647. var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0
  6648. Wil = (Wil + gamma1l) | 0
  6649. Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0
  6650. Wil = (Wil + Wi16l) | 0
  6651. Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0
  6652. W[i] = Wih
  6653. W[i + 1] = Wil
  6654. }
  6655. for (var j = 0; j < 160; j += 2) {
  6656. Wih = W[j]
  6657. Wil = W[j + 1]
  6658. var majh = maj(ah, bh, ch)
  6659. var majl = maj(al, bl, cl)
  6660. var sigma0h = sigma0(ah, al)
  6661. var sigma0l = sigma0(al, ah)
  6662. var sigma1h = sigma1(eh, el)
  6663. var sigma1l = sigma1(el, eh)
  6664. // t1 = h + sigma1 + ch + K[j] + W[j]
  6665. var Kih = K[j]
  6666. var Kil = K[j + 1]
  6667. var chh = Ch(eh, fh, gh)
  6668. var chl = Ch(el, fl, gl)
  6669. var t1l = (hl + sigma1l) | 0
  6670. var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0
  6671. t1l = (t1l + chl) | 0
  6672. t1h = (t1h + chh + getCarry(t1l, chl)) | 0
  6673. t1l = (t1l + Kil) | 0
  6674. t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0
  6675. t1l = (t1l + Wil) | 0
  6676. t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0
  6677. // t2 = sigma0 + maj
  6678. var t2l = (sigma0l + majl) | 0
  6679. var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0
  6680. hh = gh
  6681. hl = gl
  6682. gh = fh
  6683. gl = fl
  6684. fh = eh
  6685. fl = el
  6686. el = (dl + t1l) | 0
  6687. eh = (dh + t1h + getCarry(el, dl)) | 0
  6688. dh = ch
  6689. dl = cl
  6690. ch = bh
  6691. cl = bl
  6692. bh = ah
  6693. bl = al
  6694. al = (t1l + t2l) | 0
  6695. ah = (t1h + t2h + getCarry(al, t1l)) | 0
  6696. }
  6697. this._al = (this._al + al) | 0
  6698. this._bl = (this._bl + bl) | 0
  6699. this._cl = (this._cl + cl) | 0
  6700. this._dl = (this._dl + dl) | 0
  6701. this._el = (this._el + el) | 0
  6702. this._fl = (this._fl + fl) | 0
  6703. this._gl = (this._gl + gl) | 0
  6704. this._hl = (this._hl + hl) | 0
  6705. this._ah = (this._ah + ah + getCarry(this._al, al)) | 0
  6706. this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0
  6707. this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0
  6708. this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0
  6709. this._eh = (this._eh + eh + getCarry(this._el, el)) | 0
  6710. this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0
  6711. this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
  6712. this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
  6713. }
  6714. Sha512.prototype._hash = function () {
  6715. var H = new Buffer(64)
  6716. function writeInt64BE (h, l, offset) {
  6717. H.writeInt32BE(h, offset)
  6718. H.writeInt32BE(l, offset + 4)
  6719. }
  6720. writeInt64BE(this._ah, this._al, 0)
  6721. writeInt64BE(this._bh, this._bl, 8)
  6722. writeInt64BE(this._ch, this._cl, 16)
  6723. writeInt64BE(this._dh, this._dl, 24)
  6724. writeInt64BE(this._eh, this._el, 32)
  6725. writeInt64BE(this._fh, this._fl, 40)
  6726. writeInt64BE(this._gh, this._gl, 48)
  6727. writeInt64BE(this._hh, this._hl, 56)
  6728. return H
  6729. }
  6730. module.exports = Sha512
  6731. }).call(this,require("buffer").Buffer)
  6732. },{"./hash":37,"buffer":6,"inherits":15}],45:[function(require,module,exports){
  6733. // Copyright Joyent, Inc. and other Node contributors.
  6734. //
  6735. // Permission is hereby granted, free of charge, to any person obtaining a
  6736. // copy of this software and associated documentation files (the
  6737. // "Software"), to deal in the Software without restriction, including
  6738. // without limitation the rights to use, copy, modify, merge, publish,
  6739. // distribute, sublicense, and/or sell copies of the Software, and to permit
  6740. // persons to whom the Software is furnished to do so, subject to the
  6741. // following conditions:
  6742. //
  6743. // The above copyright notice and this permission notice shall be included
  6744. // in all copies or substantial portions of the Software.
  6745. //
  6746. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  6747. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  6748. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  6749. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  6750. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  6751. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  6752. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  6753. module.exports = Stream;
  6754. var EE = require('events').EventEmitter;
  6755. var inherits = require('inherits');
  6756. inherits(Stream, EE);
  6757. Stream.Readable = require('readable-stream/readable.js');
  6758. Stream.Writable = require('readable-stream/writable.js');
  6759. Stream.Duplex = require('readable-stream/duplex.js');
  6760. Stream.Transform = require('readable-stream/transform.js');
  6761. Stream.PassThrough = require('readable-stream/passthrough.js');
  6762. // Backwards-compat with node 0.4.x
  6763. Stream.Stream = Stream;
  6764. // old-style streams. Note that the pipe method (the only relevant
  6765. // part of this class) is overridden in the Readable class.
  6766. function Stream() {
  6767. EE.call(this);
  6768. }
  6769. Stream.prototype.pipe = function(dest, options) {
  6770. var source = this;
  6771. function ondata(chunk) {
  6772. if (dest.writable) {
  6773. if (false === dest.write(chunk) && source.pause) {
  6774. source.pause();
  6775. }
  6776. }
  6777. }
  6778. source.on('data', ondata);
  6779. function ondrain() {
  6780. if (source.readable && source.resume) {
  6781. source.resume();
  6782. }
  6783. }
  6784. dest.on('drain', ondrain);
  6785. // If the 'end' option is not supplied, dest.end() will be called when
  6786. // source gets the 'end' or 'close' events. Only dest.end() once.
  6787. if (!dest._isStdio && (!options || options.end !== false)) {
  6788. source.on('end', onend);
  6789. source.on('close', onclose);
  6790. }
  6791. var didOnEnd = false;
  6792. function onend() {
  6793. if (didOnEnd) return;
  6794. didOnEnd = true;
  6795. dest.end();
  6796. }
  6797. function onclose() {
  6798. if (didOnEnd) return;
  6799. didOnEnd = true;
  6800. if (typeof dest.destroy === 'function') dest.destroy();
  6801. }
  6802. // don't leave dangling pipes when there are errors.
  6803. function onerror(er) {
  6804. cleanup();
  6805. if (EE.listenerCount(this, 'error') === 0) {
  6806. throw er; // Unhandled stream error in pipe.
  6807. }
  6808. }
  6809. source.on('error', onerror);
  6810. dest.on('error', onerror);
  6811. // remove all the event listeners that were added.
  6812. function cleanup() {
  6813. source.removeListener('data', ondata);
  6814. dest.removeListener('drain', ondrain);
  6815. source.removeListener('end', onend);
  6816. source.removeListener('close', onclose);
  6817. source.removeListener('error', onerror);
  6818. dest.removeListener('error', onerror);
  6819. source.removeListener('end', cleanup);
  6820. source.removeListener('close', cleanup);
  6821. dest.removeListener('close', cleanup);
  6822. }
  6823. source.on('end', cleanup);
  6824. source.on('close', cleanup);
  6825. dest.on('close', cleanup);
  6826. dest.emit('pipe', source);
  6827. // Allow for unix-like usage: A.pipe(B).pipe(C)
  6828. return dest;
  6829. };
  6830. },{"events":13,"inherits":15,"readable-stream/duplex.js":25,"readable-stream/passthrough.js":32,"readable-stream/readable.js":33,"readable-stream/transform.js":34,"readable-stream/writable.js":35}],46:[function(require,module,exports){
  6831. // Copyright Joyent, Inc. and other Node contributors.
  6832. //
  6833. // Permission is hereby granted, free of charge, to any person obtaining a
  6834. // copy of this software and associated documentation files (the
  6835. // "Software"), to deal in the Software without restriction, including
  6836. // without limitation the rights to use, copy, modify, merge, publish,
  6837. // distribute, sublicense, and/or sell copies of the Software, and to permit
  6838. // persons to whom the Software is furnished to do so, subject to the
  6839. // following conditions:
  6840. //
  6841. // The above copyright notice and this permission notice shall be included
  6842. // in all copies or substantial portions of the Software.
  6843. //
  6844. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  6845. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  6846. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  6847. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  6848. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  6849. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  6850. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  6851. var Buffer = require('buffer').Buffer;
  6852. var isBufferEncoding = Buffer.isEncoding
  6853. || function(encoding) {
  6854. switch (encoding && encoding.toLowerCase()) {
  6855. 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;
  6856. default: return false;
  6857. }
  6858. }
  6859. function assertEncoding(encoding) {
  6860. if (encoding && !isBufferEncoding(encoding)) {
  6861. throw new Error('Unknown encoding: ' + encoding);
  6862. }
  6863. }
  6864. // StringDecoder provides an interface for efficiently splitting a series of
  6865. // buffers into a series of JS strings without breaking apart multi-byte
  6866. // characters. CESU-8 is handled as part of the UTF-8 encoding.
  6867. //
  6868. // @TODO Handling all encodings inside a single object makes it very difficult
  6869. // to reason about this code, so it should be split up in the future.
  6870. // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
  6871. // points as used by CESU-8.
  6872. var StringDecoder = exports.StringDecoder = function(encoding) {
  6873. this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
  6874. assertEncoding(encoding);
  6875. switch (this.encoding) {
  6876. case 'utf8':
  6877. // CESU-8 represents each of Surrogate Pair by 3-bytes
  6878. this.surrogateSize = 3;
  6879. break;
  6880. case 'ucs2':
  6881. case 'utf16le':
  6882. // UTF-16 represents each of Surrogate Pair by 2-bytes
  6883. this.surrogateSize = 2;
  6884. this.detectIncompleteChar = utf16DetectIncompleteChar;
  6885. break;
  6886. case 'base64':
  6887. // Base-64 stores 3 bytes in 4 chars, and pads the remainder.
  6888. this.surrogateSize = 3;
  6889. this.detectIncompleteChar = base64DetectIncompleteChar;
  6890. break;
  6891. default:
  6892. this.write = passThroughWrite;
  6893. return;
  6894. }
  6895. // Enough space to store all bytes of a single character. UTF-8 needs 4
  6896. // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
  6897. this.charBuffer = new Buffer(6);
  6898. // Number of bytes received for the current incomplete multi-byte character.
  6899. this.charReceived = 0;
  6900. // Number of bytes expected for the current incomplete multi-byte character.
  6901. this.charLength = 0;
  6902. };
  6903. // write decodes the given buffer and returns it as JS string that is
  6904. // guaranteed to not contain any partial multi-byte characters. Any partial
  6905. // character found at the end of the buffer is buffered up, and will be
  6906. // returned when calling write again with the remaining bytes.
  6907. //
  6908. // Note: Converting a Buffer containing an orphan surrogate to a String
  6909. // currently works, but converting a String to a Buffer (via `new Buffer`, or
  6910. // Buffer#write) will replace incomplete surrogates with the unicode
  6911. // replacement character. See https://codereview.chromium.org/121173009/ .
  6912. StringDecoder.prototype.write = function(buffer) {
  6913. var charStr = '';
  6914. // if our last write ended with an incomplete multibyte character
  6915. while (this.charLength) {
  6916. // determine how many remaining bytes this buffer has to offer for this char
  6917. var available = (buffer.length >= this.charLength - this.charReceived) ?
  6918. this.charLength - this.charReceived :
  6919. buffer.length;
  6920. // add the new bytes to the char buffer
  6921. buffer.copy(this.charBuffer, this.charReceived, 0, available);
  6922. this.charReceived += available;
  6923. if (this.charReceived < this.charLength) {
  6924. // still not enough chars in this buffer? wait for more ...
  6925. return '';
  6926. }
  6927. // remove bytes belonging to the current character from the buffer
  6928. buffer = buffer.slice(available, buffer.length);
  6929. // get the character that was split
  6930. charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
  6931. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  6932. var charCode = charStr.charCodeAt(charStr.length - 1);
  6933. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  6934. this.charLength += this.surrogateSize;
  6935. charStr = '';
  6936. continue;
  6937. }
  6938. this.charReceived = this.charLength = 0;
  6939. // if there are no more bytes in this buffer, just emit our char
  6940. if (buffer.length === 0) {
  6941. return charStr;
  6942. }
  6943. break;
  6944. }
  6945. // determine and set charLength / charReceived
  6946. this.detectIncompleteChar(buffer);
  6947. var end = buffer.length;
  6948. if (this.charLength) {
  6949. // buffer the incomplete character bytes we got
  6950. buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
  6951. end -= this.charReceived;
  6952. }
  6953. charStr += buffer.toString(this.encoding, 0, end);
  6954. var end = charStr.length - 1;
  6955. var charCode = charStr.charCodeAt(end);
  6956. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  6957. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  6958. var size = this.surrogateSize;
  6959. this.charLength += size;
  6960. this.charReceived += size;
  6961. this.charBuffer.copy(this.charBuffer, size, 0, size);
  6962. buffer.copy(this.charBuffer, 0, 0, size);
  6963. return charStr.substring(0, end);
  6964. }
  6965. // or just emit the charStr
  6966. return charStr;
  6967. };
  6968. // detectIncompleteChar determines if there is an incomplete UTF-8 character at
  6969. // the end of the given buffer. If so, it sets this.charLength to the byte
  6970. // length that character, and sets this.charReceived to the number of bytes
  6971. // that are available for this character.
  6972. StringDecoder.prototype.detectIncompleteChar = function(buffer) {
  6973. // determine how many bytes we have to check at the end of this buffer
  6974. var i = (buffer.length >= 3) ? 3 : buffer.length;
  6975. // Figure out if one of the last i bytes of our buffer announces an
  6976. // incomplete char.
  6977. for (; i > 0; i--) {
  6978. var c = buffer[buffer.length - i];
  6979. // See http://en.wikipedia.org/wiki/UTF-8#Description
  6980. // 110XXXXX
  6981. if (i == 1 && c >> 5 == 0x06) {
  6982. this.charLength = 2;
  6983. break;
  6984. }
  6985. // 1110XXXX
  6986. if (i <= 2 && c >> 4 == 0x0E) {
  6987. this.charLength = 3;
  6988. break;
  6989. }
  6990. // 11110XXX
  6991. if (i <= 3 && c >> 3 == 0x1E) {
  6992. this.charLength = 4;
  6993. break;
  6994. }
  6995. }
  6996. this.charReceived = i;
  6997. };
  6998. StringDecoder.prototype.end = function(buffer) {
  6999. var res = '';
  7000. if (buffer && buffer.length)
  7001. res = this.write(buffer);
  7002. if (this.charReceived) {
  7003. var cr = this.charReceived;
  7004. var buf = this.charBuffer;
  7005. var enc = this.encoding;
  7006. res += buf.slice(0, cr).toString(enc);
  7007. }
  7008. return res;
  7009. };
  7010. function passThroughWrite(buffer) {
  7011. return buffer.toString(this.encoding);
  7012. }
  7013. function utf16DetectIncompleteChar(buffer) {
  7014. this.charReceived = buffer.length % 2;
  7015. this.charLength = this.charReceived ? 2 : 0;
  7016. }
  7017. function base64DetectIncompleteChar(buffer) {
  7018. this.charReceived = buffer.length % 3;
  7019. this.charLength = this.charReceived ? 3 : 0;
  7020. }
  7021. },{"buffer":6}],47:[function(require,module,exports){
  7022. (function (global){
  7023. /**
  7024. * Module exports.
  7025. */
  7026. module.exports = deprecate;
  7027. /**
  7028. * Mark that a method should not be used.
  7029. * Returns a modified function which warns once by default.
  7030. *
  7031. * If `localStorage.noDeprecation = true` is set, then it is a no-op.
  7032. *
  7033. * If `localStorage.throwDeprecation = true` is set, then deprecated functions
  7034. * will throw an Error when invoked.
  7035. *
  7036. * If `localStorage.traceDeprecation = true` is set, then deprecated functions
  7037. * will invoke `console.trace()` instead of `console.error()`.
  7038. *
  7039. * @param {Function} fn - the function to deprecate
  7040. * @param {String} msg - the string to print to the console when `fn` is invoked
  7041. * @returns {Function} a new "deprecated" version of `fn`
  7042. * @api public
  7043. */
  7044. function deprecate (fn, msg) {
  7045. if (config('noDeprecation')) {
  7046. return fn;
  7047. }
  7048. var warned = false;
  7049. function deprecated() {
  7050. if (!warned) {
  7051. if (config('throwDeprecation')) {
  7052. throw new Error(msg);
  7053. } else if (config('traceDeprecation')) {
  7054. console.trace(msg);
  7055. } else {
  7056. console.warn(msg);
  7057. }
  7058. warned = true;
  7059. }
  7060. return fn.apply(this, arguments);
  7061. }
  7062. return deprecated;
  7063. }
  7064. /**
  7065. * Checks `localStorage` for boolean values for the given `name`.
  7066. *
  7067. * @param {String} name
  7068. * @returns {Boolean}
  7069. * @api private
  7070. */
  7071. function config (name) {
  7072. // accessing global.localStorage can trigger a DOMException in sandboxed iframes
  7073. try {
  7074. if (!global.localStorage) return false;
  7075. } catch (_) {
  7076. return false;
  7077. }
  7078. var val = global.localStorage[name];
  7079. if (null == val) return false;
  7080. return String(val).toLowerCase() === 'true';
  7081. }
  7082. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  7083. },{}],48:[function(require,module,exports){
  7084. (function (Buffer){
  7085. var pbkdf2 = require('pbkdf2');
  7086. var Promise = require('pinkie-promise');
  7087. function shouldUseNative() {
  7088. return !!(typeof window !== 'undefined' && window.crypto && window.crypto.subtle);
  7089. }
  7090. function pbkdf2Native(password, salt, iterations, keylen, digest) {
  7091. var algorithms = {
  7092. 'sha1': 'SHA-1',
  7093. 'sha-1': 'SHA-1',
  7094. 'sha256': 'SHA-256',
  7095. 'sha-256': 'SHA-256',
  7096. 'sha512': 'SHA-512',
  7097. 'sha-512': 'SHA-512',
  7098. };
  7099. return window.crypto.subtle.importKey('raw', new Buffer(password), 'PBKDF2', false, ['deriveKey'])
  7100. .then(function (key) {
  7101. var algo = {
  7102. name: 'PBKDF2',
  7103. salt: new Buffer(salt),
  7104. iterations: iterations,
  7105. hash: algorithms[digest.toLowerCase()],
  7106. };
  7107. return window.crypto.subtle.deriveKey(algo, key, {
  7108. name: 'AES-CTR',
  7109. length: keylen * 8
  7110. }, true, ['encrypt', 'decrypt']);
  7111. })
  7112. .then(function (derivedKey) {
  7113. return window.crypto.subtle.exportKey('raw', derivedKey).then(function (keyArray) {
  7114. return new Buffer(keyArray).toString('hex');
  7115. });
  7116. });
  7117. }
  7118. function pbkdf2Browserified(password, salt, iterations, keylen, digest) {
  7119. return new Promise(function (resolve, reject) {
  7120. pbkdf2.pbkdf2(password, salt, iterations, keylen, digest, function (error, key) {
  7121. if (error) {
  7122. reject('error in pbkdf2');
  7123. } else {
  7124. resolve(key.toString('hex'));
  7125. }
  7126. });
  7127. });
  7128. }
  7129. module.exports = shouldUseNative() ? pbkdf2Native : pbkdf2Browserified;
  7130. }).call(this,require("buffer").Buffer)
  7131. },{"buffer":6,"pbkdf2":19,"pinkie-promise":21}],49:[function(require,module,exports){
  7132. (function (Buffer){
  7133. var pbkdf2 = require('./pbkdf2');
  7134. var createHMAC = require('create-hmac');
  7135. var Promise = require('pinkie-promise');
  7136. module.exports = {
  7137. encryptLogin: encryptLogin,
  7138. renderPassword: renderPassword,
  7139. createFingerprint: createFingerprint,
  7140. _deriveEncryptedLogin: deriveEncryptedLogin,
  7141. _getPasswordTemplate: getPasswordTemplate,
  7142. _prettyPrint: prettyPrint,
  7143. _string2charCodes: string2charCodes,
  7144. _getCharType: getCharType,
  7145. _getPasswordChar: getPasswordChar,
  7146. _createHmac: createHmac,
  7147. };
  7148. function encryptLogin(login, masterPassword, options) {
  7149. var _options = options !== undefined ? options : {};
  7150. var iterations = _options.iterations || 8192;
  7151. var keylen = _options.keylen || 32;
  7152. return pbkdf2(masterPassword, login, iterations, keylen, 'sha256');
  7153. }
  7154. function renderPassword(encryptedLogin, site, passwordOptions) {
  7155. return deriveEncryptedLogin(encryptedLogin, site, passwordOptions).then(function (derivedEncryptedLogin) {
  7156. var template = passwordOptions.template || getPasswordTemplate(passwordOptions);
  7157. return prettyPrint(derivedEncryptedLogin, template);
  7158. });
  7159. }
  7160. function createHmac(encryptedLogin, salt) {
  7161. return new Promise(function (resolve) {
  7162. resolve(createHMAC('sha256', new Buffer(encryptedLogin)).update(salt).digest('hex'));
  7163. });
  7164. }
  7165. function deriveEncryptedLogin(encryptedLogin, site, options) {
  7166. var _options = options !== undefined ? options : {};
  7167. var length = _options.length || 12;
  7168. var counter = _options.counter || 1;
  7169. var salt = site + counter.toString();
  7170. return createHmac(encryptedLogin, salt).then(function (derivedHash) {
  7171. return derivedHash.substring(0, length);
  7172. });
  7173. }
  7174. function getPasswordTemplate(passwordTypes) {
  7175. var templates = {
  7176. lowercase: 'vc',
  7177. uppercase: 'VC',
  7178. numbers: 'n',
  7179. symbols: 's',
  7180. };
  7181. var returnedTemplate = '';
  7182. Object.keys(templates).forEach(function (template) {
  7183. if (passwordTypes.hasOwnProperty(template) && passwordTypes[template]) {
  7184. returnedTemplate += templates[template]
  7185. }
  7186. });
  7187. return returnedTemplate;
  7188. }
  7189. function prettyPrint(hash, template) {
  7190. var password = '';
  7191. string2charCodes(hash).forEach(function (charCode, index) {
  7192. var charType = getCharType(template, index);
  7193. password += getPasswordChar(charType, charCode);
  7194. });
  7195. return password;
  7196. }
  7197. function string2charCodes(text) {
  7198. var charCodes = [];
  7199. for (var i = 0; i < text.length; i++) {
  7200. charCodes.push(text.charCodeAt(i));
  7201. }
  7202. return charCodes;
  7203. }
  7204. function getCharType(template, index) {
  7205. return template[index % template.length];
  7206. }
  7207. function getPasswordChar(charType, index) {
  7208. var passwordsChars = {
  7209. V: 'AEIOUY',
  7210. C: 'BCDFGHJKLMNPQRSTVWXZ',
  7211. v: 'aeiouy',
  7212. c: 'bcdfghjklmnpqrstvwxz',
  7213. A: 'AEIOUYBCDFGHJKLMNPQRSTVWXZ',
  7214. a: 'AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz',
  7215. n: '0123456789',
  7216. s: '@&%?,=[]_:-+*$#!\'^~;()/.',
  7217. x: 'AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!\'^~;()/.'
  7218. };
  7219. var passwordChar = passwordsChars[charType];
  7220. return passwordChar[index % passwordChar.length];
  7221. }
  7222. function createFingerprint(str) {
  7223. return new Promise(function (resolve) {
  7224. resolve(createHMAC('sha256', new Buffer(str)).digest('hex'))
  7225. });
  7226. }
  7227. }).call(this,require("buffer").Buffer)
  7228. },{"./pbkdf2":48,"buffer":6,"create-hmac":12,"pinkie-promise":21}],50:[function(require,module,exports){
  7229. var pbkdf2 = require('./pbkdf2');
  7230. var bigInt = require("big-integer");
  7231. module.exports = {
  7232. generatePassword: generatePassword,
  7233. _calcEntropy: calcEntropy,
  7234. _consumeEntropy: consumeEntropy,
  7235. _getSetOfCharacters: getSetOfCharacters,
  7236. _getConfiguredRules: getConfiguredRules,
  7237. _insertStringPseudoRandomly: insertStringPseudoRandomly,
  7238. _getOneCharPerRule: getOneCharPerRule,
  7239. _renderPassword: renderPassword
  7240. };
  7241. function generatePassword(site, login, masterPassword, passwordProfile) {
  7242. return calcEntropy(site, login, masterPassword, passwordProfile).then(function (entropy) {
  7243. return renderPassword(entropy, passwordProfile);
  7244. });
  7245. }
  7246. function calcEntropy(site, login, masterPassword, passwordProfile) {
  7247. var salt = site + login + passwordProfile.counter.toString(16);
  7248. return pbkdf2(masterPassword, salt, passwordProfile.iterations, passwordProfile.keylen, passwordProfile.digest);
  7249. }
  7250. var characterSubsets = {
  7251. lowercase: 'abcdefghijklmnopqrstuvwxyz',
  7252. uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  7253. numbers: '0123456789',
  7254. symbols: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  7255. };
  7256. function getSetOfCharacters(rules) {
  7257. if (typeof rules === 'undefined') {
  7258. return characterSubsets.lowercase + characterSubsets.uppercase + characterSubsets.numbers + characterSubsets.symbols;
  7259. }
  7260. var setOfChars = '';
  7261. rules.forEach(function (rule) {
  7262. setOfChars += characterSubsets[rule];
  7263. });
  7264. return setOfChars;
  7265. }
  7266. function consumeEntropy(generatedPassword, quotient, setOfCharacters, maxLength) {
  7267. if (generatedPassword.length >= maxLength) {
  7268. return {value: generatedPassword, entropy: quotient};
  7269. }
  7270. var longDivision = quotient.divmod(setOfCharacters.length);
  7271. generatedPassword += setOfCharacters[longDivision.remainder];
  7272. return consumeEntropy(generatedPassword, longDivision.quotient, setOfCharacters, maxLength);
  7273. }
  7274. function insertStringPseudoRandomly(generatedPassword, entropy, string) {
  7275. for (var i = 0; i < string.length; i++) {
  7276. var longDivision = entropy.divmod(generatedPassword.length);
  7277. generatedPassword = generatedPassword.slice(0, longDivision.remainder) + string[i] + generatedPassword.slice(longDivision.remainder);
  7278. entropy = longDivision.quotient;
  7279. }
  7280. return generatedPassword;
  7281. }
  7282. function getOneCharPerRule(entropy, rules) {
  7283. var oneCharPerRules = '';
  7284. rules.forEach(function (rule) {
  7285. var password = consumeEntropy('', entropy, characterSubsets[rule], 1);
  7286. oneCharPerRules += password.value;
  7287. entropy = password.entropy;
  7288. });
  7289. return {value: oneCharPerRules, entropy: entropy};
  7290. }
  7291. function getConfiguredRules(passwordProfile) {
  7292. return ['lowercase', 'uppercase', 'numbers', 'symbols'].filter(function (rule) {
  7293. return passwordProfile[rule];
  7294. });
  7295. }
  7296. function renderPassword(entropy, passwordProfile) {
  7297. var rules = getConfiguredRules(passwordProfile);
  7298. var setOfCharacters = getSetOfCharacters(rules);
  7299. var password = consumeEntropy('', bigInt(entropy, 16), setOfCharacters, passwordProfile.length - rules.length);
  7300. var charactersToAdd = getOneCharPerRule(password.entropy, rules);
  7301. return insertStringPseudoRandomly(password.value, charactersToAdd.entropy, charactersToAdd.value);
  7302. }
  7303. },{"./pbkdf2":48,"big-integer":3}]},{},[1])(1)
  7304. });