tpUtils.js 296 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. 'use strict'
  3. // base-x encoding / decoding
  4. // Copyright (c) 2018 base-x contributors
  5. // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
  6. // Distributed under the MIT software license, see the accompanying
  7. // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
  8. // @ts-ignore
  9. var _Buffer = require('safe-buffer').Buffer
  10. function base (ALPHABET) {
  11. if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
  12. var BASE_MAP = new Uint8Array(256)
  13. for (var j = 0; j < BASE_MAP.length; j++) {
  14. BASE_MAP[j] = 255
  15. }
  16. for (var i = 0; i < ALPHABET.length; i++) {
  17. var x = ALPHABET.charAt(i)
  18. var xc = x.charCodeAt(0)
  19. if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
  20. BASE_MAP[xc] = i
  21. }
  22. var BASE = ALPHABET.length
  23. var LEADER = ALPHABET.charAt(0)
  24. var FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
  25. var iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
  26. function encode (source) {
  27. if (Array.isArray(source) || source instanceof Uint8Array) { source = _Buffer.from(source) }
  28. if (!_Buffer.isBuffer(source)) { throw new TypeError('Expected Buffer') }
  29. if (source.length === 0) { return '' }
  30. // Skip & count leading zeroes.
  31. var zeroes = 0
  32. var length = 0
  33. var pbegin = 0
  34. var pend = source.length
  35. while (pbegin !== pend && source[pbegin] === 0) {
  36. pbegin++
  37. zeroes++
  38. }
  39. // Allocate enough space in big-endian base58 representation.
  40. var size = ((pend - pbegin) * iFACTOR + 1) >>> 0
  41. var b58 = new Uint8Array(size)
  42. // Process the bytes.
  43. while (pbegin !== pend) {
  44. var carry = source[pbegin]
  45. // Apply "b58 = b58 * 256 + ch".
  46. var i = 0
  47. for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
  48. carry += (256 * b58[it1]) >>> 0
  49. b58[it1] = (carry % BASE) >>> 0
  50. carry = (carry / BASE) >>> 0
  51. }
  52. if (carry !== 0) { throw new Error('Non-zero carry') }
  53. length = i
  54. pbegin++
  55. }
  56. // Skip leading zeroes in base58 result.
  57. var it2 = size - length
  58. while (it2 !== size && b58[it2] === 0) {
  59. it2++
  60. }
  61. // Translate the result into a string.
  62. var str = LEADER.repeat(zeroes)
  63. for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }
  64. return str
  65. }
  66. function decodeUnsafe (source) {
  67. if (typeof source !== 'string') { throw new TypeError('Expected String') }
  68. if (source.length === 0) { return _Buffer.alloc(0) }
  69. var psz = 0
  70. // Skip leading spaces.
  71. if (source[psz] === ' ') { return }
  72. // Skip and count leading '1's.
  73. var zeroes = 0
  74. var length = 0
  75. while (source[psz] === LEADER) {
  76. zeroes++
  77. psz++
  78. }
  79. // Allocate enough space in big-endian base256 representation.
  80. var size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
  81. var b256 = new Uint8Array(size)
  82. // Process the characters.
  83. while (source[psz]) {
  84. // Decode character
  85. var carry = BASE_MAP[source.charCodeAt(psz)]
  86. // Invalid character
  87. if (carry === 255) { return }
  88. var i = 0
  89. for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
  90. carry += (BASE * b256[it3]) >>> 0
  91. b256[it3] = (carry % 256) >>> 0
  92. carry = (carry / 256) >>> 0
  93. }
  94. if (carry !== 0) { throw new Error('Non-zero carry') }
  95. length = i
  96. psz++
  97. }
  98. // Skip trailing spaces.
  99. if (source[psz] === ' ') { return }
  100. // Skip leading zeroes in b256.
  101. var it4 = size - length
  102. while (it4 !== size && b256[it4] === 0) {
  103. it4++
  104. }
  105. var vch = _Buffer.allocUnsafe(zeroes + (size - it4))
  106. vch.fill(0x00, 0, zeroes)
  107. var j = zeroes
  108. while (it4 !== size) {
  109. vch[j++] = b256[it4++]
  110. }
  111. return vch
  112. }
  113. function decode (string) {
  114. var buffer = decodeUnsafe(string)
  115. if (buffer) { return buffer }
  116. throw new Error('Non-base' + BASE + ' character')
  117. }
  118. return {
  119. encode: encode,
  120. decodeUnsafe: decodeUnsafe,
  121. decode: decode
  122. }
  123. }
  124. module.exports = base
  125. },{"safe-buffer":28}],2:[function(require,module,exports){
  126. var basex = require('base-x')
  127. var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  128. module.exports = basex(ALPHABET)
  129. },{"base-x":1}],3:[function(require,module,exports){
  130. var Buffer = require('safe-buffer').Buffer
  131. var Transform = require('stream').Transform
  132. var StringDecoder = require('string_decoder').StringDecoder
  133. var inherits = require('inherits')
  134. function CipherBase (hashMode) {
  135. Transform.call(this)
  136. this.hashMode = typeof hashMode === 'string'
  137. if (this.hashMode) {
  138. this[hashMode] = this._finalOrDigest
  139. } else {
  140. this.final = this._finalOrDigest
  141. }
  142. if (this._final) {
  143. this.__final = this._final
  144. this._final = null
  145. }
  146. this._decoder = null
  147. this._encoding = null
  148. }
  149. inherits(CipherBase, Transform)
  150. CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
  151. if (typeof data === 'string') {
  152. data = Buffer.from(data, inputEnc)
  153. }
  154. var outData = this._update(data)
  155. if (this.hashMode) return this
  156. if (outputEnc) {
  157. outData = this._toString(outData, outputEnc)
  158. }
  159. return outData
  160. }
  161. CipherBase.prototype.setAutoPadding = function () {}
  162. CipherBase.prototype.getAuthTag = function () {
  163. throw new Error('trying to get auth tag in unsupported state')
  164. }
  165. CipherBase.prototype.setAuthTag = function () {
  166. throw new Error('trying to set auth tag in unsupported state')
  167. }
  168. CipherBase.prototype.setAAD = function () {
  169. throw new Error('trying to set aad in unsupported state')
  170. }
  171. CipherBase.prototype._transform = function (data, _, next) {
  172. var err
  173. try {
  174. if (this.hashMode) {
  175. this._update(data)
  176. } else {
  177. this.push(this._update(data))
  178. }
  179. } catch (e) {
  180. err = e
  181. } finally {
  182. next(err)
  183. }
  184. }
  185. CipherBase.prototype._flush = function (done) {
  186. var err
  187. try {
  188. this.push(this.__final())
  189. } catch (e) {
  190. err = e
  191. }
  192. done(err)
  193. }
  194. CipherBase.prototype._finalOrDigest = function (outputEnc) {
  195. var outData = this.__final() || Buffer.alloc(0)
  196. if (outputEnc) {
  197. outData = this._toString(outData, outputEnc, true)
  198. }
  199. return outData
  200. }
  201. CipherBase.prototype._toString = function (value, enc, fin) {
  202. if (!this._decoder) {
  203. this._decoder = new StringDecoder(enc)
  204. this._encoding = enc
  205. }
  206. if (this._encoding !== enc) throw new Error('can\'t switch encodings')
  207. var out = this._decoder.write(value)
  208. if (fin) {
  209. out += this._decoder.end()
  210. }
  211. return out
  212. }
  213. module.exports = CipherBase
  214. },{"inherits":10,"safe-buffer":28,"stream":49,"string_decoder":64}],4:[function(require,module,exports){
  215. var MD5 = require('md5.js')
  216. module.exports = function (buffer) {
  217. return new MD5().update(buffer).digest()
  218. }
  219. },{"md5.js":11}],5:[function(require,module,exports){
  220. 'use strict'
  221. var inherits = require('inherits')
  222. var Legacy = require('./legacy')
  223. var Base = require('cipher-base')
  224. var Buffer = require('safe-buffer').Buffer
  225. var md5 = require('create-hash/md5')
  226. var RIPEMD160 = require('ripemd160')
  227. var sha = require('sha.js')
  228. var ZEROS = Buffer.alloc(128)
  229. function Hmac (alg, key) {
  230. Base.call(this, 'digest')
  231. if (typeof key === 'string') {
  232. key = Buffer.from(key)
  233. }
  234. var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64
  235. this._alg = alg
  236. this._key = key
  237. if (key.length > blocksize) {
  238. var hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg)
  239. key = hash.update(key).digest()
  240. } else if (key.length < blocksize) {
  241. key = Buffer.concat([key, ZEROS], blocksize)
  242. }
  243. var ipad = this._ipad = Buffer.allocUnsafe(blocksize)
  244. var opad = this._opad = Buffer.allocUnsafe(blocksize)
  245. for (var i = 0; i < blocksize; i++) {
  246. ipad[i] = key[i] ^ 0x36
  247. opad[i] = key[i] ^ 0x5C
  248. }
  249. this._hash = alg === 'rmd160' ? new RIPEMD160() : sha(alg)
  250. this._hash.update(ipad)
  251. }
  252. inherits(Hmac, Base)
  253. Hmac.prototype._update = function (data) {
  254. this._hash.update(data)
  255. }
  256. Hmac.prototype._final = function () {
  257. var h = this._hash.digest()
  258. var hash = this._alg === 'rmd160' ? new RIPEMD160() : sha(this._alg)
  259. return hash.update(this._opad).update(h).digest()
  260. }
  261. module.exports = function createHmac (alg, key) {
  262. alg = alg.toLowerCase()
  263. if (alg === 'rmd160' || alg === 'ripemd160') {
  264. return new Hmac('rmd160', key)
  265. }
  266. if (alg === 'md5') {
  267. return new Legacy(md5, key)
  268. }
  269. return new Hmac(alg, key)
  270. }
  271. },{"./legacy":6,"cipher-base":3,"create-hash/md5":4,"inherits":10,"ripemd160":27,"safe-buffer":28,"sha.js":30}],6:[function(require,module,exports){
  272. 'use strict'
  273. var inherits = require('inherits')
  274. var Buffer = require('safe-buffer').Buffer
  275. var Base = require('cipher-base')
  276. var ZEROS = Buffer.alloc(128)
  277. var blocksize = 64
  278. function Hmac (alg, key) {
  279. Base.call(this, 'digest')
  280. if (typeof key === 'string') {
  281. key = Buffer.from(key)
  282. }
  283. this._alg = alg
  284. this._key = key
  285. if (key.length > blocksize) {
  286. key = alg(key)
  287. } else if (key.length < blocksize) {
  288. key = Buffer.concat([key, ZEROS], blocksize)
  289. }
  290. var ipad = this._ipad = Buffer.allocUnsafe(blocksize)
  291. var opad = this._opad = Buffer.allocUnsafe(blocksize)
  292. for (var i = 0; i < blocksize; i++) {
  293. ipad[i] = key[i] ^ 0x36
  294. opad[i] = key[i] ^ 0x5C
  295. }
  296. this._hash = [ipad]
  297. }
  298. inherits(Hmac, Base)
  299. Hmac.prototype._update = function (data) {
  300. this._hash.push(data)
  301. }
  302. Hmac.prototype._final = function () {
  303. var h = this._alg(Buffer.concat(this._hash))
  304. return this._alg(Buffer.concat([this._opad, h]))
  305. }
  306. module.exports = Hmac
  307. },{"cipher-base":3,"inherits":10,"safe-buffer":28}],7:[function(require,module,exports){
  308. (function (Buffer){(function (){
  309. "use strict";
  310. Object.defineProperty(exports, "__esModule", { value: true });
  311. exports.derivePath = exports.isValidPath = exports.getPublicKey = exports.getMasterKeyFromSeed = void 0;
  312. const createHmac = require("create-hmac");
  313. const nacl = require("tweetnacl");
  314. const utils_1 = require("./utils");
  315. const ED25519_CURVE = 'ed25519 seed';
  316. const HARDENED_OFFSET = 0x80000000;
  317. exports.getMasterKeyFromSeed = (seed) => {
  318. const hmac = createHmac('sha512', ED25519_CURVE);
  319. const I = hmac.update(Buffer.from(seed, 'hex')).digest();
  320. const IL = I.slice(0, 32);
  321. const IR = I.slice(32);
  322. return {
  323. key: IL,
  324. chainCode: IR,
  325. };
  326. };
  327. const CKDPriv = ({ key, chainCode }, index) => {
  328. const indexBuffer = Buffer.allocUnsafe(4);
  329. indexBuffer.writeUInt32BE(index, 0);
  330. const data = Buffer.concat([Buffer.alloc(1, 0), key, indexBuffer]);
  331. const I = createHmac('sha512', chainCode)
  332. .update(data)
  333. .digest();
  334. const IL = I.slice(0, 32);
  335. const IR = I.slice(32);
  336. return {
  337. key: IL,
  338. chainCode: IR,
  339. };
  340. };
  341. exports.getPublicKey = (privateKey, withZeroByte = true) => {
  342. const keyPair = nacl.sign.keyPair.fromSeed(privateKey);
  343. const signPk = keyPair.secretKey.subarray(32);
  344. const zero = Buffer.alloc(1, 0);
  345. return withZeroByte ?
  346. Buffer.concat([zero, Buffer.from(signPk)]) :
  347. Buffer.from(signPk);
  348. };
  349. exports.isValidPath = (path) => {
  350. if (!utils_1.pathRegex.test(path)) {
  351. return false;
  352. }
  353. return !path
  354. .split('/')
  355. .slice(1)
  356. .map(utils_1.replaceDerive)
  357. .some(isNaN);
  358. };
  359. exports.derivePath = (path, seed, offset = HARDENED_OFFSET) => {
  360. if (!exports.isValidPath(path)) {
  361. throw new Error('Invalid derivation path');
  362. }
  363. const { key, chainCode } = exports.getMasterKeyFromSeed(seed);
  364. const segments = path
  365. .split('/')
  366. .slice(1)
  367. .map(utils_1.replaceDerive)
  368. .map(el => parseInt(el, 10));
  369. return segments.reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), { key, chainCode });
  370. };
  371. }).call(this)}).call(this,require("buffer").Buffer)
  372. },{"./utils":8,"buffer":43,"create-hmac":5,"tweetnacl":38}],8:[function(require,module,exports){
  373. "use strict";
  374. Object.defineProperty(exports, "__esModule", { value: true });
  375. exports.replaceDerive = exports.pathRegex = void 0;
  376. exports.pathRegex = new RegExp("^m(\\/[0-9]+')+$");
  377. exports.replaceDerive = (val) => val.replace("'", '');
  378. },{}],9:[function(require,module,exports){
  379. 'use strict'
  380. var Buffer = require('safe-buffer').Buffer
  381. var Transform = require('readable-stream').Transform
  382. var inherits = require('inherits')
  383. function throwIfNotStringOrBuffer (val, prefix) {
  384. if (!Buffer.isBuffer(val) && typeof val !== 'string') {
  385. throw new TypeError(prefix + ' must be a string or a buffer')
  386. }
  387. }
  388. function HashBase (blockSize) {
  389. Transform.call(this)
  390. this._block = Buffer.allocUnsafe(blockSize)
  391. this._blockSize = blockSize
  392. this._blockOffset = 0
  393. this._length = [0, 0, 0, 0]
  394. this._finalized = false
  395. }
  396. inherits(HashBase, Transform)
  397. HashBase.prototype._transform = function (chunk, encoding, callback) {
  398. var error = null
  399. try {
  400. this.update(chunk, encoding)
  401. } catch (err) {
  402. error = err
  403. }
  404. callback(error)
  405. }
  406. HashBase.prototype._flush = function (callback) {
  407. var error = null
  408. try {
  409. this.push(this.digest())
  410. } catch (err) {
  411. error = err
  412. }
  413. callback(error)
  414. }
  415. HashBase.prototype.update = function (data, encoding) {
  416. throwIfNotStringOrBuffer(data, 'Data')
  417. if (this._finalized) throw new Error('Digest already called')
  418. if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)
  419. // consume data
  420. var block = this._block
  421. var offset = 0
  422. while (this._blockOffset + data.length - offset >= this._blockSize) {
  423. for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++]
  424. this._update()
  425. this._blockOffset = 0
  426. }
  427. while (offset < data.length) block[this._blockOffset++] = data[offset++]
  428. // update length
  429. for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
  430. this._length[j] += carry
  431. carry = (this._length[j] / 0x0100000000) | 0
  432. if (carry > 0) this._length[j] -= 0x0100000000 * carry
  433. }
  434. return this
  435. }
  436. HashBase.prototype._update = function () {
  437. throw new Error('_update is not implemented')
  438. }
  439. HashBase.prototype.digest = function (encoding) {
  440. if (this._finalized) throw new Error('Digest already called')
  441. this._finalized = true
  442. var digest = this._digest()
  443. if (encoding !== undefined) digest = digest.toString(encoding)
  444. // reset state
  445. this._block.fill(0)
  446. this._blockOffset = 0
  447. for (var i = 0; i < 4; ++i) this._length[i] = 0
  448. return digest
  449. }
  450. HashBase.prototype._digest = function () {
  451. throw new Error('_digest is not implemented')
  452. }
  453. module.exports = HashBase
  454. },{"inherits":10,"readable-stream":26,"safe-buffer":28}],10:[function(require,module,exports){
  455. if (typeof Object.create === 'function') {
  456. // implementation from standard node.js 'util' module
  457. module.exports = function inherits(ctor, superCtor) {
  458. if (superCtor) {
  459. ctor.super_ = superCtor
  460. ctor.prototype = Object.create(superCtor.prototype, {
  461. constructor: {
  462. value: ctor,
  463. enumerable: false,
  464. writable: true,
  465. configurable: true
  466. }
  467. })
  468. }
  469. };
  470. } else {
  471. // old school shim for old browsers
  472. module.exports = function inherits(ctor, superCtor) {
  473. if (superCtor) {
  474. ctor.super_ = superCtor
  475. var TempCtor = function () {}
  476. TempCtor.prototype = superCtor.prototype
  477. ctor.prototype = new TempCtor()
  478. ctor.prototype.constructor = ctor
  479. }
  480. }
  481. }
  482. },{}],11:[function(require,module,exports){
  483. 'use strict'
  484. var inherits = require('inherits')
  485. var HashBase = require('hash-base')
  486. var Buffer = require('safe-buffer').Buffer
  487. var ARRAY16 = new Array(16)
  488. function MD5 () {
  489. HashBase.call(this, 64)
  490. // state
  491. this._a = 0x67452301
  492. this._b = 0xefcdab89
  493. this._c = 0x98badcfe
  494. this._d = 0x10325476
  495. }
  496. inherits(MD5, HashBase)
  497. MD5.prototype._update = function () {
  498. var M = ARRAY16
  499. for (var i = 0; i < 16; ++i) M[i] = this._block.readInt32LE(i * 4)
  500. var a = this._a
  501. var b = this._b
  502. var c = this._c
  503. var d = this._d
  504. a = fnF(a, b, c, d, M[0], 0xd76aa478, 7)
  505. d = fnF(d, a, b, c, M[1], 0xe8c7b756, 12)
  506. c = fnF(c, d, a, b, M[2], 0x242070db, 17)
  507. b = fnF(b, c, d, a, M[3], 0xc1bdceee, 22)
  508. a = fnF(a, b, c, d, M[4], 0xf57c0faf, 7)
  509. d = fnF(d, a, b, c, M[5], 0x4787c62a, 12)
  510. c = fnF(c, d, a, b, M[6], 0xa8304613, 17)
  511. b = fnF(b, c, d, a, M[7], 0xfd469501, 22)
  512. a = fnF(a, b, c, d, M[8], 0x698098d8, 7)
  513. d = fnF(d, a, b, c, M[9], 0x8b44f7af, 12)
  514. c = fnF(c, d, a, b, M[10], 0xffff5bb1, 17)
  515. b = fnF(b, c, d, a, M[11], 0x895cd7be, 22)
  516. a = fnF(a, b, c, d, M[12], 0x6b901122, 7)
  517. d = fnF(d, a, b, c, M[13], 0xfd987193, 12)
  518. c = fnF(c, d, a, b, M[14], 0xa679438e, 17)
  519. b = fnF(b, c, d, a, M[15], 0x49b40821, 22)
  520. a = fnG(a, b, c, d, M[1], 0xf61e2562, 5)
  521. d = fnG(d, a, b, c, M[6], 0xc040b340, 9)
  522. c = fnG(c, d, a, b, M[11], 0x265e5a51, 14)
  523. b = fnG(b, c, d, a, M[0], 0xe9b6c7aa, 20)
  524. a = fnG(a, b, c, d, M[5], 0xd62f105d, 5)
  525. d = fnG(d, a, b, c, M[10], 0x02441453, 9)
  526. c = fnG(c, d, a, b, M[15], 0xd8a1e681, 14)
  527. b = fnG(b, c, d, a, M[4], 0xe7d3fbc8, 20)
  528. a = fnG(a, b, c, d, M[9], 0x21e1cde6, 5)
  529. d = fnG(d, a, b, c, M[14], 0xc33707d6, 9)
  530. c = fnG(c, d, a, b, M[3], 0xf4d50d87, 14)
  531. b = fnG(b, c, d, a, M[8], 0x455a14ed, 20)
  532. a = fnG(a, b, c, d, M[13], 0xa9e3e905, 5)
  533. d = fnG(d, a, b, c, M[2], 0xfcefa3f8, 9)
  534. c = fnG(c, d, a, b, M[7], 0x676f02d9, 14)
  535. b = fnG(b, c, d, a, M[12], 0x8d2a4c8a, 20)
  536. a = fnH(a, b, c, d, M[5], 0xfffa3942, 4)
  537. d = fnH(d, a, b, c, M[8], 0x8771f681, 11)
  538. c = fnH(c, d, a, b, M[11], 0x6d9d6122, 16)
  539. b = fnH(b, c, d, a, M[14], 0xfde5380c, 23)
  540. a = fnH(a, b, c, d, M[1], 0xa4beea44, 4)
  541. d = fnH(d, a, b, c, M[4], 0x4bdecfa9, 11)
  542. c = fnH(c, d, a, b, M[7], 0xf6bb4b60, 16)
  543. b = fnH(b, c, d, a, M[10], 0xbebfbc70, 23)
  544. a = fnH(a, b, c, d, M[13], 0x289b7ec6, 4)
  545. d = fnH(d, a, b, c, M[0], 0xeaa127fa, 11)
  546. c = fnH(c, d, a, b, M[3], 0xd4ef3085, 16)
  547. b = fnH(b, c, d, a, M[6], 0x04881d05, 23)
  548. a = fnH(a, b, c, d, M[9], 0xd9d4d039, 4)
  549. d = fnH(d, a, b, c, M[12], 0xe6db99e5, 11)
  550. c = fnH(c, d, a, b, M[15], 0x1fa27cf8, 16)
  551. b = fnH(b, c, d, a, M[2], 0xc4ac5665, 23)
  552. a = fnI(a, b, c, d, M[0], 0xf4292244, 6)
  553. d = fnI(d, a, b, c, M[7], 0x432aff97, 10)
  554. c = fnI(c, d, a, b, M[14], 0xab9423a7, 15)
  555. b = fnI(b, c, d, a, M[5], 0xfc93a039, 21)
  556. a = fnI(a, b, c, d, M[12], 0x655b59c3, 6)
  557. d = fnI(d, a, b, c, M[3], 0x8f0ccc92, 10)
  558. c = fnI(c, d, a, b, M[10], 0xffeff47d, 15)
  559. b = fnI(b, c, d, a, M[1], 0x85845dd1, 21)
  560. a = fnI(a, b, c, d, M[8], 0x6fa87e4f, 6)
  561. d = fnI(d, a, b, c, M[15], 0xfe2ce6e0, 10)
  562. c = fnI(c, d, a, b, M[6], 0xa3014314, 15)
  563. b = fnI(b, c, d, a, M[13], 0x4e0811a1, 21)
  564. a = fnI(a, b, c, d, M[4], 0xf7537e82, 6)
  565. d = fnI(d, a, b, c, M[11], 0xbd3af235, 10)
  566. c = fnI(c, d, a, b, M[2], 0x2ad7d2bb, 15)
  567. b = fnI(b, c, d, a, M[9], 0xeb86d391, 21)
  568. this._a = (this._a + a) | 0
  569. this._b = (this._b + b) | 0
  570. this._c = (this._c + c) | 0
  571. this._d = (this._d + d) | 0
  572. }
  573. MD5.prototype._digest = function () {
  574. // create padding and handle blocks
  575. this._block[this._blockOffset++] = 0x80
  576. if (this._blockOffset > 56) {
  577. this._block.fill(0, this._blockOffset, 64)
  578. this._update()
  579. this._blockOffset = 0
  580. }
  581. this._block.fill(0, this._blockOffset, 56)
  582. this._block.writeUInt32LE(this._length[0], 56)
  583. this._block.writeUInt32LE(this._length[1], 60)
  584. this._update()
  585. // produce result
  586. var buffer = Buffer.allocUnsafe(16)
  587. buffer.writeInt32LE(this._a, 0)
  588. buffer.writeInt32LE(this._b, 4)
  589. buffer.writeInt32LE(this._c, 8)
  590. buffer.writeInt32LE(this._d, 12)
  591. return buffer
  592. }
  593. function rotl (x, n) {
  594. return (x << n) | (x >>> (32 - n))
  595. }
  596. function fnF (a, b, c, d, m, k, s) {
  597. return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + b) | 0
  598. }
  599. function fnG (a, b, c, d, m, k, s) {
  600. return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + b) | 0
  601. }
  602. function fnH (a, b, c, d, m, k, s) {
  603. return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + b) | 0
  604. }
  605. function fnI (a, b, c, d, m, k, s) {
  606. return (rotl((a + ((c ^ (b | (~d)))) + m + k) | 0, s) + b) | 0
  607. }
  608. module.exports = MD5
  609. },{"hash-base":9,"inherits":10,"safe-buffer":28}],12:[function(require,module,exports){
  610. 'use strict';
  611. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  612. var codes = {};
  613. function createErrorType(code, message, Base) {
  614. if (!Base) {
  615. Base = Error;
  616. }
  617. function getMessage(arg1, arg2, arg3) {
  618. if (typeof message === 'string') {
  619. return message;
  620. } else {
  621. return message(arg1, arg2, arg3);
  622. }
  623. }
  624. var NodeError =
  625. /*#__PURE__*/
  626. function (_Base) {
  627. _inheritsLoose(NodeError, _Base);
  628. function NodeError(arg1, arg2, arg3) {
  629. return _Base.call(this, getMessage(arg1, arg2, arg3)) || this;
  630. }
  631. return NodeError;
  632. }(Base);
  633. NodeError.prototype.name = Base.name;
  634. NodeError.prototype.code = code;
  635. codes[code] = NodeError;
  636. } // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
  637. function oneOf(expected, thing) {
  638. if (Array.isArray(expected)) {
  639. var len = expected.length;
  640. expected = expected.map(function (i) {
  641. return String(i);
  642. });
  643. if (len > 2) {
  644. return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];
  645. } else if (len === 2) {
  646. return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);
  647. } else {
  648. return "of ".concat(thing, " ").concat(expected[0]);
  649. }
  650. } else {
  651. return "of ".concat(thing, " ").concat(String(expected));
  652. }
  653. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
  654. function startsWith(str, search, pos) {
  655. return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
  656. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
  657. function endsWith(str, search, this_len) {
  658. if (this_len === undefined || this_len > str.length) {
  659. this_len = str.length;
  660. }
  661. return str.substring(this_len - search.length, this_len) === search;
  662. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
  663. function includes(str, search, start) {
  664. if (typeof start !== 'number') {
  665. start = 0;
  666. }
  667. if (start + search.length > str.length) {
  668. return false;
  669. } else {
  670. return str.indexOf(search, start) !== -1;
  671. }
  672. }
  673. createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
  674. return 'The value "' + value + '" is invalid for option "' + name + '"';
  675. }, TypeError);
  676. createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
  677. // determiner: 'must be' or 'must not be'
  678. var determiner;
  679. if (typeof expected === 'string' && startsWith(expected, 'not ')) {
  680. determiner = 'must not be';
  681. expected = expected.replace(/^not /, '');
  682. } else {
  683. determiner = 'must be';
  684. }
  685. var msg;
  686. if (endsWith(name, ' argument')) {
  687. // For cases like 'first argument'
  688. msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
  689. } else {
  690. var type = includes(name, '.') ? 'property' : 'argument';
  691. msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
  692. }
  693. msg += ". Received type ".concat(typeof actual);
  694. return msg;
  695. }, TypeError);
  696. createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
  697. createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
  698. return 'The ' + name + ' method is not implemented';
  699. });
  700. createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
  701. createErrorType('ERR_STREAM_DESTROYED', function (name) {
  702. return 'Cannot call ' + name + ' after a stream was destroyed';
  703. });
  704. createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
  705. createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
  706. createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
  707. createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
  708. createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
  709. return 'Unknown encoding: ' + arg;
  710. }, TypeError);
  711. createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
  712. module.exports.codes = codes;
  713. },{}],13:[function(require,module,exports){
  714. (function (process){(function (){
  715. // Copyright Joyent, Inc. and other Node contributors.
  716. //
  717. // Permission is hereby granted, free of charge, to any person obtaining a
  718. // copy of this software and associated documentation files (the
  719. // "Software"), to deal in the Software without restriction, including
  720. // without limitation the rights to use, copy, modify, merge, publish,
  721. // distribute, sublicense, and/or sell copies of the Software, and to permit
  722. // persons to whom the Software is furnished to do so, subject to the
  723. // following conditions:
  724. //
  725. // The above copyright notice and this permission notice shall be included
  726. // in all copies or substantial portions of the Software.
  727. //
  728. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  729. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  730. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  731. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  732. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  733. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  734. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  735. // a duplex stream is just a stream that is both readable and writable.
  736. // Since JS doesn't have multiple prototypal inheritance, this class
  737. // prototypally inherits from Readable, and then parasitically from
  738. // Writable.
  739. 'use strict';
  740. /*<replacement>*/
  741. var objectKeys = Object.keys || function (obj) {
  742. var keys = [];
  743. for (var key in obj) {
  744. keys.push(key);
  745. }
  746. return keys;
  747. };
  748. /*</replacement>*/
  749. module.exports = Duplex;
  750. var Readable = require('./_stream_readable');
  751. var Writable = require('./_stream_writable');
  752. require('inherits')(Duplex, Readable);
  753. {
  754. // Allow the keys array to be GC'ed.
  755. var keys = objectKeys(Writable.prototype);
  756. for (var v = 0; v < keys.length; v++) {
  757. var method = keys[v];
  758. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
  759. }
  760. }
  761. function Duplex(options) {
  762. if (!(this instanceof Duplex)) return new Duplex(options);
  763. Readable.call(this, options);
  764. Writable.call(this, options);
  765. this.allowHalfOpen = true;
  766. if (options) {
  767. if (options.readable === false) this.readable = false;
  768. if (options.writable === false) this.writable = false;
  769. if (options.allowHalfOpen === false) {
  770. this.allowHalfOpen = false;
  771. this.once('end', onend);
  772. }
  773. }
  774. }
  775. Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
  776. // making it explicit this property is not enumerable
  777. // because otherwise some prototype manipulation in
  778. // userland will fail
  779. enumerable: false,
  780. get: function get() {
  781. return this._writableState.highWaterMark;
  782. }
  783. });
  784. Object.defineProperty(Duplex.prototype, 'writableBuffer', {
  785. // making it explicit this property is not enumerable
  786. // because otherwise some prototype manipulation in
  787. // userland will fail
  788. enumerable: false,
  789. get: function get() {
  790. return this._writableState && this._writableState.getBuffer();
  791. }
  792. });
  793. Object.defineProperty(Duplex.prototype, 'writableLength', {
  794. // making it explicit this property is not enumerable
  795. // because otherwise some prototype manipulation in
  796. // userland will fail
  797. enumerable: false,
  798. get: function get() {
  799. return this._writableState.length;
  800. }
  801. }); // the no-half-open enforcer
  802. function onend() {
  803. // If the writable side ended, then we're ok.
  804. if (this._writableState.ended) return; // no more data can be written.
  805. // But allow more writes to happen in this tick.
  806. process.nextTick(onEndNT, this);
  807. }
  808. function onEndNT(self) {
  809. self.end();
  810. }
  811. Object.defineProperty(Duplex.prototype, 'destroyed', {
  812. // making it explicit this property is not enumerable
  813. // because otherwise some prototype manipulation in
  814. // userland will fail
  815. enumerable: false,
  816. get: function get() {
  817. if (this._readableState === undefined || this._writableState === undefined) {
  818. return false;
  819. }
  820. return this._readableState.destroyed && this._writableState.destroyed;
  821. },
  822. set: function set(value) {
  823. // we ignore the value if the stream
  824. // has not been initialized yet
  825. if (this._readableState === undefined || this._writableState === undefined) {
  826. return;
  827. } // backward compatibility, the user is explicitly
  828. // managing destroyed
  829. this._readableState.destroyed = value;
  830. this._writableState.destroyed = value;
  831. }
  832. });
  833. }).call(this)}).call(this,require('_process'))
  834. },{"./_stream_readable":15,"./_stream_writable":17,"_process":47,"inherits":10}],14:[function(require,module,exports){
  835. // Copyright Joyent, Inc. and other Node contributors.
  836. //
  837. // Permission is hereby granted, free of charge, to any person obtaining a
  838. // copy of this software and associated documentation files (the
  839. // "Software"), to deal in the Software without restriction, including
  840. // without limitation the rights to use, copy, modify, merge, publish,
  841. // distribute, sublicense, and/or sell copies of the Software, and to permit
  842. // persons to whom the Software is furnished to do so, subject to the
  843. // following conditions:
  844. //
  845. // The above copyright notice and this permission notice shall be included
  846. // in all copies or substantial portions of the Software.
  847. //
  848. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  849. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  850. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  851. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  852. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  853. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  854. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  855. // a passthrough stream.
  856. // basically just the most minimal sort of Transform stream.
  857. // Every written chunk gets output as-is.
  858. 'use strict';
  859. module.exports = PassThrough;
  860. var Transform = require('./_stream_transform');
  861. require('inherits')(PassThrough, Transform);
  862. function PassThrough(options) {
  863. if (!(this instanceof PassThrough)) return new PassThrough(options);
  864. Transform.call(this, options);
  865. }
  866. PassThrough.prototype._transform = function (chunk, encoding, cb) {
  867. cb(null, chunk);
  868. };
  869. },{"./_stream_transform":16,"inherits":10}],15:[function(require,module,exports){
  870. (function (process,global){(function (){
  871. // Copyright Joyent, Inc. and other Node contributors.
  872. //
  873. // Permission is hereby granted, free of charge, to any person obtaining a
  874. // copy of this software and associated documentation files (the
  875. // "Software"), to deal in the Software without restriction, including
  876. // without limitation the rights to use, copy, modify, merge, publish,
  877. // distribute, sublicense, and/or sell copies of the Software, and to permit
  878. // persons to whom the Software is furnished to do so, subject to the
  879. // following conditions:
  880. //
  881. // The above copyright notice and this permission notice shall be included
  882. // in all copies or substantial portions of the Software.
  883. //
  884. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  885. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  886. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  887. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  888. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  889. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  890. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  891. 'use strict';
  892. module.exports = Readable;
  893. /*<replacement>*/
  894. var Duplex;
  895. /*</replacement>*/
  896. Readable.ReadableState = ReadableState;
  897. /*<replacement>*/
  898. var EE = require('events').EventEmitter;
  899. var EElistenerCount = function EElistenerCount(emitter, type) {
  900. return emitter.listeners(type).length;
  901. };
  902. /*</replacement>*/
  903. /*<replacement>*/
  904. var Stream = require('./internal/streams/stream');
  905. /*</replacement>*/
  906. var Buffer = require('buffer').Buffer;
  907. var OurUint8Array = global.Uint8Array || function () {};
  908. function _uint8ArrayToBuffer(chunk) {
  909. return Buffer.from(chunk);
  910. }
  911. function _isUint8Array(obj) {
  912. return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
  913. }
  914. /*<replacement>*/
  915. var debugUtil = require('util');
  916. var debug;
  917. if (debugUtil && debugUtil.debuglog) {
  918. debug = debugUtil.debuglog('stream');
  919. } else {
  920. debug = function debug() {};
  921. }
  922. /*</replacement>*/
  923. var BufferList = require('./internal/streams/buffer_list');
  924. var destroyImpl = require('./internal/streams/destroy');
  925. var _require = require('./internal/streams/state'),
  926. getHighWaterMark = _require.getHighWaterMark;
  927. var _require$codes = require('../errors').codes,
  928. ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE,
  929. ERR_STREAM_PUSH_AFTER_EOF = _require$codes.ERR_STREAM_PUSH_AFTER_EOF,
  930. ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
  931. ERR_STREAM_UNSHIFT_AFTER_END_EVENT = _require$codes.ERR_STREAM_UNSHIFT_AFTER_END_EVENT; // Lazy loaded to improve the startup performance.
  932. var StringDecoder;
  933. var createReadableStreamAsyncIterator;
  934. var from;
  935. require('inherits')(Readable, Stream);
  936. var errorOrDestroy = destroyImpl.errorOrDestroy;
  937. var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
  938. function prependListener(emitter, event, fn) {
  939. // Sadly this is not cacheable as some libraries bundle their own
  940. // event emitter implementation with them.
  941. if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); // This is a hack to make sure that our error handler is attached before any
  942. // userland ones. NEVER DO THIS. This is here only because this code needs
  943. // to continue to work with older versions of Node.js that do not include
  944. // the prependListener() method. The goal is to eventually remove this hack.
  945. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (Array.isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
  946. }
  947. function ReadableState(options, stream, isDuplex) {
  948. Duplex = Duplex || require('./_stream_duplex');
  949. options = options || {}; // Duplex streams are both readable and writable, but share
  950. // the same options object.
  951. // However, some cases require setting options to different
  952. // values for the readable and the writable sides of the duplex stream.
  953. // These options can be provided separately as readableXXX and writableXXX.
  954. if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag. Used to make read(n) ignore n and to
  955. // make all the buffer merging and length checks go away
  956. this.objectMode = !!options.objectMode;
  957. if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; // the point at which it stops calling _read() to fill the buffer
  958. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  959. this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex); // A linked list is used to store data chunks instead of an array because the
  960. // linked list can remove elements from the beginning faster than
  961. // array.shift()
  962. this.buffer = new BufferList();
  963. this.length = 0;
  964. this.pipes = null;
  965. this.pipesCount = 0;
  966. this.flowing = null;
  967. this.ended = false;
  968. this.endEmitted = false;
  969. this.reading = false; // a flag to be able to tell if the event 'readable'/'data' is emitted
  970. // immediately, or on a later tick. We set this to true at first, because
  971. // any actions that shouldn't happen until "later" should generally also
  972. // not happen before the first read call.
  973. this.sync = true; // whenever we return null, then we set a flag to say
  974. // that we're awaiting a 'readable' event emission.
  975. this.needReadable = false;
  976. this.emittedReadable = false;
  977. this.readableListening = false;
  978. this.resumeScheduled = false;
  979. this.paused = true; // Should close be emitted on destroy. Defaults to true.
  980. this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'end' (and potentially 'finish')
  981. this.autoDestroy = !!options.autoDestroy; // has it been destroyed
  982. this.destroyed = false; // Crypto is kind of old and crusty. Historically, its default string
  983. // encoding is 'binary' so we have to make this configurable.
  984. // Everything else in the universe uses 'utf8', though.
  985. this.defaultEncoding = options.defaultEncoding || 'utf8'; // the number of writers that are awaiting a drain event in .pipe()s
  986. this.awaitDrain = 0; // if true, a maybeReadMore has been scheduled
  987. this.readingMore = false;
  988. this.decoder = null;
  989. this.encoding = null;
  990. if (options.encoding) {
  991. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  992. this.decoder = new StringDecoder(options.encoding);
  993. this.encoding = options.encoding;
  994. }
  995. }
  996. function Readable(options) {
  997. Duplex = Duplex || require('./_stream_duplex');
  998. if (!(this instanceof Readable)) return new Readable(options); // Checking for a Stream.Duplex instance is faster here instead of inside
  999. // the ReadableState constructor, at least with V8 6.5
  1000. var isDuplex = this instanceof Duplex;
  1001. this._readableState = new ReadableState(options, this, isDuplex); // legacy
  1002. this.readable = true;
  1003. if (options) {
  1004. if (typeof options.read === 'function') this._read = options.read;
  1005. if (typeof options.destroy === 'function') this._destroy = options.destroy;
  1006. }
  1007. Stream.call(this);
  1008. }
  1009. Object.defineProperty(Readable.prototype, 'destroyed', {
  1010. // making it explicit this property is not enumerable
  1011. // because otherwise some prototype manipulation in
  1012. // userland will fail
  1013. enumerable: false,
  1014. get: function get() {
  1015. if (this._readableState === undefined) {
  1016. return false;
  1017. }
  1018. return this._readableState.destroyed;
  1019. },
  1020. set: function set(value) {
  1021. // we ignore the value if the stream
  1022. // has not been initialized yet
  1023. if (!this._readableState) {
  1024. return;
  1025. } // backward compatibility, the user is explicitly
  1026. // managing destroyed
  1027. this._readableState.destroyed = value;
  1028. }
  1029. });
  1030. Readable.prototype.destroy = destroyImpl.destroy;
  1031. Readable.prototype._undestroy = destroyImpl.undestroy;
  1032. Readable.prototype._destroy = function (err, cb) {
  1033. cb(err);
  1034. }; // Manually shove something into the read() buffer.
  1035. // This returns true if the highWaterMark has not been hit yet,
  1036. // similar to how Writable.write() returns true if you should
  1037. // write() some more.
  1038. Readable.prototype.push = function (chunk, encoding) {
  1039. var state = this._readableState;
  1040. var skipChunkCheck;
  1041. if (!state.objectMode) {
  1042. if (typeof chunk === 'string') {
  1043. encoding = encoding || state.defaultEncoding;
  1044. if (encoding !== state.encoding) {
  1045. chunk = Buffer.from(chunk, encoding);
  1046. encoding = '';
  1047. }
  1048. skipChunkCheck = true;
  1049. }
  1050. } else {
  1051. skipChunkCheck = true;
  1052. }
  1053. return readableAddChunk(this, chunk, encoding, false, skipChunkCheck);
  1054. }; // Unshift should *always* be something directly out of read()
  1055. Readable.prototype.unshift = function (chunk) {
  1056. return readableAddChunk(this, chunk, null, true, false);
  1057. };
  1058. function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
  1059. debug('readableAddChunk', chunk);
  1060. var state = stream._readableState;
  1061. if (chunk === null) {
  1062. state.reading = false;
  1063. onEofChunk(stream, state);
  1064. } else {
  1065. var er;
  1066. if (!skipChunkCheck) er = chunkInvalid(state, chunk);
  1067. if (er) {
  1068. errorOrDestroy(stream, er);
  1069. } else if (state.objectMode || chunk && chunk.length > 0) {
  1070. if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
  1071. chunk = _uint8ArrayToBuffer(chunk);
  1072. }
  1073. if (addToFront) {
  1074. if (state.endEmitted) errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());else addChunk(stream, state, chunk, true);
  1075. } else if (state.ended) {
  1076. errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
  1077. } else if (state.destroyed) {
  1078. return false;
  1079. } else {
  1080. state.reading = false;
  1081. if (state.decoder && !encoding) {
  1082. chunk = state.decoder.write(chunk);
  1083. if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
  1084. } else {
  1085. addChunk(stream, state, chunk, false);
  1086. }
  1087. }
  1088. } else if (!addToFront) {
  1089. state.reading = false;
  1090. maybeReadMore(stream, state);
  1091. }
  1092. } // We can push more data if we are below the highWaterMark.
  1093. // Also, if we have no data yet, we can stand some more bytes.
  1094. // This is to work around cases where hwm=0, such as the repl.
  1095. return !state.ended && (state.length < state.highWaterMark || state.length === 0);
  1096. }
  1097. function addChunk(stream, state, chunk, addToFront) {
  1098. if (state.flowing && state.length === 0 && !state.sync) {
  1099. state.awaitDrain = 0;
  1100. stream.emit('data', chunk);
  1101. } else {
  1102. // update the buffer info.
  1103. state.length += state.objectMode ? 1 : chunk.length;
  1104. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  1105. if (state.needReadable) emitReadable(stream);
  1106. }
  1107. maybeReadMore(stream, state);
  1108. }
  1109. function chunkInvalid(state, chunk) {
  1110. var er;
  1111. if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
  1112. er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
  1113. }
  1114. return er;
  1115. }
  1116. Readable.prototype.isPaused = function () {
  1117. return this._readableState.flowing === false;
  1118. }; // backwards compatibility.
  1119. Readable.prototype.setEncoding = function (enc) {
  1120. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  1121. var decoder = new StringDecoder(enc);
  1122. this._readableState.decoder = decoder; // If setEncoding(null), decoder.encoding equals utf8
  1123. this._readableState.encoding = this._readableState.decoder.encoding; // Iterate over current buffer to convert already stored Buffers:
  1124. var p = this._readableState.buffer.head;
  1125. var content = '';
  1126. while (p !== null) {
  1127. content += decoder.write(p.data);
  1128. p = p.next;
  1129. }
  1130. this._readableState.buffer.clear();
  1131. if (content !== '') this._readableState.buffer.push(content);
  1132. this._readableState.length = content.length;
  1133. return this;
  1134. }; // Don't raise the hwm > 1GB
  1135. var MAX_HWM = 0x40000000;
  1136. function computeNewHighWaterMark(n) {
  1137. if (n >= MAX_HWM) {
  1138. // TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
  1139. n = MAX_HWM;
  1140. } else {
  1141. // Get the next highest power of 2 to prevent increasing hwm excessively in
  1142. // tiny amounts
  1143. n--;
  1144. n |= n >>> 1;
  1145. n |= n >>> 2;
  1146. n |= n >>> 4;
  1147. n |= n >>> 8;
  1148. n |= n >>> 16;
  1149. n++;
  1150. }
  1151. return n;
  1152. } // This function is designed to be inlinable, so please take care when making
  1153. // changes to the function body.
  1154. function howMuchToRead(n, state) {
  1155. if (n <= 0 || state.length === 0 && state.ended) return 0;
  1156. if (state.objectMode) return 1;
  1157. if (n !== n) {
  1158. // Only flow one buffer at a time
  1159. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  1160. } // If we're asking for more than the current hwm, then raise the hwm.
  1161. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  1162. if (n <= state.length) return n; // Don't have enough
  1163. if (!state.ended) {
  1164. state.needReadable = true;
  1165. return 0;
  1166. }
  1167. return state.length;
  1168. } // you can override either this method, or the async _read(n) below.
  1169. Readable.prototype.read = function (n) {
  1170. debug('read', n);
  1171. n = parseInt(n, 10);
  1172. var state = this._readableState;
  1173. var nOrig = n;
  1174. if (n !== 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we
  1175. // already have a bunch of data in the buffer, then just trigger
  1176. // the 'readable' event and move on.
  1177. if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) {
  1178. debug('read: emitReadable', state.length, state.ended);
  1179. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  1180. return null;
  1181. }
  1182. n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up.
  1183. if (n === 0 && state.ended) {
  1184. if (state.length === 0) endReadable(this);
  1185. return null;
  1186. } // All the actual chunk generation logic needs to be
  1187. // *below* the call to _read. The reason is that in certain
  1188. // synthetic stream cases, such as passthrough streams, _read
  1189. // may be a completely synchronous operation which may change
  1190. // the state of the read buffer, providing enough data when
  1191. // before there was *not* enough.
  1192. //
  1193. // So, the steps are:
  1194. // 1. Figure out what the state of things will be after we do
  1195. // a read from the buffer.
  1196. //
  1197. // 2. If that resulting state will trigger a _read, then call _read.
  1198. // Note that this may be asynchronous, or synchronous. Yes, it is
  1199. // deeply ugly to write APIs this way, but that still doesn't mean
  1200. // that the Readable class should behave improperly, as streams are
  1201. // designed to be sync/async agnostic.
  1202. // Take note if the _read call is sync or async (ie, if the read call
  1203. // has returned yet), so that we know whether or not it's safe to emit
  1204. // 'readable' etc.
  1205. //
  1206. // 3. Actually pull the requested chunks out of the buffer and return.
  1207. // if we need a readable event, then we need to do some reading.
  1208. var doRead = state.needReadable;
  1209. debug('need readable', doRead); // if we currently have less than the highWaterMark, then also read some
  1210. if (state.length === 0 || state.length - n < state.highWaterMark) {
  1211. doRead = true;
  1212. debug('length less than watermark', doRead);
  1213. } // however, if we've ended, then there's no point, and if we're already
  1214. // reading, then it's unnecessary.
  1215. if (state.ended || state.reading) {
  1216. doRead = false;
  1217. debug('reading or ended', doRead);
  1218. } else if (doRead) {
  1219. debug('do read');
  1220. state.reading = true;
  1221. state.sync = true; // if the length is currently zero, then we *need* a readable event.
  1222. if (state.length === 0) state.needReadable = true; // call internal read method
  1223. this._read(state.highWaterMark);
  1224. state.sync = false; // If _read pushed data synchronously, then `reading` will be false,
  1225. // and we need to re-evaluate how much data we can return to the user.
  1226. if (!state.reading) n = howMuchToRead(nOrig, state);
  1227. }
  1228. var ret;
  1229. if (n > 0) ret = fromList(n, state);else ret = null;
  1230. if (ret === null) {
  1231. state.needReadable = state.length <= state.highWaterMark;
  1232. n = 0;
  1233. } else {
  1234. state.length -= n;
  1235. state.awaitDrain = 0;
  1236. }
  1237. if (state.length === 0) {
  1238. // If we have nothing in the buffer, then we want to know
  1239. // as soon as we *do* get something into the buffer.
  1240. if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick.
  1241. if (nOrig !== n && state.ended) endReadable(this);
  1242. }
  1243. if (ret !== null) this.emit('data', ret);
  1244. return ret;
  1245. };
  1246. function onEofChunk(stream, state) {
  1247. debug('onEofChunk');
  1248. if (state.ended) return;
  1249. if (state.decoder) {
  1250. var chunk = state.decoder.end();
  1251. if (chunk && chunk.length) {
  1252. state.buffer.push(chunk);
  1253. state.length += state.objectMode ? 1 : chunk.length;
  1254. }
  1255. }
  1256. state.ended = true;
  1257. if (state.sync) {
  1258. // if we are sync, wait until next tick to emit the data.
  1259. // Otherwise we risk emitting data in the flow()
  1260. // the readable code triggers during a read() call
  1261. emitReadable(stream);
  1262. } else {
  1263. // emit 'readable' now to make sure it gets picked up.
  1264. state.needReadable = false;
  1265. if (!state.emittedReadable) {
  1266. state.emittedReadable = true;
  1267. emitReadable_(stream);
  1268. }
  1269. }
  1270. } // Don't emit readable right away in sync mode, because this can trigger
  1271. // another read() call => stack overflow. This way, it might trigger
  1272. // a nextTick recursion warning, but that's not so bad.
  1273. function emitReadable(stream) {
  1274. var state = stream._readableState;
  1275. debug('emitReadable', state.needReadable, state.emittedReadable);
  1276. state.needReadable = false;
  1277. if (!state.emittedReadable) {
  1278. debug('emitReadable', state.flowing);
  1279. state.emittedReadable = true;
  1280. process.nextTick(emitReadable_, stream);
  1281. }
  1282. }
  1283. function emitReadable_(stream) {
  1284. var state = stream._readableState;
  1285. debug('emitReadable_', state.destroyed, state.length, state.ended);
  1286. if (!state.destroyed && (state.length || state.ended)) {
  1287. stream.emit('readable');
  1288. state.emittedReadable = false;
  1289. } // The stream needs another readable event if
  1290. // 1. It is not flowing, as the flow mechanism will take
  1291. // care of it.
  1292. // 2. It is not ended.
  1293. // 3. It is below the highWaterMark, so we can schedule
  1294. // another readable later.
  1295. state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark;
  1296. flow(stream);
  1297. } // at this point, the user has presumably seen the 'readable' event,
  1298. // and called read() to consume some data. that may have triggered
  1299. // in turn another _read(n) call, in which case reading = true if
  1300. // it's in progress.
  1301. // However, if we're not ended, or reading, and the length < hwm,
  1302. // then go ahead and try to read some more preemptively.
  1303. function maybeReadMore(stream, state) {
  1304. if (!state.readingMore) {
  1305. state.readingMore = true;
  1306. process.nextTick(maybeReadMore_, stream, state);
  1307. }
  1308. }
  1309. function maybeReadMore_(stream, state) {
  1310. // Attempt to read more data if we should.
  1311. //
  1312. // The conditions for reading more data are (one of):
  1313. // - Not enough data buffered (state.length < state.highWaterMark). The loop
  1314. // is responsible for filling the buffer with enough data if such data
  1315. // is available. If highWaterMark is 0 and we are not in the flowing mode
  1316. // we should _not_ attempt to buffer any extra data. We'll get more data
  1317. // when the stream consumer calls read() instead.
  1318. // - No data in the buffer, and the stream is in flowing mode. In this mode
  1319. // the loop below is responsible for ensuring read() is called. Failing to
  1320. // call read here would abort the flow and there's no other mechanism for
  1321. // continuing the flow if the stream consumer has just subscribed to the
  1322. // 'data' event.
  1323. //
  1324. // In addition to the above conditions to keep reading data, the following
  1325. // conditions prevent the data from being read:
  1326. // - The stream has ended (state.ended).
  1327. // - There is already a pending 'read' operation (state.reading). This is a
  1328. // case where the the stream has called the implementation defined _read()
  1329. // method, but they are processing the call asynchronously and have _not_
  1330. // called push() with new data. In this case we skip performing more
  1331. // read()s. The execution ends in this method again after the _read() ends
  1332. // up calling push() with more data.
  1333. while (!state.reading && !state.ended && (state.length < state.highWaterMark || state.flowing && state.length === 0)) {
  1334. var len = state.length;
  1335. debug('maybeReadMore read 0');
  1336. stream.read(0);
  1337. if (len === state.length) // didn't get any data, stop spinning.
  1338. break;
  1339. }
  1340. state.readingMore = false;
  1341. } // abstract method. to be overridden in specific implementation classes.
  1342. // call cb(er, data) where data is <= n in length.
  1343. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  1344. // arbitrary, and perhaps not very meaningful.
  1345. Readable.prototype._read = function (n) {
  1346. errorOrDestroy(this, new ERR_METHOD_NOT_IMPLEMENTED('_read()'));
  1347. };
  1348. Readable.prototype.pipe = function (dest, pipeOpts) {
  1349. var src = this;
  1350. var state = this._readableState;
  1351. switch (state.pipesCount) {
  1352. case 0:
  1353. state.pipes = dest;
  1354. break;
  1355. case 1:
  1356. state.pipes = [state.pipes, dest];
  1357. break;
  1358. default:
  1359. state.pipes.push(dest);
  1360. break;
  1361. }
  1362. state.pipesCount += 1;
  1363. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  1364. var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
  1365. var endFn = doEnd ? onend : unpipe;
  1366. if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn);
  1367. dest.on('unpipe', onunpipe);
  1368. function onunpipe(readable, unpipeInfo) {
  1369. debug('onunpipe');
  1370. if (readable === src) {
  1371. if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
  1372. unpipeInfo.hasUnpiped = true;
  1373. cleanup();
  1374. }
  1375. }
  1376. }
  1377. function onend() {
  1378. debug('onend');
  1379. dest.end();
  1380. } // when the dest drains, it reduces the awaitDrain counter
  1381. // on the source. This would be more elegant with a .once()
  1382. // handler in flow(), but adding and removing repeatedly is
  1383. // too slow.
  1384. var ondrain = pipeOnDrain(src);
  1385. dest.on('drain', ondrain);
  1386. var cleanedUp = false;
  1387. function cleanup() {
  1388. debug('cleanup'); // cleanup event handlers once the pipe is broken
  1389. dest.removeListener('close', onclose);
  1390. dest.removeListener('finish', onfinish);
  1391. dest.removeListener('drain', ondrain);
  1392. dest.removeListener('error', onerror);
  1393. dest.removeListener('unpipe', onunpipe);
  1394. src.removeListener('end', onend);
  1395. src.removeListener('end', unpipe);
  1396. src.removeListener('data', ondata);
  1397. cleanedUp = true; // if the reader is waiting for a drain event from this
  1398. // specific writer, then it would cause it to never start
  1399. // flowing again.
  1400. // So, if this is awaiting a drain, then we just call it now.
  1401. // If we don't know, then assume that we are waiting for one.
  1402. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  1403. }
  1404. src.on('data', ondata);
  1405. function ondata(chunk) {
  1406. debug('ondata');
  1407. var ret = dest.write(chunk);
  1408. debug('dest.write', ret);
  1409. if (ret === false) {
  1410. // If the user unpiped during `dest.write()`, it is possible
  1411. // to get stuck in a permanently paused state if that write
  1412. // also returned false.
  1413. // => Check whether `dest` is still a piping destination.
  1414. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  1415. debug('false write response, pause', state.awaitDrain);
  1416. state.awaitDrain++;
  1417. }
  1418. src.pause();
  1419. }
  1420. } // if the dest has an error, then stop piping into it.
  1421. // however, don't suppress the throwing behavior for this.
  1422. function onerror(er) {
  1423. debug('onerror', er);
  1424. unpipe();
  1425. dest.removeListener('error', onerror);
  1426. if (EElistenerCount(dest, 'error') === 0) errorOrDestroy(dest, er);
  1427. } // Make sure our error handler is attached before userland ones.
  1428. prependListener(dest, 'error', onerror); // Both close and finish should trigger unpipe, but only once.
  1429. function onclose() {
  1430. dest.removeListener('finish', onfinish);
  1431. unpipe();
  1432. }
  1433. dest.once('close', onclose);
  1434. function onfinish() {
  1435. debug('onfinish');
  1436. dest.removeListener('close', onclose);
  1437. unpipe();
  1438. }
  1439. dest.once('finish', onfinish);
  1440. function unpipe() {
  1441. debug('unpipe');
  1442. src.unpipe(dest);
  1443. } // tell the dest that it's being piped to
  1444. dest.emit('pipe', src); // start the flow if it hasn't been started already.
  1445. if (!state.flowing) {
  1446. debug('pipe resume');
  1447. src.resume();
  1448. }
  1449. return dest;
  1450. };
  1451. function pipeOnDrain(src) {
  1452. return function pipeOnDrainFunctionResult() {
  1453. var state = src._readableState;
  1454. debug('pipeOnDrain', state.awaitDrain);
  1455. if (state.awaitDrain) state.awaitDrain--;
  1456. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  1457. state.flowing = true;
  1458. flow(src);
  1459. }
  1460. };
  1461. }
  1462. Readable.prototype.unpipe = function (dest) {
  1463. var state = this._readableState;
  1464. var unpipeInfo = {
  1465. hasUnpiped: false
  1466. }; // if we're not piping anywhere, then do nothing.
  1467. if (state.pipesCount === 0) return this; // just one destination. most common case.
  1468. if (state.pipesCount === 1) {
  1469. // passed in one, but it's not the right one.
  1470. if (dest && dest !== state.pipes) return this;
  1471. if (!dest) dest = state.pipes; // got a match.
  1472. state.pipes = null;
  1473. state.pipesCount = 0;
  1474. state.flowing = false;
  1475. if (dest) dest.emit('unpipe', this, unpipeInfo);
  1476. return this;
  1477. } // slow case. multiple pipe destinations.
  1478. if (!dest) {
  1479. // remove all.
  1480. var dests = state.pipes;
  1481. var len = state.pipesCount;
  1482. state.pipes = null;
  1483. state.pipesCount = 0;
  1484. state.flowing = false;
  1485. for (var i = 0; i < len; i++) {
  1486. dests[i].emit('unpipe', this, {
  1487. hasUnpiped: false
  1488. });
  1489. }
  1490. return this;
  1491. } // try to find the right one.
  1492. var index = indexOf(state.pipes, dest);
  1493. if (index === -1) return this;
  1494. state.pipes.splice(index, 1);
  1495. state.pipesCount -= 1;
  1496. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  1497. dest.emit('unpipe', this, unpipeInfo);
  1498. return this;
  1499. }; // set up data events if they are asked for
  1500. // Ensure readable listeners eventually get something
  1501. Readable.prototype.on = function (ev, fn) {
  1502. var res = Stream.prototype.on.call(this, ev, fn);
  1503. var state = this._readableState;
  1504. if (ev === 'data') {
  1505. // update readableListening so that resume() may be a no-op
  1506. // a few lines down. This is needed to support once('readable').
  1507. state.readableListening = this.listenerCount('readable') > 0; // Try start flowing on next tick if stream isn't explicitly paused
  1508. if (state.flowing !== false) this.resume();
  1509. } else if (ev === 'readable') {
  1510. if (!state.endEmitted && !state.readableListening) {
  1511. state.readableListening = state.needReadable = true;
  1512. state.flowing = false;
  1513. state.emittedReadable = false;
  1514. debug('on readable', state.length, state.reading);
  1515. if (state.length) {
  1516. emitReadable(this);
  1517. } else if (!state.reading) {
  1518. process.nextTick(nReadingNextTick, this);
  1519. }
  1520. }
  1521. }
  1522. return res;
  1523. };
  1524. Readable.prototype.addListener = Readable.prototype.on;
  1525. Readable.prototype.removeListener = function (ev, fn) {
  1526. var res = Stream.prototype.removeListener.call(this, ev, fn);
  1527. if (ev === 'readable') {
  1528. // We need to check if there is someone still listening to
  1529. // readable and reset the state. However this needs to happen
  1530. // after readable has been emitted but before I/O (nextTick) to
  1531. // support once('readable', fn) cycles. This means that calling
  1532. // resume within the same tick will have no
  1533. // effect.
  1534. process.nextTick(updateReadableListening, this);
  1535. }
  1536. return res;
  1537. };
  1538. Readable.prototype.removeAllListeners = function (ev) {
  1539. var res = Stream.prototype.removeAllListeners.apply(this, arguments);
  1540. if (ev === 'readable' || ev === undefined) {
  1541. // We need to check if there is someone still listening to
  1542. // readable and reset the state. However this needs to happen
  1543. // after readable has been emitted but before I/O (nextTick) to
  1544. // support once('readable', fn) cycles. This means that calling
  1545. // resume within the same tick will have no
  1546. // effect.
  1547. process.nextTick(updateReadableListening, this);
  1548. }
  1549. return res;
  1550. };
  1551. function updateReadableListening(self) {
  1552. var state = self._readableState;
  1553. state.readableListening = self.listenerCount('readable') > 0;
  1554. if (state.resumeScheduled && !state.paused) {
  1555. // flowing needs to be set to true now, otherwise
  1556. // the upcoming resume will not flow.
  1557. state.flowing = true; // crude way to check if we should resume
  1558. } else if (self.listenerCount('data') > 0) {
  1559. self.resume();
  1560. }
  1561. }
  1562. function nReadingNextTick(self) {
  1563. debug('readable nexttick read 0');
  1564. self.read(0);
  1565. } // pause() and resume() are remnants of the legacy readable stream API
  1566. // If the user uses them, then switch into old mode.
  1567. Readable.prototype.resume = function () {
  1568. var state = this._readableState;
  1569. if (!state.flowing) {
  1570. debug('resume'); // we flow only if there is no one listening
  1571. // for readable, but we still have to call
  1572. // resume()
  1573. state.flowing = !state.readableListening;
  1574. resume(this, state);
  1575. }
  1576. state.paused = false;
  1577. return this;
  1578. };
  1579. function resume(stream, state) {
  1580. if (!state.resumeScheduled) {
  1581. state.resumeScheduled = true;
  1582. process.nextTick(resume_, stream, state);
  1583. }
  1584. }
  1585. function resume_(stream, state) {
  1586. debug('resume', state.reading);
  1587. if (!state.reading) {
  1588. stream.read(0);
  1589. }
  1590. state.resumeScheduled = false;
  1591. stream.emit('resume');
  1592. flow(stream);
  1593. if (state.flowing && !state.reading) stream.read(0);
  1594. }
  1595. Readable.prototype.pause = function () {
  1596. debug('call pause flowing=%j', this._readableState.flowing);
  1597. if (this._readableState.flowing !== false) {
  1598. debug('pause');
  1599. this._readableState.flowing = false;
  1600. this.emit('pause');
  1601. }
  1602. this._readableState.paused = true;
  1603. return this;
  1604. };
  1605. function flow(stream) {
  1606. var state = stream._readableState;
  1607. debug('flow', state.flowing);
  1608. while (state.flowing && stream.read() !== null) {
  1609. ;
  1610. }
  1611. } // wrap an old-style stream as the async data source.
  1612. // This is *not* part of the readable stream interface.
  1613. // It is an ugly unfortunate mess of history.
  1614. Readable.prototype.wrap = function (stream) {
  1615. var _this = this;
  1616. var state = this._readableState;
  1617. var paused = false;
  1618. stream.on('end', function () {
  1619. debug('wrapped end');
  1620. if (state.decoder && !state.ended) {
  1621. var chunk = state.decoder.end();
  1622. if (chunk && chunk.length) _this.push(chunk);
  1623. }
  1624. _this.push(null);
  1625. });
  1626. stream.on('data', function (chunk) {
  1627. debug('wrapped data');
  1628. if (state.decoder) chunk = state.decoder.write(chunk); // don't skip over falsy values in objectMode
  1629. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  1630. var ret = _this.push(chunk);
  1631. if (!ret) {
  1632. paused = true;
  1633. stream.pause();
  1634. }
  1635. }); // proxy all the other methods.
  1636. // important when wrapping filters and duplexes.
  1637. for (var i in stream) {
  1638. if (this[i] === undefined && typeof stream[i] === 'function') {
  1639. this[i] = function methodWrap(method) {
  1640. return function methodWrapReturnFunction() {
  1641. return stream[method].apply(stream, arguments);
  1642. };
  1643. }(i);
  1644. }
  1645. } // proxy certain important events.
  1646. for (var n = 0; n < kProxyEvents.length; n++) {
  1647. stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
  1648. } // when we try to consume some more bytes, simply unpause the
  1649. // underlying stream.
  1650. this._read = function (n) {
  1651. debug('wrapped _read', n);
  1652. if (paused) {
  1653. paused = false;
  1654. stream.resume();
  1655. }
  1656. };
  1657. return this;
  1658. };
  1659. if (typeof Symbol === 'function') {
  1660. Readable.prototype[Symbol.asyncIterator] = function () {
  1661. if (createReadableStreamAsyncIterator === undefined) {
  1662. createReadableStreamAsyncIterator = require('./internal/streams/async_iterator');
  1663. }
  1664. return createReadableStreamAsyncIterator(this);
  1665. };
  1666. }
  1667. Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
  1668. // making it explicit this property is not enumerable
  1669. // because otherwise some prototype manipulation in
  1670. // userland will fail
  1671. enumerable: false,
  1672. get: function get() {
  1673. return this._readableState.highWaterMark;
  1674. }
  1675. });
  1676. Object.defineProperty(Readable.prototype, 'readableBuffer', {
  1677. // making it explicit this property is not enumerable
  1678. // because otherwise some prototype manipulation in
  1679. // userland will fail
  1680. enumerable: false,
  1681. get: function get() {
  1682. return this._readableState && this._readableState.buffer;
  1683. }
  1684. });
  1685. Object.defineProperty(Readable.prototype, 'readableFlowing', {
  1686. // making it explicit this property is not enumerable
  1687. // because otherwise some prototype manipulation in
  1688. // userland will fail
  1689. enumerable: false,
  1690. get: function get() {
  1691. return this._readableState.flowing;
  1692. },
  1693. set: function set(state) {
  1694. if (this._readableState) {
  1695. this._readableState.flowing = state;
  1696. }
  1697. }
  1698. }); // exposed for testing purposes only.
  1699. Readable._fromList = fromList;
  1700. Object.defineProperty(Readable.prototype, 'readableLength', {
  1701. // making it explicit this property is not enumerable
  1702. // because otherwise some prototype manipulation in
  1703. // userland will fail
  1704. enumerable: false,
  1705. get: function get() {
  1706. return this._readableState.length;
  1707. }
  1708. }); // Pluck off n bytes from an array of buffers.
  1709. // Length is the combined lengths of all the buffers in the list.
  1710. // This function is designed to be inlinable, so please take care when making
  1711. // changes to the function body.
  1712. function fromList(n, state) {
  1713. // nothing buffered
  1714. if (state.length === 0) return null;
  1715. var ret;
  1716. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  1717. // read it all, truncate the list
  1718. if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.first();else ret = state.buffer.concat(state.length);
  1719. state.buffer.clear();
  1720. } else {
  1721. // read part of list
  1722. ret = state.buffer.consume(n, state.decoder);
  1723. }
  1724. return ret;
  1725. }
  1726. function endReadable(stream) {
  1727. var state = stream._readableState;
  1728. debug('endReadable', state.endEmitted);
  1729. if (!state.endEmitted) {
  1730. state.ended = true;
  1731. process.nextTick(endReadableNT, state, stream);
  1732. }
  1733. }
  1734. function endReadableNT(state, stream) {
  1735. debug('endReadableNT', state.endEmitted, state.length); // Check that we didn't get one last unshift.
  1736. if (!state.endEmitted && state.length === 0) {
  1737. state.endEmitted = true;
  1738. stream.readable = false;
  1739. stream.emit('end');
  1740. if (state.autoDestroy) {
  1741. // In case of duplex streams we need a way to detect
  1742. // if the writable side is ready for autoDestroy as well
  1743. var wState = stream._writableState;
  1744. if (!wState || wState.autoDestroy && wState.finished) {
  1745. stream.destroy();
  1746. }
  1747. }
  1748. }
  1749. }
  1750. if (typeof Symbol === 'function') {
  1751. Readable.from = function (iterable, opts) {
  1752. if (from === undefined) {
  1753. from = require('./internal/streams/from');
  1754. }
  1755. return from(Readable, iterable, opts);
  1756. };
  1757. }
  1758. function indexOf(xs, x) {
  1759. for (var i = 0, l = xs.length; i < l; i++) {
  1760. if (xs[i] === x) return i;
  1761. }
  1762. return -1;
  1763. }
  1764. }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1765. },{"../errors":12,"./_stream_duplex":13,"./internal/streams/async_iterator":18,"./internal/streams/buffer_list":19,"./internal/streams/destroy":20,"./internal/streams/from":22,"./internal/streams/state":24,"./internal/streams/stream":25,"_process":47,"buffer":43,"events":44,"inherits":10,"string_decoder/":37,"util":42}],16:[function(require,module,exports){
  1766. // Copyright Joyent, Inc. and other Node contributors.
  1767. //
  1768. // Permission is hereby granted, free of charge, to any person obtaining a
  1769. // copy of this software and associated documentation files (the
  1770. // "Software"), to deal in the Software without restriction, including
  1771. // without limitation the rights to use, copy, modify, merge, publish,
  1772. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1773. // persons to whom the Software is furnished to do so, subject to the
  1774. // following conditions:
  1775. //
  1776. // The above copyright notice and this permission notice shall be included
  1777. // in all copies or substantial portions of the Software.
  1778. //
  1779. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1780. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1781. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1782. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1783. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1784. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1785. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1786. // a transform stream is a readable/writable stream where you do
  1787. // something with the data. Sometimes it's called a "filter",
  1788. // but that's not a great name for it, since that implies a thing where
  1789. // some bits pass through, and others are simply ignored. (That would
  1790. // be a valid example of a transform, of course.)
  1791. //
  1792. // While the output is causally related to the input, it's not a
  1793. // necessarily symmetric or synchronous transformation. For example,
  1794. // a zlib stream might take multiple plain-text writes(), and then
  1795. // emit a single compressed chunk some time in the future.
  1796. //
  1797. // Here's how this works:
  1798. //
  1799. // The Transform stream has all the aspects of the readable and writable
  1800. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  1801. // internally, and returns false if there's a lot of pending writes
  1802. // buffered up. When you call read(), that calls _read(n) until
  1803. // there's enough pending readable data buffered up.
  1804. //
  1805. // In a transform stream, the written data is placed in a buffer. When
  1806. // _read(n) is called, it transforms the queued up data, calling the
  1807. // buffered _write cb's as it consumes chunks. If consuming a single
  1808. // written chunk would result in multiple output chunks, then the first
  1809. // outputted bit calls the readcb, and subsequent chunks just go into
  1810. // the read buffer, and will cause it to emit 'readable' if necessary.
  1811. //
  1812. // This way, back-pressure is actually determined by the reading side,
  1813. // since _read has to be called to start processing a new chunk. However,
  1814. // a pathological inflate type of transform can cause excessive buffering
  1815. // here. For example, imagine a stream where every byte of input is
  1816. // interpreted as an integer from 0-255, and then results in that many
  1817. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  1818. // 1kb of data being output. In this case, you could write a very small
  1819. // amount of input, and end up with a very large amount of output. In
  1820. // such a pathological inflating mechanism, there'd be no way to tell
  1821. // the system to stop doing the transform. A single 4MB write could
  1822. // cause the system to run out of memory.
  1823. //
  1824. // However, even in such a pathological case, only a single written chunk
  1825. // would be consumed, and then the rest would wait (un-transformed) until
  1826. // the results of the previous transformed chunk were consumed.
  1827. 'use strict';
  1828. module.exports = Transform;
  1829. var _require$codes = require('../errors').codes,
  1830. ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
  1831. ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK,
  1832. ERR_TRANSFORM_ALREADY_TRANSFORMING = _require$codes.ERR_TRANSFORM_ALREADY_TRANSFORMING,
  1833. ERR_TRANSFORM_WITH_LENGTH_0 = _require$codes.ERR_TRANSFORM_WITH_LENGTH_0;
  1834. var Duplex = require('./_stream_duplex');
  1835. require('inherits')(Transform, Duplex);
  1836. function afterTransform(er, data) {
  1837. var ts = this._transformState;
  1838. ts.transforming = false;
  1839. var cb = ts.writecb;
  1840. if (cb === null) {
  1841. return this.emit('error', new ERR_MULTIPLE_CALLBACK());
  1842. }
  1843. ts.writechunk = null;
  1844. ts.writecb = null;
  1845. if (data != null) // single equals check for both `null` and `undefined`
  1846. this.push(data);
  1847. cb(er);
  1848. var rs = this._readableState;
  1849. rs.reading = false;
  1850. if (rs.needReadable || rs.length < rs.highWaterMark) {
  1851. this._read(rs.highWaterMark);
  1852. }
  1853. }
  1854. function Transform(options) {
  1855. if (!(this instanceof Transform)) return new Transform(options);
  1856. Duplex.call(this, options);
  1857. this._transformState = {
  1858. afterTransform: afterTransform.bind(this),
  1859. needTransform: false,
  1860. transforming: false,
  1861. writecb: null,
  1862. writechunk: null,
  1863. writeencoding: null
  1864. }; // start out asking for a readable event once data is transformed.
  1865. this._readableState.needReadable = true; // we have implemented the _read method, and done the other things
  1866. // that Readable wants before the first _read call, so unset the
  1867. // sync guard flag.
  1868. this._readableState.sync = false;
  1869. if (options) {
  1870. if (typeof options.transform === 'function') this._transform = options.transform;
  1871. if (typeof options.flush === 'function') this._flush = options.flush;
  1872. } // When the writable side finishes, then flush out anything remaining.
  1873. this.on('prefinish', prefinish);
  1874. }
  1875. function prefinish() {
  1876. var _this = this;
  1877. if (typeof this._flush === 'function' && !this._readableState.destroyed) {
  1878. this._flush(function (er, data) {
  1879. done(_this, er, data);
  1880. });
  1881. } else {
  1882. done(this, null, null);
  1883. }
  1884. }
  1885. Transform.prototype.push = function (chunk, encoding) {
  1886. this._transformState.needTransform = false;
  1887. return Duplex.prototype.push.call(this, chunk, encoding);
  1888. }; // This is the part where you do stuff!
  1889. // override this function in implementation classes.
  1890. // 'chunk' is an input chunk.
  1891. //
  1892. // Call `push(newChunk)` to pass along transformed output
  1893. // to the readable side. You may call 'push' zero or more times.
  1894. //
  1895. // Call `cb(err)` when you are done with this chunk. If you pass
  1896. // an error, then that'll put the hurt on the whole operation. If you
  1897. // never call cb(), then you'll never get another chunk.
  1898. Transform.prototype._transform = function (chunk, encoding, cb) {
  1899. cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
  1900. };
  1901. Transform.prototype._write = function (chunk, encoding, cb) {
  1902. var ts = this._transformState;
  1903. ts.writecb = cb;
  1904. ts.writechunk = chunk;
  1905. ts.writeencoding = encoding;
  1906. if (!ts.transforming) {
  1907. var rs = this._readableState;
  1908. if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
  1909. }
  1910. }; // Doesn't matter what the args are here.
  1911. // _transform does all the work.
  1912. // That we got here means that the readable side wants more data.
  1913. Transform.prototype._read = function (n) {
  1914. var ts = this._transformState;
  1915. if (ts.writechunk !== null && !ts.transforming) {
  1916. ts.transforming = true;
  1917. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  1918. } else {
  1919. // mark that we need a transform, so that any data that comes in
  1920. // will get processed, now that we've asked for it.
  1921. ts.needTransform = true;
  1922. }
  1923. };
  1924. Transform.prototype._destroy = function (err, cb) {
  1925. Duplex.prototype._destroy.call(this, err, function (err2) {
  1926. cb(err2);
  1927. });
  1928. };
  1929. function done(stream, er, data) {
  1930. if (er) return stream.emit('error', er);
  1931. if (data != null) // single equals check for both `null` and `undefined`
  1932. stream.push(data); // TODO(BridgeAR): Write a test for these two error cases
  1933. // if there's nothing in the write buffer, then that means
  1934. // that nothing more will ever be provided
  1935. if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0();
  1936. if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
  1937. return stream.push(null);
  1938. }
  1939. },{"../errors":12,"./_stream_duplex":13,"inherits":10}],17:[function(require,module,exports){
  1940. (function (process,global){(function (){
  1941. // Copyright Joyent, Inc. and other Node contributors.
  1942. //
  1943. // Permission is hereby granted, free of charge, to any person obtaining a
  1944. // copy of this software and associated documentation files (the
  1945. // "Software"), to deal in the Software without restriction, including
  1946. // without limitation the rights to use, copy, modify, merge, publish,
  1947. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1948. // persons to whom the Software is furnished to do so, subject to the
  1949. // following conditions:
  1950. //
  1951. // The above copyright notice and this permission notice shall be included
  1952. // in all copies or substantial portions of the Software.
  1953. //
  1954. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1955. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1956. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1957. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1958. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1959. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1960. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1961. // A bit simpler than readable streams.
  1962. // Implement an async ._write(chunk, encoding, cb), and it'll handle all
  1963. // the drain event emission and buffering.
  1964. 'use strict';
  1965. module.exports = Writable;
  1966. /* <replacement> */
  1967. function WriteReq(chunk, encoding, cb) {
  1968. this.chunk = chunk;
  1969. this.encoding = encoding;
  1970. this.callback = cb;
  1971. this.next = null;
  1972. } // It seems a linked list but it is not
  1973. // there will be only 2 of these for each stream
  1974. function CorkedRequest(state) {
  1975. var _this = this;
  1976. this.next = null;
  1977. this.entry = null;
  1978. this.finish = function () {
  1979. onCorkedFinish(_this, state);
  1980. };
  1981. }
  1982. /* </replacement> */
  1983. /*<replacement>*/
  1984. var Duplex;
  1985. /*</replacement>*/
  1986. Writable.WritableState = WritableState;
  1987. /*<replacement>*/
  1988. var internalUtil = {
  1989. deprecate: require('util-deprecate')
  1990. };
  1991. /*</replacement>*/
  1992. /*<replacement>*/
  1993. var Stream = require('./internal/streams/stream');
  1994. /*</replacement>*/
  1995. var Buffer = require('buffer').Buffer;
  1996. var OurUint8Array = global.Uint8Array || function () {};
  1997. function _uint8ArrayToBuffer(chunk) {
  1998. return Buffer.from(chunk);
  1999. }
  2000. function _isUint8Array(obj) {
  2001. return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
  2002. }
  2003. var destroyImpl = require('./internal/streams/destroy');
  2004. var _require = require('./internal/streams/state'),
  2005. getHighWaterMark = _require.getHighWaterMark;
  2006. var _require$codes = require('../errors').codes,
  2007. ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE,
  2008. ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
  2009. ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK,
  2010. ERR_STREAM_CANNOT_PIPE = _require$codes.ERR_STREAM_CANNOT_PIPE,
  2011. ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED,
  2012. ERR_STREAM_NULL_VALUES = _require$codes.ERR_STREAM_NULL_VALUES,
  2013. ERR_STREAM_WRITE_AFTER_END = _require$codes.ERR_STREAM_WRITE_AFTER_END,
  2014. ERR_UNKNOWN_ENCODING = _require$codes.ERR_UNKNOWN_ENCODING;
  2015. var errorOrDestroy = destroyImpl.errorOrDestroy;
  2016. require('inherits')(Writable, Stream);
  2017. function nop() {}
  2018. function WritableState(options, stream, isDuplex) {
  2019. Duplex = Duplex || require('./_stream_duplex');
  2020. options = options || {}; // Duplex streams are both readable and writable, but share
  2021. // the same options object.
  2022. // However, some cases require setting options to different
  2023. // values for the readable and the writable sides of the duplex stream,
  2024. // e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
  2025. if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag to indicate whether or not this stream
  2026. // contains buffers or objects.
  2027. this.objectMode = !!options.objectMode;
  2028. if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // the point at which write() starts returning false
  2029. // Note: 0 is a valid value, means that we always return false if
  2030. // the entire buffer is not flushed immediately on write()
  2031. this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex); // if _final has been called
  2032. this.finalCalled = false; // drain event flag.
  2033. this.needDrain = false; // at the start of calling end()
  2034. this.ending = false; // when end() has been called, and returned
  2035. this.ended = false; // when 'finish' is emitted
  2036. this.finished = false; // has it been destroyed
  2037. this.destroyed = false; // should we decode strings into buffers before passing to _write?
  2038. // this is here so that some node-core streams can optimize string
  2039. // handling at a lower level.
  2040. var noDecode = options.decodeStrings === false;
  2041. this.decodeStrings = !noDecode; // Crypto is kind of old and crusty. Historically, its default string
  2042. // encoding is 'binary' so we have to make this configurable.
  2043. // Everything else in the universe uses 'utf8', though.
  2044. this.defaultEncoding = options.defaultEncoding || 'utf8'; // not an actual buffer we keep track of, but a measurement
  2045. // of how much we're waiting to get pushed to some underlying
  2046. // socket or file.
  2047. this.length = 0; // a flag to see when we're in the middle of a write.
  2048. this.writing = false; // when true all writes will be buffered until .uncork() call
  2049. this.corked = 0; // a flag to be able to tell if the onwrite cb is called immediately,
  2050. // or on a later tick. We set this to true at first, because any
  2051. // actions that shouldn't happen until "later" should generally also
  2052. // not happen before the first write call.
  2053. this.sync = true; // a flag to know if we're processing previously buffered items, which
  2054. // may call the _write() callback in the same tick, so that we don't
  2055. // end up in an overlapped onwrite situation.
  2056. this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb)
  2057. this.onwrite = function (er) {
  2058. onwrite(stream, er);
  2059. }; // the callback that the user supplies to write(chunk,encoding,cb)
  2060. this.writecb = null; // the amount that is being written when _write is called.
  2061. this.writelen = 0;
  2062. this.bufferedRequest = null;
  2063. this.lastBufferedRequest = null; // number of pending user-supplied write callbacks
  2064. // this must be 0 before 'finish' can be emitted
  2065. this.pendingcb = 0; // emit prefinish if the only thing we're waiting for is _write cbs
  2066. // This is relevant for synchronous Transform streams
  2067. this.prefinished = false; // True if the error was already emitted and should not be thrown again
  2068. this.errorEmitted = false; // Should close be emitted on destroy. Defaults to true.
  2069. this.emitClose = options.emitClose !== false; // Should .destroy() be called after 'finish' (and potentially 'end')
  2070. this.autoDestroy = !!options.autoDestroy; // count buffered requests
  2071. this.bufferedRequestCount = 0; // allocate the first CorkedRequest, there is always
  2072. // one allocated and free to use, and we maintain at most two
  2073. this.corkedRequestsFree = new CorkedRequest(this);
  2074. }
  2075. WritableState.prototype.getBuffer = function getBuffer() {
  2076. var current = this.bufferedRequest;
  2077. var out = [];
  2078. while (current) {
  2079. out.push(current);
  2080. current = current.next;
  2081. }
  2082. return out;
  2083. };
  2084. (function () {
  2085. try {
  2086. Object.defineProperty(WritableState.prototype, 'buffer', {
  2087. get: internalUtil.deprecate(function writableStateBufferGetter() {
  2088. return this.getBuffer();
  2089. }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003')
  2090. });
  2091. } catch (_) {}
  2092. })(); // Test _writableState for inheritance to account for Duplex streams,
  2093. // whose prototype chain only points to Readable.
  2094. var realHasInstance;
  2095. if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
  2096. realHasInstance = Function.prototype[Symbol.hasInstance];
  2097. Object.defineProperty(Writable, Symbol.hasInstance, {
  2098. value: function value(object) {
  2099. if (realHasInstance.call(this, object)) return true;
  2100. if (this !== Writable) return false;
  2101. return object && object._writableState instanceof WritableState;
  2102. }
  2103. });
  2104. } else {
  2105. realHasInstance = function realHasInstance(object) {
  2106. return object instanceof this;
  2107. };
  2108. }
  2109. function Writable(options) {
  2110. Duplex = Duplex || require('./_stream_duplex'); // Writable ctor is applied to Duplexes, too.
  2111. // `realHasInstance` is necessary because using plain `instanceof`
  2112. // would return false, as no `_writableState` property is attached.
  2113. // Trying to use the custom `instanceof` for Writable here will also break the
  2114. // Node.js LazyTransform implementation, which has a non-trivial getter for
  2115. // `_writableState` that would lead to infinite recursion.
  2116. // Checking for a Stream.Duplex instance is faster here instead of inside
  2117. // the WritableState constructor, at least with V8 6.5
  2118. var isDuplex = this instanceof Duplex;
  2119. if (!isDuplex && !realHasInstance.call(Writable, this)) return new Writable(options);
  2120. this._writableState = new WritableState(options, this, isDuplex); // legacy.
  2121. this.writable = true;
  2122. if (options) {
  2123. if (typeof options.write === 'function') this._write = options.write;
  2124. if (typeof options.writev === 'function') this._writev = options.writev;
  2125. if (typeof options.destroy === 'function') this._destroy = options.destroy;
  2126. if (typeof options.final === 'function') this._final = options.final;
  2127. }
  2128. Stream.call(this);
  2129. } // Otherwise people can pipe Writable streams, which is just wrong.
  2130. Writable.prototype.pipe = function () {
  2131. errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
  2132. };
  2133. function writeAfterEnd(stream, cb) {
  2134. var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb
  2135. errorOrDestroy(stream, er);
  2136. process.nextTick(cb, er);
  2137. } // Checks that a user-supplied chunk is valid, especially for the particular
  2138. // mode the stream is in. Currently this means that `null` is never accepted
  2139. // and undefined/non-string values are only allowed in object mode.
  2140. function validChunk(stream, state, chunk, cb) {
  2141. var er;
  2142. if (chunk === null) {
  2143. er = new ERR_STREAM_NULL_VALUES();
  2144. } else if (typeof chunk !== 'string' && !state.objectMode) {
  2145. er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
  2146. }
  2147. if (er) {
  2148. errorOrDestroy(stream, er);
  2149. process.nextTick(cb, er);
  2150. return false;
  2151. }
  2152. return true;
  2153. }
  2154. Writable.prototype.write = function (chunk, encoding, cb) {
  2155. var state = this._writableState;
  2156. var ret = false;
  2157. var isBuf = !state.objectMode && _isUint8Array(chunk);
  2158. if (isBuf && !Buffer.isBuffer(chunk)) {
  2159. chunk = _uint8ArrayToBuffer(chunk);
  2160. }
  2161. if (typeof encoding === 'function') {
  2162. cb = encoding;
  2163. encoding = null;
  2164. }
  2165. if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
  2166. if (typeof cb !== 'function') cb = nop;
  2167. if (state.ending) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) {
  2168. state.pendingcb++;
  2169. ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
  2170. }
  2171. return ret;
  2172. };
  2173. Writable.prototype.cork = function () {
  2174. this._writableState.corked++;
  2175. };
  2176. Writable.prototype.uncork = function () {
  2177. var state = this._writableState;
  2178. if (state.corked) {
  2179. state.corked--;
  2180. if (!state.writing && !state.corked && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
  2181. }
  2182. };
  2183. Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
  2184. // node::ParseEncoding() requires lower case.
  2185. if (typeof encoding === 'string') encoding = encoding.toLowerCase();
  2186. if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new ERR_UNKNOWN_ENCODING(encoding);
  2187. this._writableState.defaultEncoding = encoding;
  2188. return this;
  2189. };
  2190. Object.defineProperty(Writable.prototype, 'writableBuffer', {
  2191. // making it explicit this property is not enumerable
  2192. // because otherwise some prototype manipulation in
  2193. // userland will fail
  2194. enumerable: false,
  2195. get: function get() {
  2196. return this._writableState && this._writableState.getBuffer();
  2197. }
  2198. });
  2199. function decodeChunk(state, chunk, encoding) {
  2200. if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
  2201. chunk = Buffer.from(chunk, encoding);
  2202. }
  2203. return chunk;
  2204. }
  2205. Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
  2206. // making it explicit this property is not enumerable
  2207. // because otherwise some prototype manipulation in
  2208. // userland will fail
  2209. enumerable: false,
  2210. get: function get() {
  2211. return this._writableState.highWaterMark;
  2212. }
  2213. }); // if we're already writing something, then just put this
  2214. // in the queue, and wait our turn. Otherwise, call _write
  2215. // If we return false, then we need a drain event, so set that flag.
  2216. function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
  2217. if (!isBuf) {
  2218. var newChunk = decodeChunk(state, chunk, encoding);
  2219. if (chunk !== newChunk) {
  2220. isBuf = true;
  2221. encoding = 'buffer';
  2222. chunk = newChunk;
  2223. }
  2224. }
  2225. var len = state.objectMode ? 1 : chunk.length;
  2226. state.length += len;
  2227. var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false.
  2228. if (!ret) state.needDrain = true;
  2229. if (state.writing || state.corked) {
  2230. var last = state.lastBufferedRequest;
  2231. state.lastBufferedRequest = {
  2232. chunk: chunk,
  2233. encoding: encoding,
  2234. isBuf: isBuf,
  2235. callback: cb,
  2236. next: null
  2237. };
  2238. if (last) {
  2239. last.next = state.lastBufferedRequest;
  2240. } else {
  2241. state.bufferedRequest = state.lastBufferedRequest;
  2242. }
  2243. state.bufferedRequestCount += 1;
  2244. } else {
  2245. doWrite(stream, state, false, len, chunk, encoding, cb);
  2246. }
  2247. return ret;
  2248. }
  2249. function doWrite(stream, state, writev, len, chunk, encoding, cb) {
  2250. state.writelen = len;
  2251. state.writecb = cb;
  2252. state.writing = true;
  2253. state.sync = true;
  2254. if (state.destroyed) state.onwrite(new ERR_STREAM_DESTROYED('write'));else if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
  2255. state.sync = false;
  2256. }
  2257. function onwriteError(stream, state, sync, er, cb) {
  2258. --state.pendingcb;
  2259. if (sync) {
  2260. // defer the callback if we are being called synchronously
  2261. // to avoid piling up things on the stack
  2262. process.nextTick(cb, er); // this can emit finish, and it will always happen
  2263. // after error
  2264. process.nextTick(finishMaybe, stream, state);
  2265. stream._writableState.errorEmitted = true;
  2266. errorOrDestroy(stream, er);
  2267. } else {
  2268. // the caller expect this to happen before if
  2269. // it is async
  2270. cb(er);
  2271. stream._writableState.errorEmitted = true;
  2272. errorOrDestroy(stream, er); // this can emit finish, but finish must
  2273. // always follow error
  2274. finishMaybe(stream, state);
  2275. }
  2276. }
  2277. function onwriteStateUpdate(state) {
  2278. state.writing = false;
  2279. state.writecb = null;
  2280. state.length -= state.writelen;
  2281. state.writelen = 0;
  2282. }
  2283. function onwrite(stream, er) {
  2284. var state = stream._writableState;
  2285. var sync = state.sync;
  2286. var cb = state.writecb;
  2287. if (typeof cb !== 'function') throw new ERR_MULTIPLE_CALLBACK();
  2288. onwriteStateUpdate(state);
  2289. if (er) onwriteError(stream, state, sync, er, cb);else {
  2290. // Check if we're actually ready to finish, but don't emit yet
  2291. var finished = needFinish(state) || stream.destroyed;
  2292. if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
  2293. clearBuffer(stream, state);
  2294. }
  2295. if (sync) {
  2296. process.nextTick(afterWrite, stream, state, finished, cb);
  2297. } else {
  2298. afterWrite(stream, state, finished, cb);
  2299. }
  2300. }
  2301. }
  2302. function afterWrite(stream, state, finished, cb) {
  2303. if (!finished) onwriteDrain(stream, state);
  2304. state.pendingcb--;
  2305. cb();
  2306. finishMaybe(stream, state);
  2307. } // Must force callback to be called on nextTick, so that we don't
  2308. // emit 'drain' before the write() consumer gets the 'false' return
  2309. // value, and has a chance to attach a 'drain' listener.
  2310. function onwriteDrain(stream, state) {
  2311. if (state.length === 0 && state.needDrain) {
  2312. state.needDrain = false;
  2313. stream.emit('drain');
  2314. }
  2315. } // if there's something in the buffer waiting, then process it
  2316. function clearBuffer(stream, state) {
  2317. state.bufferProcessing = true;
  2318. var entry = state.bufferedRequest;
  2319. if (stream._writev && entry && entry.next) {
  2320. // Fast case, write everything using _writev()
  2321. var l = state.bufferedRequestCount;
  2322. var buffer = new Array(l);
  2323. var holder = state.corkedRequestsFree;
  2324. holder.entry = entry;
  2325. var count = 0;
  2326. var allBuffers = true;
  2327. while (entry) {
  2328. buffer[count] = entry;
  2329. if (!entry.isBuf) allBuffers = false;
  2330. entry = entry.next;
  2331. count += 1;
  2332. }
  2333. buffer.allBuffers = allBuffers;
  2334. doWrite(stream, state, true, state.length, buffer, '', holder.finish); // doWrite is almost always async, defer these to save a bit of time
  2335. // as the hot path ends with doWrite
  2336. state.pendingcb++;
  2337. state.lastBufferedRequest = null;
  2338. if (holder.next) {
  2339. state.corkedRequestsFree = holder.next;
  2340. holder.next = null;
  2341. } else {
  2342. state.corkedRequestsFree = new CorkedRequest(state);
  2343. }
  2344. state.bufferedRequestCount = 0;
  2345. } else {
  2346. // Slow case, write chunks one-by-one
  2347. while (entry) {
  2348. var chunk = entry.chunk;
  2349. var encoding = entry.encoding;
  2350. var cb = entry.callback;
  2351. var len = state.objectMode ? 1 : chunk.length;
  2352. doWrite(stream, state, false, len, chunk, encoding, cb);
  2353. entry = entry.next;
  2354. state.bufferedRequestCount--; // if we didn't call the onwrite immediately, then
  2355. // it means that we need to wait until it does.
  2356. // also, that means that the chunk and cb are currently
  2357. // being processed, so move the buffer counter past them.
  2358. if (state.writing) {
  2359. break;
  2360. }
  2361. }
  2362. if (entry === null) state.lastBufferedRequest = null;
  2363. }
  2364. state.bufferedRequest = entry;
  2365. state.bufferProcessing = false;
  2366. }
  2367. Writable.prototype._write = function (chunk, encoding, cb) {
  2368. cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
  2369. };
  2370. Writable.prototype._writev = null;
  2371. Writable.prototype.end = function (chunk, encoding, cb) {
  2372. var state = this._writableState;
  2373. if (typeof chunk === 'function') {
  2374. cb = chunk;
  2375. chunk = null;
  2376. encoding = null;
  2377. } else if (typeof encoding === 'function') {
  2378. cb = encoding;
  2379. encoding = null;
  2380. }
  2381. if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks
  2382. if (state.corked) {
  2383. state.corked = 1;
  2384. this.uncork();
  2385. } // ignore unnecessary end() calls.
  2386. if (!state.ending) endWritable(this, state, cb);
  2387. return this;
  2388. };
  2389. Object.defineProperty(Writable.prototype, 'writableLength', {
  2390. // making it explicit this property is not enumerable
  2391. // because otherwise some prototype manipulation in
  2392. // userland will fail
  2393. enumerable: false,
  2394. get: function get() {
  2395. return this._writableState.length;
  2396. }
  2397. });
  2398. function needFinish(state) {
  2399. return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
  2400. }
  2401. function callFinal(stream, state) {
  2402. stream._final(function (err) {
  2403. state.pendingcb--;
  2404. if (err) {
  2405. errorOrDestroy(stream, err);
  2406. }
  2407. state.prefinished = true;
  2408. stream.emit('prefinish');
  2409. finishMaybe(stream, state);
  2410. });
  2411. }
  2412. function prefinish(stream, state) {
  2413. if (!state.prefinished && !state.finalCalled) {
  2414. if (typeof stream._final === 'function' && !state.destroyed) {
  2415. state.pendingcb++;
  2416. state.finalCalled = true;
  2417. process.nextTick(callFinal, stream, state);
  2418. } else {
  2419. state.prefinished = true;
  2420. stream.emit('prefinish');
  2421. }
  2422. }
  2423. }
  2424. function finishMaybe(stream, state) {
  2425. var need = needFinish(state);
  2426. if (need) {
  2427. prefinish(stream, state);
  2428. if (state.pendingcb === 0) {
  2429. state.finished = true;
  2430. stream.emit('finish');
  2431. if (state.autoDestroy) {
  2432. // In case of duplex streams we need a way to detect
  2433. // if the readable side is ready for autoDestroy as well
  2434. var rState = stream._readableState;
  2435. if (!rState || rState.autoDestroy && rState.endEmitted) {
  2436. stream.destroy();
  2437. }
  2438. }
  2439. }
  2440. }
  2441. return need;
  2442. }
  2443. function endWritable(stream, state, cb) {
  2444. state.ending = true;
  2445. finishMaybe(stream, state);
  2446. if (cb) {
  2447. if (state.finished) process.nextTick(cb);else stream.once('finish', cb);
  2448. }
  2449. state.ended = true;
  2450. stream.writable = false;
  2451. }
  2452. function onCorkedFinish(corkReq, state, err) {
  2453. var entry = corkReq.entry;
  2454. corkReq.entry = null;
  2455. while (entry) {
  2456. var cb = entry.callback;
  2457. state.pendingcb--;
  2458. cb(err);
  2459. entry = entry.next;
  2460. } // reuse the free corkReq.
  2461. state.corkedRequestsFree.next = corkReq;
  2462. }
  2463. Object.defineProperty(Writable.prototype, 'destroyed', {
  2464. // making it explicit this property is not enumerable
  2465. // because otherwise some prototype manipulation in
  2466. // userland will fail
  2467. enumerable: false,
  2468. get: function get() {
  2469. if (this._writableState === undefined) {
  2470. return false;
  2471. }
  2472. return this._writableState.destroyed;
  2473. },
  2474. set: function set(value) {
  2475. // we ignore the value if the stream
  2476. // has not been initialized yet
  2477. if (!this._writableState) {
  2478. return;
  2479. } // backward compatibility, the user is explicitly
  2480. // managing destroyed
  2481. this._writableState.destroyed = value;
  2482. }
  2483. });
  2484. Writable.prototype.destroy = destroyImpl.destroy;
  2485. Writable.prototype._undestroy = destroyImpl.undestroy;
  2486. Writable.prototype._destroy = function (err, cb) {
  2487. cb(err);
  2488. };
  2489. }).call(this)}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2490. },{"../errors":12,"./_stream_duplex":13,"./internal/streams/destroy":20,"./internal/streams/state":24,"./internal/streams/stream":25,"_process":47,"buffer":43,"inherits":10,"util-deprecate":39}],18:[function(require,module,exports){
  2491. (function (process){(function (){
  2492. 'use strict';
  2493. var _Object$setPrototypeO;
  2494. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2495. var finished = require('./end-of-stream');
  2496. var kLastResolve = Symbol('lastResolve');
  2497. var kLastReject = Symbol('lastReject');
  2498. var kError = Symbol('error');
  2499. var kEnded = Symbol('ended');
  2500. var kLastPromise = Symbol('lastPromise');
  2501. var kHandlePromise = Symbol('handlePromise');
  2502. var kStream = Symbol('stream');
  2503. function createIterResult(value, done) {
  2504. return {
  2505. value: value,
  2506. done: done
  2507. };
  2508. }
  2509. function readAndResolve(iter) {
  2510. var resolve = iter[kLastResolve];
  2511. if (resolve !== null) {
  2512. var data = iter[kStream].read(); // we defer if data is null
  2513. // we can be expecting either 'end' or
  2514. // 'error'
  2515. if (data !== null) {
  2516. iter[kLastPromise] = null;
  2517. iter[kLastResolve] = null;
  2518. iter[kLastReject] = null;
  2519. resolve(createIterResult(data, false));
  2520. }
  2521. }
  2522. }
  2523. function onReadable(iter) {
  2524. // we wait for the next tick, because it might
  2525. // emit an error with process.nextTick
  2526. process.nextTick(readAndResolve, iter);
  2527. }
  2528. function wrapForNext(lastPromise, iter) {
  2529. return function (resolve, reject) {
  2530. lastPromise.then(function () {
  2531. if (iter[kEnded]) {
  2532. resolve(createIterResult(undefined, true));
  2533. return;
  2534. }
  2535. iter[kHandlePromise](resolve, reject);
  2536. }, reject);
  2537. };
  2538. }
  2539. var AsyncIteratorPrototype = Object.getPrototypeOf(function () {});
  2540. var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPrototypeO = {
  2541. get stream() {
  2542. return this[kStream];
  2543. },
  2544. next: function next() {
  2545. var _this = this;
  2546. // if we have detected an error in the meanwhile
  2547. // reject straight away
  2548. var error = this[kError];
  2549. if (error !== null) {
  2550. return Promise.reject(error);
  2551. }
  2552. if (this[kEnded]) {
  2553. return Promise.resolve(createIterResult(undefined, true));
  2554. }
  2555. if (this[kStream].destroyed) {
  2556. // We need to defer via nextTick because if .destroy(err) is
  2557. // called, the error will be emitted via nextTick, and
  2558. // we cannot guarantee that there is no error lingering around
  2559. // waiting to be emitted.
  2560. return new Promise(function (resolve, reject) {
  2561. process.nextTick(function () {
  2562. if (_this[kError]) {
  2563. reject(_this[kError]);
  2564. } else {
  2565. resolve(createIterResult(undefined, true));
  2566. }
  2567. });
  2568. });
  2569. } // if we have multiple next() calls
  2570. // we will wait for the previous Promise to finish
  2571. // this logic is optimized to support for await loops,
  2572. // where next() is only called once at a time
  2573. var lastPromise = this[kLastPromise];
  2574. var promise;
  2575. if (lastPromise) {
  2576. promise = new Promise(wrapForNext(lastPromise, this));
  2577. } else {
  2578. // fast path needed to support multiple this.push()
  2579. // without triggering the next() queue
  2580. var data = this[kStream].read();
  2581. if (data !== null) {
  2582. return Promise.resolve(createIterResult(data, false));
  2583. }
  2584. promise = new Promise(this[kHandlePromise]);
  2585. }
  2586. this[kLastPromise] = promise;
  2587. return promise;
  2588. }
  2589. }, _defineProperty(_Object$setPrototypeO, Symbol.asyncIterator, function () {
  2590. return this;
  2591. }), _defineProperty(_Object$setPrototypeO, "return", function _return() {
  2592. var _this2 = this;
  2593. // destroy(err, cb) is a private API
  2594. // we can guarantee we have that here, because we control the
  2595. // Readable class this is attached to
  2596. return new Promise(function (resolve, reject) {
  2597. _this2[kStream].destroy(null, function (err) {
  2598. if (err) {
  2599. reject(err);
  2600. return;
  2601. }
  2602. resolve(createIterResult(undefined, true));
  2603. });
  2604. });
  2605. }), _Object$setPrototypeO), AsyncIteratorPrototype);
  2606. var createReadableStreamAsyncIterator = function createReadableStreamAsyncIterator(stream) {
  2607. var _Object$create;
  2608. var iterator = Object.create(ReadableStreamAsyncIteratorPrototype, (_Object$create = {}, _defineProperty(_Object$create, kStream, {
  2609. value: stream,
  2610. writable: true
  2611. }), _defineProperty(_Object$create, kLastResolve, {
  2612. value: null,
  2613. writable: true
  2614. }), _defineProperty(_Object$create, kLastReject, {
  2615. value: null,
  2616. writable: true
  2617. }), _defineProperty(_Object$create, kError, {
  2618. value: null,
  2619. writable: true
  2620. }), _defineProperty(_Object$create, kEnded, {
  2621. value: stream._readableState.endEmitted,
  2622. writable: true
  2623. }), _defineProperty(_Object$create, kHandlePromise, {
  2624. value: function value(resolve, reject) {
  2625. var data = iterator[kStream].read();
  2626. if (data) {
  2627. iterator[kLastPromise] = null;
  2628. iterator[kLastResolve] = null;
  2629. iterator[kLastReject] = null;
  2630. resolve(createIterResult(data, false));
  2631. } else {
  2632. iterator[kLastResolve] = resolve;
  2633. iterator[kLastReject] = reject;
  2634. }
  2635. },
  2636. writable: true
  2637. }), _Object$create));
  2638. iterator[kLastPromise] = null;
  2639. finished(stream, function (err) {
  2640. if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
  2641. var reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise
  2642. // returned by next() and store the error
  2643. if (reject !== null) {
  2644. iterator[kLastPromise] = null;
  2645. iterator[kLastResolve] = null;
  2646. iterator[kLastReject] = null;
  2647. reject(err);
  2648. }
  2649. iterator[kError] = err;
  2650. return;
  2651. }
  2652. var resolve = iterator[kLastResolve];
  2653. if (resolve !== null) {
  2654. iterator[kLastPromise] = null;
  2655. iterator[kLastResolve] = null;
  2656. iterator[kLastReject] = null;
  2657. resolve(createIterResult(undefined, true));
  2658. }
  2659. iterator[kEnded] = true;
  2660. });
  2661. stream.on('readable', onReadable.bind(null, iterator));
  2662. return iterator;
  2663. };
  2664. module.exports = createReadableStreamAsyncIterator;
  2665. }).call(this)}).call(this,require('_process'))
  2666. },{"./end-of-stream":21,"_process":47}],19:[function(require,module,exports){
  2667. 'use strict';
  2668. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  2669. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  2670. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2671. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2672. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  2673. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  2674. var _require = require('buffer'),
  2675. Buffer = _require.Buffer;
  2676. var _require2 = require('util'),
  2677. inspect = _require2.inspect;
  2678. var custom = inspect && inspect.custom || 'inspect';
  2679. function copyBuffer(src, target, offset) {
  2680. Buffer.prototype.copy.call(src, target, offset);
  2681. }
  2682. module.exports =
  2683. /*#__PURE__*/
  2684. function () {
  2685. function BufferList() {
  2686. _classCallCheck(this, BufferList);
  2687. this.head = null;
  2688. this.tail = null;
  2689. this.length = 0;
  2690. }
  2691. _createClass(BufferList, [{
  2692. key: "push",
  2693. value: function push(v) {
  2694. var entry = {
  2695. data: v,
  2696. next: null
  2697. };
  2698. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  2699. this.tail = entry;
  2700. ++this.length;
  2701. }
  2702. }, {
  2703. key: "unshift",
  2704. value: function unshift(v) {
  2705. var entry = {
  2706. data: v,
  2707. next: this.head
  2708. };
  2709. if (this.length === 0) this.tail = entry;
  2710. this.head = entry;
  2711. ++this.length;
  2712. }
  2713. }, {
  2714. key: "shift",
  2715. value: function shift() {
  2716. if (this.length === 0) return;
  2717. var ret = this.head.data;
  2718. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  2719. --this.length;
  2720. return ret;
  2721. }
  2722. }, {
  2723. key: "clear",
  2724. value: function clear() {
  2725. this.head = this.tail = null;
  2726. this.length = 0;
  2727. }
  2728. }, {
  2729. key: "join",
  2730. value: function join(s) {
  2731. if (this.length === 0) return '';
  2732. var p = this.head;
  2733. var ret = '' + p.data;
  2734. while (p = p.next) {
  2735. ret += s + p.data;
  2736. }
  2737. return ret;
  2738. }
  2739. }, {
  2740. key: "concat",
  2741. value: function concat(n) {
  2742. if (this.length === 0) return Buffer.alloc(0);
  2743. var ret = Buffer.allocUnsafe(n >>> 0);
  2744. var p = this.head;
  2745. var i = 0;
  2746. while (p) {
  2747. copyBuffer(p.data, ret, i);
  2748. i += p.data.length;
  2749. p = p.next;
  2750. }
  2751. return ret;
  2752. } // Consumes a specified amount of bytes or characters from the buffered data.
  2753. }, {
  2754. key: "consume",
  2755. value: function consume(n, hasStrings) {
  2756. var ret;
  2757. if (n < this.head.data.length) {
  2758. // `slice` is the same for buffers and strings.
  2759. ret = this.head.data.slice(0, n);
  2760. this.head.data = this.head.data.slice(n);
  2761. } else if (n === this.head.data.length) {
  2762. // First chunk is a perfect match.
  2763. ret = this.shift();
  2764. } else {
  2765. // Result spans more than one buffer.
  2766. ret = hasStrings ? this._getString(n) : this._getBuffer(n);
  2767. }
  2768. return ret;
  2769. }
  2770. }, {
  2771. key: "first",
  2772. value: function first() {
  2773. return this.head.data;
  2774. } // Consumes a specified amount of characters from the buffered data.
  2775. }, {
  2776. key: "_getString",
  2777. value: function _getString(n) {
  2778. var p = this.head;
  2779. var c = 1;
  2780. var ret = p.data;
  2781. n -= ret.length;
  2782. while (p = p.next) {
  2783. var str = p.data;
  2784. var nb = n > str.length ? str.length : n;
  2785. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  2786. n -= nb;
  2787. if (n === 0) {
  2788. if (nb === str.length) {
  2789. ++c;
  2790. if (p.next) this.head = p.next;else this.head = this.tail = null;
  2791. } else {
  2792. this.head = p;
  2793. p.data = str.slice(nb);
  2794. }
  2795. break;
  2796. }
  2797. ++c;
  2798. }
  2799. this.length -= c;
  2800. return ret;
  2801. } // Consumes a specified amount of bytes from the buffered data.
  2802. }, {
  2803. key: "_getBuffer",
  2804. value: function _getBuffer(n) {
  2805. var ret = Buffer.allocUnsafe(n);
  2806. var p = this.head;
  2807. var c = 1;
  2808. p.data.copy(ret);
  2809. n -= p.data.length;
  2810. while (p = p.next) {
  2811. var buf = p.data;
  2812. var nb = n > buf.length ? buf.length : n;
  2813. buf.copy(ret, ret.length - n, 0, nb);
  2814. n -= nb;
  2815. if (n === 0) {
  2816. if (nb === buf.length) {
  2817. ++c;
  2818. if (p.next) this.head = p.next;else this.head = this.tail = null;
  2819. } else {
  2820. this.head = p;
  2821. p.data = buf.slice(nb);
  2822. }
  2823. break;
  2824. }
  2825. ++c;
  2826. }
  2827. this.length -= c;
  2828. return ret;
  2829. } // Make sure the linked list only shows the minimal necessary information.
  2830. }, {
  2831. key: custom,
  2832. value: function value(_, options) {
  2833. return inspect(this, _objectSpread({}, options, {
  2834. // Only inspect one level.
  2835. depth: 0,
  2836. // It should not recurse.
  2837. customInspect: false
  2838. }));
  2839. }
  2840. }]);
  2841. return BufferList;
  2842. }();
  2843. },{"buffer":43,"util":42}],20:[function(require,module,exports){
  2844. (function (process){(function (){
  2845. 'use strict'; // undocumented cb() API, needed for core, not for public API
  2846. function destroy(err, cb) {
  2847. var _this = this;
  2848. var readableDestroyed = this._readableState && this._readableState.destroyed;
  2849. var writableDestroyed = this._writableState && this._writableState.destroyed;
  2850. if (readableDestroyed || writableDestroyed) {
  2851. if (cb) {
  2852. cb(err);
  2853. } else if (err) {
  2854. if (!this._writableState) {
  2855. process.nextTick(emitErrorNT, this, err);
  2856. } else if (!this._writableState.errorEmitted) {
  2857. this._writableState.errorEmitted = true;
  2858. process.nextTick(emitErrorNT, this, err);
  2859. }
  2860. }
  2861. return this;
  2862. } // we set destroyed to true before firing error callbacks in order
  2863. // to make it re-entrance safe in case destroy() is called within callbacks
  2864. if (this._readableState) {
  2865. this._readableState.destroyed = true;
  2866. } // if this is a duplex stream mark the writable part as destroyed as well
  2867. if (this._writableState) {
  2868. this._writableState.destroyed = true;
  2869. }
  2870. this._destroy(err || null, function (err) {
  2871. if (!cb && err) {
  2872. if (!_this._writableState) {
  2873. process.nextTick(emitErrorAndCloseNT, _this, err);
  2874. } else if (!_this._writableState.errorEmitted) {
  2875. _this._writableState.errorEmitted = true;
  2876. process.nextTick(emitErrorAndCloseNT, _this, err);
  2877. } else {
  2878. process.nextTick(emitCloseNT, _this);
  2879. }
  2880. } else if (cb) {
  2881. process.nextTick(emitCloseNT, _this);
  2882. cb(err);
  2883. } else {
  2884. process.nextTick(emitCloseNT, _this);
  2885. }
  2886. });
  2887. return this;
  2888. }
  2889. function emitErrorAndCloseNT(self, err) {
  2890. emitErrorNT(self, err);
  2891. emitCloseNT(self);
  2892. }
  2893. function emitCloseNT(self) {
  2894. if (self._writableState && !self._writableState.emitClose) return;
  2895. if (self._readableState && !self._readableState.emitClose) return;
  2896. self.emit('close');
  2897. }
  2898. function undestroy() {
  2899. if (this._readableState) {
  2900. this._readableState.destroyed = false;
  2901. this._readableState.reading = false;
  2902. this._readableState.ended = false;
  2903. this._readableState.endEmitted = false;
  2904. }
  2905. if (this._writableState) {
  2906. this._writableState.destroyed = false;
  2907. this._writableState.ended = false;
  2908. this._writableState.ending = false;
  2909. this._writableState.finalCalled = false;
  2910. this._writableState.prefinished = false;
  2911. this._writableState.finished = false;
  2912. this._writableState.errorEmitted = false;
  2913. }
  2914. }
  2915. function emitErrorNT(self, err) {
  2916. self.emit('error', err);
  2917. }
  2918. function errorOrDestroy(stream, err) {
  2919. // We have tests that rely on errors being emitted
  2920. // in the same tick, so changing this is semver major.
  2921. // For now when you opt-in to autoDestroy we allow
  2922. // the error to be emitted nextTick. In a future
  2923. // semver major update we should change the default to this.
  2924. var rState = stream._readableState;
  2925. var wState = stream._writableState;
  2926. if (rState && rState.autoDestroy || wState && wState.autoDestroy) stream.destroy(err);else stream.emit('error', err);
  2927. }
  2928. module.exports = {
  2929. destroy: destroy,
  2930. undestroy: undestroy,
  2931. errorOrDestroy: errorOrDestroy
  2932. };
  2933. }).call(this)}).call(this,require('_process'))
  2934. },{"_process":47}],21:[function(require,module,exports){
  2935. // Ported from https://github.com/mafintosh/end-of-stream with
  2936. // permission from the author, Mathias Buus (@mafintosh).
  2937. 'use strict';
  2938. var ERR_STREAM_PREMATURE_CLOSE = require('../../../errors').codes.ERR_STREAM_PREMATURE_CLOSE;
  2939. function once(callback) {
  2940. var called = false;
  2941. return function () {
  2942. if (called) return;
  2943. called = true;
  2944. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  2945. args[_key] = arguments[_key];
  2946. }
  2947. callback.apply(this, args);
  2948. };
  2949. }
  2950. function noop() {}
  2951. function isRequest(stream) {
  2952. return stream.setHeader && typeof stream.abort === 'function';
  2953. }
  2954. function eos(stream, opts, callback) {
  2955. if (typeof opts === 'function') return eos(stream, null, opts);
  2956. if (!opts) opts = {};
  2957. callback = once(callback || noop);
  2958. var readable = opts.readable || opts.readable !== false && stream.readable;
  2959. var writable = opts.writable || opts.writable !== false && stream.writable;
  2960. var onlegacyfinish = function onlegacyfinish() {
  2961. if (!stream.writable) onfinish();
  2962. };
  2963. var writableEnded = stream._writableState && stream._writableState.finished;
  2964. var onfinish = function onfinish() {
  2965. writable = false;
  2966. writableEnded = true;
  2967. if (!readable) callback.call(stream);
  2968. };
  2969. var readableEnded = stream._readableState && stream._readableState.endEmitted;
  2970. var onend = function onend() {
  2971. readable = false;
  2972. readableEnded = true;
  2973. if (!writable) callback.call(stream);
  2974. };
  2975. var onerror = function onerror(err) {
  2976. callback.call(stream, err);
  2977. };
  2978. var onclose = function onclose() {
  2979. var err;
  2980. if (readable && !readableEnded) {
  2981. if (!stream._readableState || !stream._readableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
  2982. return callback.call(stream, err);
  2983. }
  2984. if (writable && !writableEnded) {
  2985. if (!stream._writableState || !stream._writableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
  2986. return callback.call(stream, err);
  2987. }
  2988. };
  2989. var onrequest = function onrequest() {
  2990. stream.req.on('finish', onfinish);
  2991. };
  2992. if (isRequest(stream)) {
  2993. stream.on('complete', onfinish);
  2994. stream.on('abort', onclose);
  2995. if (stream.req) onrequest();else stream.on('request', onrequest);
  2996. } else if (writable && !stream._writableState) {
  2997. // legacy streams
  2998. stream.on('end', onlegacyfinish);
  2999. stream.on('close', onlegacyfinish);
  3000. }
  3001. stream.on('end', onend);
  3002. stream.on('finish', onfinish);
  3003. if (opts.error !== false) stream.on('error', onerror);
  3004. stream.on('close', onclose);
  3005. return function () {
  3006. stream.removeListener('complete', onfinish);
  3007. stream.removeListener('abort', onclose);
  3008. stream.removeListener('request', onrequest);
  3009. if (stream.req) stream.req.removeListener('finish', onfinish);
  3010. stream.removeListener('end', onlegacyfinish);
  3011. stream.removeListener('close', onlegacyfinish);
  3012. stream.removeListener('finish', onfinish);
  3013. stream.removeListener('end', onend);
  3014. stream.removeListener('error', onerror);
  3015. stream.removeListener('close', onclose);
  3016. };
  3017. }
  3018. module.exports = eos;
  3019. },{"../../../errors":12}],22:[function(require,module,exports){
  3020. module.exports = function () {
  3021. throw new Error('Readable.from is not available in the browser')
  3022. };
  3023. },{}],23:[function(require,module,exports){
  3024. // Ported from https://github.com/mafintosh/pump with
  3025. // permission from the author, Mathias Buus (@mafintosh).
  3026. 'use strict';
  3027. var eos;
  3028. function once(callback) {
  3029. var called = false;
  3030. return function () {
  3031. if (called) return;
  3032. called = true;
  3033. callback.apply(void 0, arguments);
  3034. };
  3035. }
  3036. var _require$codes = require('../../../errors').codes,
  3037. ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS,
  3038. ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED;
  3039. function noop(err) {
  3040. // Rethrow the error if it exists to avoid swallowing it
  3041. if (err) throw err;
  3042. }
  3043. function isRequest(stream) {
  3044. return stream.setHeader && typeof stream.abort === 'function';
  3045. }
  3046. function destroyer(stream, reading, writing, callback) {
  3047. callback = once(callback);
  3048. var closed = false;
  3049. stream.on('close', function () {
  3050. closed = true;
  3051. });
  3052. if (eos === undefined) eos = require('./end-of-stream');
  3053. eos(stream, {
  3054. readable: reading,
  3055. writable: writing
  3056. }, function (err) {
  3057. if (err) return callback(err);
  3058. closed = true;
  3059. callback();
  3060. });
  3061. var destroyed = false;
  3062. return function (err) {
  3063. if (closed) return;
  3064. if (destroyed) return;
  3065. destroyed = true; // request.destroy just do .end - .abort is what we want
  3066. if (isRequest(stream)) return stream.abort();
  3067. if (typeof stream.destroy === 'function') return stream.destroy();
  3068. callback(err || new ERR_STREAM_DESTROYED('pipe'));
  3069. };
  3070. }
  3071. function call(fn) {
  3072. fn();
  3073. }
  3074. function pipe(from, to) {
  3075. return from.pipe(to);
  3076. }
  3077. function popCallback(streams) {
  3078. if (!streams.length) return noop;
  3079. if (typeof streams[streams.length - 1] !== 'function') return noop;
  3080. return streams.pop();
  3081. }
  3082. function pipeline() {
  3083. for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) {
  3084. streams[_key] = arguments[_key];
  3085. }
  3086. var callback = popCallback(streams);
  3087. if (Array.isArray(streams[0])) streams = streams[0];
  3088. if (streams.length < 2) {
  3089. throw new ERR_MISSING_ARGS('streams');
  3090. }
  3091. var error;
  3092. var destroys = streams.map(function (stream, i) {
  3093. var reading = i < streams.length - 1;
  3094. var writing = i > 0;
  3095. return destroyer(stream, reading, writing, function (err) {
  3096. if (!error) error = err;
  3097. if (err) destroys.forEach(call);
  3098. if (reading) return;
  3099. destroys.forEach(call);
  3100. callback(error);
  3101. });
  3102. });
  3103. return streams.reduce(pipe);
  3104. }
  3105. module.exports = pipeline;
  3106. },{"../../../errors":12,"./end-of-stream":21}],24:[function(require,module,exports){
  3107. 'use strict';
  3108. var ERR_INVALID_OPT_VALUE = require('../../../errors').codes.ERR_INVALID_OPT_VALUE;
  3109. function highWaterMarkFrom(options, isDuplex, duplexKey) {
  3110. return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null;
  3111. }
  3112. function getHighWaterMark(state, options, duplexKey, isDuplex) {
  3113. var hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
  3114. if (hwm != null) {
  3115. if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) {
  3116. var name = isDuplex ? duplexKey : 'highWaterMark';
  3117. throw new ERR_INVALID_OPT_VALUE(name, hwm);
  3118. }
  3119. return Math.floor(hwm);
  3120. } // Default value
  3121. return state.objectMode ? 16 : 16 * 1024;
  3122. }
  3123. module.exports = {
  3124. getHighWaterMark: getHighWaterMark
  3125. };
  3126. },{"../../../errors":12}],25:[function(require,module,exports){
  3127. module.exports = require('events').EventEmitter;
  3128. },{"events":44}],26:[function(require,module,exports){
  3129. exports = module.exports = require('./lib/_stream_readable.js');
  3130. exports.Stream = exports;
  3131. exports.Readable = exports;
  3132. exports.Writable = require('./lib/_stream_writable.js');
  3133. exports.Duplex = require('./lib/_stream_duplex.js');
  3134. exports.Transform = require('./lib/_stream_transform.js');
  3135. exports.PassThrough = require('./lib/_stream_passthrough.js');
  3136. exports.finished = require('./lib/internal/streams/end-of-stream.js');
  3137. exports.pipeline = require('./lib/internal/streams/pipeline.js');
  3138. },{"./lib/_stream_duplex.js":13,"./lib/_stream_passthrough.js":14,"./lib/_stream_readable.js":15,"./lib/_stream_transform.js":16,"./lib/_stream_writable.js":17,"./lib/internal/streams/end-of-stream.js":21,"./lib/internal/streams/pipeline.js":23}],27:[function(require,module,exports){
  3139. 'use strict'
  3140. var Buffer = require('buffer').Buffer
  3141. var inherits = require('inherits')
  3142. var HashBase = require('hash-base')
  3143. var ARRAY16 = new Array(16)
  3144. var zl = [
  3145. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  3146. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  3147. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  3148. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  3149. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  3150. ]
  3151. var zr = [
  3152. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  3153. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  3154. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  3155. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  3156. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  3157. ]
  3158. var sl = [
  3159. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  3160. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  3161. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  3162. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  3163. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  3164. ]
  3165. var sr = [
  3166. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  3167. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  3168. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  3169. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  3170. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  3171. ]
  3172. var hl = [0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e]
  3173. var hr = [0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000]
  3174. function RIPEMD160 () {
  3175. HashBase.call(this, 64)
  3176. // state
  3177. this._a = 0x67452301
  3178. this._b = 0xefcdab89
  3179. this._c = 0x98badcfe
  3180. this._d = 0x10325476
  3181. this._e = 0xc3d2e1f0
  3182. }
  3183. inherits(RIPEMD160, HashBase)
  3184. RIPEMD160.prototype._update = function () {
  3185. var words = ARRAY16
  3186. for (var j = 0; j < 16; ++j) words[j] = this._block.readInt32LE(j * 4)
  3187. var al = this._a | 0
  3188. var bl = this._b | 0
  3189. var cl = this._c | 0
  3190. var dl = this._d | 0
  3191. var el = this._e | 0
  3192. var ar = this._a | 0
  3193. var br = this._b | 0
  3194. var cr = this._c | 0
  3195. var dr = this._d | 0
  3196. var er = this._e | 0
  3197. // computation
  3198. for (var i = 0; i < 80; i += 1) {
  3199. var tl
  3200. var tr
  3201. if (i < 16) {
  3202. tl = fn1(al, bl, cl, dl, el, words[zl[i]], hl[0], sl[i])
  3203. tr = fn5(ar, br, cr, dr, er, words[zr[i]], hr[0], sr[i])
  3204. } else if (i < 32) {
  3205. tl = fn2(al, bl, cl, dl, el, words[zl[i]], hl[1], sl[i])
  3206. tr = fn4(ar, br, cr, dr, er, words[zr[i]], hr[1], sr[i])
  3207. } else if (i < 48) {
  3208. tl = fn3(al, bl, cl, dl, el, words[zl[i]], hl[2], sl[i])
  3209. tr = fn3(ar, br, cr, dr, er, words[zr[i]], hr[2], sr[i])
  3210. } else if (i < 64) {
  3211. tl = fn4(al, bl, cl, dl, el, words[zl[i]], hl[3], sl[i])
  3212. tr = fn2(ar, br, cr, dr, er, words[zr[i]], hr[3], sr[i])
  3213. } else { // if (i<80) {
  3214. tl = fn5(al, bl, cl, dl, el, words[zl[i]], hl[4], sl[i])
  3215. tr = fn1(ar, br, cr, dr, er, words[zr[i]], hr[4], sr[i])
  3216. }
  3217. al = el
  3218. el = dl
  3219. dl = rotl(cl, 10)
  3220. cl = bl
  3221. bl = tl
  3222. ar = er
  3223. er = dr
  3224. dr = rotl(cr, 10)
  3225. cr = br
  3226. br = tr
  3227. }
  3228. // update state
  3229. var t = (this._b + cl + dr) | 0
  3230. this._b = (this._c + dl + er) | 0
  3231. this._c = (this._d + el + ar) | 0
  3232. this._d = (this._e + al + br) | 0
  3233. this._e = (this._a + bl + cr) | 0
  3234. this._a = t
  3235. }
  3236. RIPEMD160.prototype._digest = function () {
  3237. // create padding and handle blocks
  3238. this._block[this._blockOffset++] = 0x80
  3239. if (this._blockOffset > 56) {
  3240. this._block.fill(0, this._blockOffset, 64)
  3241. this._update()
  3242. this._blockOffset = 0
  3243. }
  3244. this._block.fill(0, this._blockOffset, 56)
  3245. this._block.writeUInt32LE(this._length[0], 56)
  3246. this._block.writeUInt32LE(this._length[1], 60)
  3247. this._update()
  3248. // produce result
  3249. var buffer = Buffer.alloc ? Buffer.alloc(20) : new Buffer(20)
  3250. buffer.writeInt32LE(this._a, 0)
  3251. buffer.writeInt32LE(this._b, 4)
  3252. buffer.writeInt32LE(this._c, 8)
  3253. buffer.writeInt32LE(this._d, 12)
  3254. buffer.writeInt32LE(this._e, 16)
  3255. return buffer
  3256. }
  3257. function rotl (x, n) {
  3258. return (x << n) | (x >>> (32 - n))
  3259. }
  3260. function fn1 (a, b, c, d, e, m, k, s) {
  3261. return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + e) | 0
  3262. }
  3263. function fn2 (a, b, c, d, e, m, k, s) {
  3264. return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + e) | 0
  3265. }
  3266. function fn3 (a, b, c, d, e, m, k, s) {
  3267. return (rotl((a + ((b | (~c)) ^ d) + m + k) | 0, s) + e) | 0
  3268. }
  3269. function fn4 (a, b, c, d, e, m, k, s) {
  3270. return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + e) | 0
  3271. }
  3272. function fn5 (a, b, c, d, e, m, k, s) {
  3273. return (rotl((a + (b ^ (c | (~d))) + m + k) | 0, s) + e) | 0
  3274. }
  3275. module.exports = RIPEMD160
  3276. },{"buffer":43,"hash-base":9,"inherits":10}],28:[function(require,module,exports){
  3277. /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  3278. /* eslint-disable node/no-deprecated-api */
  3279. var buffer = require('buffer')
  3280. var Buffer = buffer.Buffer
  3281. // alternative to using Object.keys for old browsers
  3282. function copyProps (src, dst) {
  3283. for (var key in src) {
  3284. dst[key] = src[key]
  3285. }
  3286. }
  3287. if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
  3288. module.exports = buffer
  3289. } else {
  3290. // Copy properties from require('buffer')
  3291. copyProps(buffer, exports)
  3292. exports.Buffer = SafeBuffer
  3293. }
  3294. function SafeBuffer (arg, encodingOrOffset, length) {
  3295. return Buffer(arg, encodingOrOffset, length)
  3296. }
  3297. SafeBuffer.prototype = Object.create(Buffer.prototype)
  3298. // Copy static methods from Buffer
  3299. copyProps(Buffer, SafeBuffer)
  3300. SafeBuffer.from = function (arg, encodingOrOffset, length) {
  3301. if (typeof arg === 'number') {
  3302. throw new TypeError('Argument must not be a number')
  3303. }
  3304. return Buffer(arg, encodingOrOffset, length)
  3305. }
  3306. SafeBuffer.alloc = function (size, fill, encoding) {
  3307. if (typeof size !== 'number') {
  3308. throw new TypeError('Argument must be a number')
  3309. }
  3310. var buf = Buffer(size)
  3311. if (fill !== undefined) {
  3312. if (typeof encoding === 'string') {
  3313. buf.fill(fill, encoding)
  3314. } else {
  3315. buf.fill(fill)
  3316. }
  3317. } else {
  3318. buf.fill(0)
  3319. }
  3320. return buf
  3321. }
  3322. SafeBuffer.allocUnsafe = function (size) {
  3323. if (typeof size !== 'number') {
  3324. throw new TypeError('Argument must be a number')
  3325. }
  3326. return Buffer(size)
  3327. }
  3328. SafeBuffer.allocUnsafeSlow = function (size) {
  3329. if (typeof size !== 'number') {
  3330. throw new TypeError('Argument must be a number')
  3331. }
  3332. return buffer.SlowBuffer(size)
  3333. }
  3334. },{"buffer":43}],29:[function(require,module,exports){
  3335. var Buffer = require('safe-buffer').Buffer
  3336. // prototype class for hash functions
  3337. function Hash (blockSize, finalSize) {
  3338. this._block = Buffer.alloc(blockSize)
  3339. this._finalSize = finalSize
  3340. this._blockSize = blockSize
  3341. this._len = 0
  3342. }
  3343. Hash.prototype.update = function (data, enc) {
  3344. if (typeof data === 'string') {
  3345. enc = enc || 'utf8'
  3346. data = Buffer.from(data, enc)
  3347. }
  3348. var block = this._block
  3349. var blockSize = this._blockSize
  3350. var length = data.length
  3351. var accum = this._len
  3352. for (var offset = 0; offset < length;) {
  3353. var assigned = accum % blockSize
  3354. var remainder = Math.min(length - offset, blockSize - assigned)
  3355. for (var i = 0; i < remainder; i++) {
  3356. block[assigned + i] = data[offset + i]
  3357. }
  3358. accum += remainder
  3359. offset += remainder
  3360. if ((accum % blockSize) === 0) {
  3361. this._update(block)
  3362. }
  3363. }
  3364. this._len += length
  3365. return this
  3366. }
  3367. Hash.prototype.digest = function (enc) {
  3368. var rem = this._len % this._blockSize
  3369. this._block[rem] = 0x80
  3370. // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
  3371. // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
  3372. this._block.fill(0, rem + 1)
  3373. if (rem >= this._finalSize) {
  3374. this._update(this._block)
  3375. this._block.fill(0)
  3376. }
  3377. var bits = this._len * 8
  3378. // uint32
  3379. if (bits <= 0xffffffff) {
  3380. this._block.writeUInt32BE(bits, this._blockSize - 4)
  3381. // uint64
  3382. } else {
  3383. var lowBits = (bits & 0xffffffff) >>> 0
  3384. var highBits = (bits - lowBits) / 0x100000000
  3385. this._block.writeUInt32BE(highBits, this._blockSize - 8)
  3386. this._block.writeUInt32BE(lowBits, this._blockSize - 4)
  3387. }
  3388. this._update(this._block)
  3389. var hash = this._hash()
  3390. return enc ? hash.toString(enc) : hash
  3391. }
  3392. Hash.prototype._update = function () {
  3393. throw new Error('_update must be implemented by subclass')
  3394. }
  3395. module.exports = Hash
  3396. },{"safe-buffer":28}],30:[function(require,module,exports){
  3397. var exports = module.exports = function SHA (algorithm) {
  3398. algorithm = algorithm.toLowerCase()
  3399. var Algorithm = exports[algorithm]
  3400. if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)')
  3401. return new Algorithm()
  3402. }
  3403. exports.sha = require('./sha')
  3404. exports.sha1 = require('./sha1')
  3405. exports.sha224 = require('./sha224')
  3406. exports.sha256 = require('./sha256')
  3407. exports.sha384 = require('./sha384')
  3408. exports.sha512 = require('./sha512')
  3409. },{"./sha":31,"./sha1":32,"./sha224":33,"./sha256":34,"./sha384":35,"./sha512":36}],31:[function(require,module,exports){
  3410. /*
  3411. * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined
  3412. * in FIPS PUB 180-1
  3413. * This source code is derived from sha1.js of the same repository.
  3414. * The difference between SHA-0 and SHA-1 is just a bitwise rotate left
  3415. * operation was added.
  3416. */
  3417. var inherits = require('inherits')
  3418. var Hash = require('./hash')
  3419. var Buffer = require('safe-buffer').Buffer
  3420. var K = [
  3421. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  3422. ]
  3423. var W = new Array(80)
  3424. function Sha () {
  3425. this.init()
  3426. this._w = W
  3427. Hash.call(this, 64, 56)
  3428. }
  3429. inherits(Sha, Hash)
  3430. Sha.prototype.init = function () {
  3431. this._a = 0x67452301
  3432. this._b = 0xefcdab89
  3433. this._c = 0x98badcfe
  3434. this._d = 0x10325476
  3435. this._e = 0xc3d2e1f0
  3436. return this
  3437. }
  3438. function rotl5 (num) {
  3439. return (num << 5) | (num >>> 27)
  3440. }
  3441. function rotl30 (num) {
  3442. return (num << 30) | (num >>> 2)
  3443. }
  3444. function ft (s, b, c, d) {
  3445. if (s === 0) return (b & c) | ((~b) & d)
  3446. if (s === 2) return (b & c) | (b & d) | (c & d)
  3447. return b ^ c ^ d
  3448. }
  3449. Sha.prototype._update = function (M) {
  3450. var W = this._w
  3451. var a = this._a | 0
  3452. var b = this._b | 0
  3453. var c = this._c | 0
  3454. var d = this._d | 0
  3455. var e = this._e | 0
  3456. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  3457. for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]
  3458. for (var j = 0; j < 80; ++j) {
  3459. var s = ~~(j / 20)
  3460. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  3461. e = d
  3462. d = c
  3463. c = rotl30(b)
  3464. b = a
  3465. a = t
  3466. }
  3467. this._a = (a + this._a) | 0
  3468. this._b = (b + this._b) | 0
  3469. this._c = (c + this._c) | 0
  3470. this._d = (d + this._d) | 0
  3471. this._e = (e + this._e) | 0
  3472. }
  3473. Sha.prototype._hash = function () {
  3474. var H = Buffer.allocUnsafe(20)
  3475. H.writeInt32BE(this._a | 0, 0)
  3476. H.writeInt32BE(this._b | 0, 4)
  3477. H.writeInt32BE(this._c | 0, 8)
  3478. H.writeInt32BE(this._d | 0, 12)
  3479. H.writeInt32BE(this._e | 0, 16)
  3480. return H
  3481. }
  3482. module.exports = Sha
  3483. },{"./hash":29,"inherits":10,"safe-buffer":28}],32:[function(require,module,exports){
  3484. /*
  3485. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  3486. * in FIPS PUB 180-1
  3487. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  3488. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  3489. * Distributed under the BSD License
  3490. * See http://pajhome.org.uk/crypt/md5 for details.
  3491. */
  3492. var inherits = require('inherits')
  3493. var Hash = require('./hash')
  3494. var Buffer = require('safe-buffer').Buffer
  3495. var K = [
  3496. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  3497. ]
  3498. var W = new Array(80)
  3499. function Sha1 () {
  3500. this.init()
  3501. this._w = W
  3502. Hash.call(this, 64, 56)
  3503. }
  3504. inherits(Sha1, Hash)
  3505. Sha1.prototype.init = function () {
  3506. this._a = 0x67452301
  3507. this._b = 0xefcdab89
  3508. this._c = 0x98badcfe
  3509. this._d = 0x10325476
  3510. this._e = 0xc3d2e1f0
  3511. return this
  3512. }
  3513. function rotl1 (num) {
  3514. return (num << 1) | (num >>> 31)
  3515. }
  3516. function rotl5 (num) {
  3517. return (num << 5) | (num >>> 27)
  3518. }
  3519. function rotl30 (num) {
  3520. return (num << 30) | (num >>> 2)
  3521. }
  3522. function ft (s, b, c, d) {
  3523. if (s === 0) return (b & c) | ((~b) & d)
  3524. if (s === 2) return (b & c) | (b & d) | (c & d)
  3525. return b ^ c ^ d
  3526. }
  3527. Sha1.prototype._update = function (M) {
  3528. var W = this._w
  3529. var a = this._a | 0
  3530. var b = this._b | 0
  3531. var c = this._c | 0
  3532. var d = this._d | 0
  3533. var e = this._e | 0
  3534. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  3535. for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
  3536. for (var j = 0; j < 80; ++j) {
  3537. var s = ~~(j / 20)
  3538. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  3539. e = d
  3540. d = c
  3541. c = rotl30(b)
  3542. b = a
  3543. a = t
  3544. }
  3545. this._a = (a + this._a) | 0
  3546. this._b = (b + this._b) | 0
  3547. this._c = (c + this._c) | 0
  3548. this._d = (d + this._d) | 0
  3549. this._e = (e + this._e) | 0
  3550. }
  3551. Sha1.prototype._hash = function () {
  3552. var H = Buffer.allocUnsafe(20)
  3553. H.writeInt32BE(this._a | 0, 0)
  3554. H.writeInt32BE(this._b | 0, 4)
  3555. H.writeInt32BE(this._c | 0, 8)
  3556. H.writeInt32BE(this._d | 0, 12)
  3557. H.writeInt32BE(this._e | 0, 16)
  3558. return H
  3559. }
  3560. module.exports = Sha1
  3561. },{"./hash":29,"inherits":10,"safe-buffer":28}],33:[function(require,module,exports){
  3562. /**
  3563. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  3564. * in FIPS 180-2
  3565. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  3566. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  3567. *
  3568. */
  3569. var inherits = require('inherits')
  3570. var Sha256 = require('./sha256')
  3571. var Hash = require('./hash')
  3572. var Buffer = require('safe-buffer').Buffer
  3573. var W = new Array(64)
  3574. function Sha224 () {
  3575. this.init()
  3576. this._w = W // new Array(64)
  3577. Hash.call(this, 64, 56)
  3578. }
  3579. inherits(Sha224, Sha256)
  3580. Sha224.prototype.init = function () {
  3581. this._a = 0xc1059ed8
  3582. this._b = 0x367cd507
  3583. this._c = 0x3070dd17
  3584. this._d = 0xf70e5939
  3585. this._e = 0xffc00b31
  3586. this._f = 0x68581511
  3587. this._g = 0x64f98fa7
  3588. this._h = 0xbefa4fa4
  3589. return this
  3590. }
  3591. Sha224.prototype._hash = function () {
  3592. var H = Buffer.allocUnsafe(28)
  3593. H.writeInt32BE(this._a, 0)
  3594. H.writeInt32BE(this._b, 4)
  3595. H.writeInt32BE(this._c, 8)
  3596. H.writeInt32BE(this._d, 12)
  3597. H.writeInt32BE(this._e, 16)
  3598. H.writeInt32BE(this._f, 20)
  3599. H.writeInt32BE(this._g, 24)
  3600. return H
  3601. }
  3602. module.exports = Sha224
  3603. },{"./hash":29,"./sha256":34,"inherits":10,"safe-buffer":28}],34:[function(require,module,exports){
  3604. /**
  3605. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  3606. * in FIPS 180-2
  3607. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  3608. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  3609. *
  3610. */
  3611. var inherits = require('inherits')
  3612. var Hash = require('./hash')
  3613. var Buffer = require('safe-buffer').Buffer
  3614. var K = [
  3615. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  3616. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  3617. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  3618. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  3619. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  3620. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  3621. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  3622. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  3623. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  3624. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  3625. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  3626. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  3627. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  3628. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  3629. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  3630. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  3631. ]
  3632. var W = new Array(64)
  3633. function Sha256 () {
  3634. this.init()
  3635. this._w = W // new Array(64)
  3636. Hash.call(this, 64, 56)
  3637. }
  3638. inherits(Sha256, Hash)
  3639. Sha256.prototype.init = function () {
  3640. this._a = 0x6a09e667
  3641. this._b = 0xbb67ae85
  3642. this._c = 0x3c6ef372
  3643. this._d = 0xa54ff53a
  3644. this._e = 0x510e527f
  3645. this._f = 0x9b05688c
  3646. this._g = 0x1f83d9ab
  3647. this._h = 0x5be0cd19
  3648. return this
  3649. }
  3650. function ch (x, y, z) {
  3651. return z ^ (x & (y ^ z))
  3652. }
  3653. function maj (x, y, z) {
  3654. return (x & y) | (z & (x | y))
  3655. }
  3656. function sigma0 (x) {
  3657. return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
  3658. }
  3659. function sigma1 (x) {
  3660. return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
  3661. }
  3662. function gamma0 (x) {
  3663. return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
  3664. }
  3665. function gamma1 (x) {
  3666. return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
  3667. }
  3668. Sha256.prototype._update = function (M) {
  3669. var W = this._w
  3670. var a = this._a | 0
  3671. var b = this._b | 0
  3672. var c = this._c | 0
  3673. var d = this._d | 0
  3674. var e = this._e | 0
  3675. var f = this._f | 0
  3676. var g = this._g | 0
  3677. var h = this._h | 0
  3678. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  3679. for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
  3680. for (var j = 0; j < 64; ++j) {
  3681. var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
  3682. var T2 = (sigma0(a) + maj(a, b, c)) | 0
  3683. h = g
  3684. g = f
  3685. f = e
  3686. e = (d + T1) | 0
  3687. d = c
  3688. c = b
  3689. b = a
  3690. a = (T1 + T2) | 0
  3691. }
  3692. this._a = (a + this._a) | 0
  3693. this._b = (b + this._b) | 0
  3694. this._c = (c + this._c) | 0
  3695. this._d = (d + this._d) | 0
  3696. this._e = (e + this._e) | 0
  3697. this._f = (f + this._f) | 0
  3698. this._g = (g + this._g) | 0
  3699. this._h = (h + this._h) | 0
  3700. }
  3701. Sha256.prototype._hash = function () {
  3702. var H = Buffer.allocUnsafe(32)
  3703. H.writeInt32BE(this._a, 0)
  3704. H.writeInt32BE(this._b, 4)
  3705. H.writeInt32BE(this._c, 8)
  3706. H.writeInt32BE(this._d, 12)
  3707. H.writeInt32BE(this._e, 16)
  3708. H.writeInt32BE(this._f, 20)
  3709. H.writeInt32BE(this._g, 24)
  3710. H.writeInt32BE(this._h, 28)
  3711. return H
  3712. }
  3713. module.exports = Sha256
  3714. },{"./hash":29,"inherits":10,"safe-buffer":28}],35:[function(require,module,exports){
  3715. var inherits = require('inherits')
  3716. var SHA512 = require('./sha512')
  3717. var Hash = require('./hash')
  3718. var Buffer = require('safe-buffer').Buffer
  3719. var W = new Array(160)
  3720. function Sha384 () {
  3721. this.init()
  3722. this._w = W
  3723. Hash.call(this, 128, 112)
  3724. }
  3725. inherits(Sha384, SHA512)
  3726. Sha384.prototype.init = function () {
  3727. this._ah = 0xcbbb9d5d
  3728. this._bh = 0x629a292a
  3729. this._ch = 0x9159015a
  3730. this._dh = 0x152fecd8
  3731. this._eh = 0x67332667
  3732. this._fh = 0x8eb44a87
  3733. this._gh = 0xdb0c2e0d
  3734. this._hh = 0x47b5481d
  3735. this._al = 0xc1059ed8
  3736. this._bl = 0x367cd507
  3737. this._cl = 0x3070dd17
  3738. this._dl = 0xf70e5939
  3739. this._el = 0xffc00b31
  3740. this._fl = 0x68581511
  3741. this._gl = 0x64f98fa7
  3742. this._hl = 0xbefa4fa4
  3743. return this
  3744. }
  3745. Sha384.prototype._hash = function () {
  3746. var H = Buffer.allocUnsafe(48)
  3747. function writeInt64BE (h, l, offset) {
  3748. H.writeInt32BE(h, offset)
  3749. H.writeInt32BE(l, offset + 4)
  3750. }
  3751. writeInt64BE(this._ah, this._al, 0)
  3752. writeInt64BE(this._bh, this._bl, 8)
  3753. writeInt64BE(this._ch, this._cl, 16)
  3754. writeInt64BE(this._dh, this._dl, 24)
  3755. writeInt64BE(this._eh, this._el, 32)
  3756. writeInt64BE(this._fh, this._fl, 40)
  3757. return H
  3758. }
  3759. module.exports = Sha384
  3760. },{"./hash":29,"./sha512":36,"inherits":10,"safe-buffer":28}],36:[function(require,module,exports){
  3761. var inherits = require('inherits')
  3762. var Hash = require('./hash')
  3763. var Buffer = require('safe-buffer').Buffer
  3764. var K = [
  3765. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  3766. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  3767. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  3768. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  3769. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  3770. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  3771. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  3772. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  3773. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  3774. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  3775. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  3776. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  3777. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  3778. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  3779. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  3780. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  3781. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  3782. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  3783. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  3784. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  3785. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  3786. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  3787. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  3788. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  3789. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  3790. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  3791. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  3792. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  3793. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  3794. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  3795. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  3796. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  3797. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  3798. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  3799. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  3800. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  3801. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  3802. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  3803. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  3804. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  3805. ]
  3806. var W = new Array(160)
  3807. function Sha512 () {
  3808. this.init()
  3809. this._w = W
  3810. Hash.call(this, 128, 112)
  3811. }
  3812. inherits(Sha512, Hash)
  3813. Sha512.prototype.init = function () {
  3814. this._ah = 0x6a09e667
  3815. this._bh = 0xbb67ae85
  3816. this._ch = 0x3c6ef372
  3817. this._dh = 0xa54ff53a
  3818. this._eh = 0x510e527f
  3819. this._fh = 0x9b05688c
  3820. this._gh = 0x1f83d9ab
  3821. this._hh = 0x5be0cd19
  3822. this._al = 0xf3bcc908
  3823. this._bl = 0x84caa73b
  3824. this._cl = 0xfe94f82b
  3825. this._dl = 0x5f1d36f1
  3826. this._el = 0xade682d1
  3827. this._fl = 0x2b3e6c1f
  3828. this._gl = 0xfb41bd6b
  3829. this._hl = 0x137e2179
  3830. return this
  3831. }
  3832. function Ch (x, y, z) {
  3833. return z ^ (x & (y ^ z))
  3834. }
  3835. function maj (x, y, z) {
  3836. return (x & y) | (z & (x | y))
  3837. }
  3838. function sigma0 (x, xl) {
  3839. return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
  3840. }
  3841. function sigma1 (x, xl) {
  3842. return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
  3843. }
  3844. function Gamma0 (x, xl) {
  3845. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
  3846. }
  3847. function Gamma0l (x, xl) {
  3848. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
  3849. }
  3850. function Gamma1 (x, xl) {
  3851. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
  3852. }
  3853. function Gamma1l (x, xl) {
  3854. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
  3855. }
  3856. function getCarry (a, b) {
  3857. return (a >>> 0) < (b >>> 0) ? 1 : 0
  3858. }
  3859. Sha512.prototype._update = function (M) {
  3860. var W = this._w
  3861. var ah = this._ah | 0
  3862. var bh = this._bh | 0
  3863. var ch = this._ch | 0
  3864. var dh = this._dh | 0
  3865. var eh = this._eh | 0
  3866. var fh = this._fh | 0
  3867. var gh = this._gh | 0
  3868. var hh = this._hh | 0
  3869. var al = this._al | 0
  3870. var bl = this._bl | 0
  3871. var cl = this._cl | 0
  3872. var dl = this._dl | 0
  3873. var el = this._el | 0
  3874. var fl = this._fl | 0
  3875. var gl = this._gl | 0
  3876. var hl = this._hl | 0
  3877. for (var i = 0; i < 32; i += 2) {
  3878. W[i] = M.readInt32BE(i * 4)
  3879. W[i + 1] = M.readInt32BE(i * 4 + 4)
  3880. }
  3881. for (; i < 160; i += 2) {
  3882. var xh = W[i - 15 * 2]
  3883. var xl = W[i - 15 * 2 + 1]
  3884. var gamma0 = Gamma0(xh, xl)
  3885. var gamma0l = Gamma0l(xl, xh)
  3886. xh = W[i - 2 * 2]
  3887. xl = W[i - 2 * 2 + 1]
  3888. var gamma1 = Gamma1(xh, xl)
  3889. var gamma1l = Gamma1l(xl, xh)
  3890. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  3891. var Wi7h = W[i - 7 * 2]
  3892. var Wi7l = W[i - 7 * 2 + 1]
  3893. var Wi16h = W[i - 16 * 2]
  3894. var Wi16l = W[i - 16 * 2 + 1]
  3895. var Wil = (gamma0l + Wi7l) | 0
  3896. var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0
  3897. Wil = (Wil + gamma1l) | 0
  3898. Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0
  3899. Wil = (Wil + Wi16l) | 0
  3900. Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0
  3901. W[i] = Wih
  3902. W[i + 1] = Wil
  3903. }
  3904. for (var j = 0; j < 160; j += 2) {
  3905. Wih = W[j]
  3906. Wil = W[j + 1]
  3907. var majh = maj(ah, bh, ch)
  3908. var majl = maj(al, bl, cl)
  3909. var sigma0h = sigma0(ah, al)
  3910. var sigma0l = sigma0(al, ah)
  3911. var sigma1h = sigma1(eh, el)
  3912. var sigma1l = sigma1(el, eh)
  3913. // t1 = h + sigma1 + ch + K[j] + W[j]
  3914. var Kih = K[j]
  3915. var Kil = K[j + 1]
  3916. var chh = Ch(eh, fh, gh)
  3917. var chl = Ch(el, fl, gl)
  3918. var t1l = (hl + sigma1l) | 0
  3919. var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0
  3920. t1l = (t1l + chl) | 0
  3921. t1h = (t1h + chh + getCarry(t1l, chl)) | 0
  3922. t1l = (t1l + Kil) | 0
  3923. t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0
  3924. t1l = (t1l + Wil) | 0
  3925. t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0
  3926. // t2 = sigma0 + maj
  3927. var t2l = (sigma0l + majl) | 0
  3928. var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0
  3929. hh = gh
  3930. hl = gl
  3931. gh = fh
  3932. gl = fl
  3933. fh = eh
  3934. fl = el
  3935. el = (dl + t1l) | 0
  3936. eh = (dh + t1h + getCarry(el, dl)) | 0
  3937. dh = ch
  3938. dl = cl
  3939. ch = bh
  3940. cl = bl
  3941. bh = ah
  3942. bl = al
  3943. al = (t1l + t2l) | 0
  3944. ah = (t1h + t2h + getCarry(al, t1l)) | 0
  3945. }
  3946. this._al = (this._al + al) | 0
  3947. this._bl = (this._bl + bl) | 0
  3948. this._cl = (this._cl + cl) | 0
  3949. this._dl = (this._dl + dl) | 0
  3950. this._el = (this._el + el) | 0
  3951. this._fl = (this._fl + fl) | 0
  3952. this._gl = (this._gl + gl) | 0
  3953. this._hl = (this._hl + hl) | 0
  3954. this._ah = (this._ah + ah + getCarry(this._al, al)) | 0
  3955. this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0
  3956. this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0
  3957. this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0
  3958. this._eh = (this._eh + eh + getCarry(this._el, el)) | 0
  3959. this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0
  3960. this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
  3961. this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
  3962. }
  3963. Sha512.prototype._hash = function () {
  3964. var H = Buffer.allocUnsafe(64)
  3965. function writeInt64BE (h, l, offset) {
  3966. H.writeInt32BE(h, offset)
  3967. H.writeInt32BE(l, offset + 4)
  3968. }
  3969. writeInt64BE(this._ah, this._al, 0)
  3970. writeInt64BE(this._bh, this._bl, 8)
  3971. writeInt64BE(this._ch, this._cl, 16)
  3972. writeInt64BE(this._dh, this._dl, 24)
  3973. writeInt64BE(this._eh, this._el, 32)
  3974. writeInt64BE(this._fh, this._fl, 40)
  3975. writeInt64BE(this._gh, this._gl, 48)
  3976. writeInt64BE(this._hh, this._hl, 56)
  3977. return H
  3978. }
  3979. module.exports = Sha512
  3980. },{"./hash":29,"inherits":10,"safe-buffer":28}],37:[function(require,module,exports){
  3981. // Copyright Joyent, Inc. and other Node contributors.
  3982. //
  3983. // Permission is hereby granted, free of charge, to any person obtaining a
  3984. // copy of this software and associated documentation files (the
  3985. // "Software"), to deal in the Software without restriction, including
  3986. // without limitation the rights to use, copy, modify, merge, publish,
  3987. // distribute, sublicense, and/or sell copies of the Software, and to permit
  3988. // persons to whom the Software is furnished to do so, subject to the
  3989. // following conditions:
  3990. //
  3991. // The above copyright notice and this permission notice shall be included
  3992. // in all copies or substantial portions of the Software.
  3993. //
  3994. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  3995. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3996. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  3997. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  3998. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  3999. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  4000. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  4001. 'use strict';
  4002. /*<replacement>*/
  4003. var Buffer = require('safe-buffer').Buffer;
  4004. /*</replacement>*/
  4005. var isEncoding = Buffer.isEncoding || function (encoding) {
  4006. encoding = '' + encoding;
  4007. switch (encoding && encoding.toLowerCase()) {
  4008. 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':
  4009. return true;
  4010. default:
  4011. return false;
  4012. }
  4013. };
  4014. function _normalizeEncoding(enc) {
  4015. if (!enc) return 'utf8';
  4016. var retried;
  4017. while (true) {
  4018. switch (enc) {
  4019. case 'utf8':
  4020. case 'utf-8':
  4021. return 'utf8';
  4022. case 'ucs2':
  4023. case 'ucs-2':
  4024. case 'utf16le':
  4025. case 'utf-16le':
  4026. return 'utf16le';
  4027. case 'latin1':
  4028. case 'binary':
  4029. return 'latin1';
  4030. case 'base64':
  4031. case 'ascii':
  4032. case 'hex':
  4033. return enc;
  4034. default:
  4035. if (retried) return; // undefined
  4036. enc = ('' + enc).toLowerCase();
  4037. retried = true;
  4038. }
  4039. }
  4040. };
  4041. // Do not cache `Buffer.isEncoding` when checking encoding names as some
  4042. // modules monkey-patch it to support additional encodings
  4043. function normalizeEncoding(enc) {
  4044. var nenc = _normalizeEncoding(enc);
  4045. if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
  4046. return nenc || enc;
  4047. }
  4048. // StringDecoder provides an interface for efficiently splitting a series of
  4049. // buffers into a series of JS strings without breaking apart multi-byte
  4050. // characters.
  4051. exports.StringDecoder = StringDecoder;
  4052. function StringDecoder(encoding) {
  4053. this.encoding = normalizeEncoding(encoding);
  4054. var nb;
  4055. switch (this.encoding) {
  4056. case 'utf16le':
  4057. this.text = utf16Text;
  4058. this.end = utf16End;
  4059. nb = 4;
  4060. break;
  4061. case 'utf8':
  4062. this.fillLast = utf8FillLast;
  4063. nb = 4;
  4064. break;
  4065. case 'base64':
  4066. this.text = base64Text;
  4067. this.end = base64End;
  4068. nb = 3;
  4069. break;
  4070. default:
  4071. this.write = simpleWrite;
  4072. this.end = simpleEnd;
  4073. return;
  4074. }
  4075. this.lastNeed = 0;
  4076. this.lastTotal = 0;
  4077. this.lastChar = Buffer.allocUnsafe(nb);
  4078. }
  4079. StringDecoder.prototype.write = function (buf) {
  4080. if (buf.length === 0) return '';
  4081. var r;
  4082. var i;
  4083. if (this.lastNeed) {
  4084. r = this.fillLast(buf);
  4085. if (r === undefined) return '';
  4086. i = this.lastNeed;
  4087. this.lastNeed = 0;
  4088. } else {
  4089. i = 0;
  4090. }
  4091. if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
  4092. return r || '';
  4093. };
  4094. StringDecoder.prototype.end = utf8End;
  4095. // Returns only complete characters in a Buffer
  4096. StringDecoder.prototype.text = utf8Text;
  4097. // Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
  4098. StringDecoder.prototype.fillLast = function (buf) {
  4099. if (this.lastNeed <= buf.length) {
  4100. buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
  4101. return this.lastChar.toString(this.encoding, 0, this.lastTotal);
  4102. }
  4103. buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
  4104. this.lastNeed -= buf.length;
  4105. };
  4106. // Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
  4107. // continuation byte. If an invalid byte is detected, -2 is returned.
  4108. function utf8CheckByte(byte) {
  4109. if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
  4110. return byte >> 6 === 0x02 ? -1 : -2;
  4111. }
  4112. // Checks at most 3 bytes at the end of a Buffer in order to detect an
  4113. // incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
  4114. // needed to complete the UTF-8 character (if applicable) are returned.
  4115. function utf8CheckIncomplete(self, buf, i) {
  4116. var j = buf.length - 1;
  4117. if (j < i) return 0;
  4118. var nb = utf8CheckByte(buf[j]);
  4119. if (nb >= 0) {
  4120. if (nb > 0) self.lastNeed = nb - 1;
  4121. return nb;
  4122. }
  4123. if (--j < i || nb === -2) return 0;
  4124. nb = utf8CheckByte(buf[j]);
  4125. if (nb >= 0) {
  4126. if (nb > 0) self.lastNeed = nb - 2;
  4127. return nb;
  4128. }
  4129. if (--j < i || nb === -2) return 0;
  4130. nb = utf8CheckByte(buf[j]);
  4131. if (nb >= 0) {
  4132. if (nb > 0) {
  4133. if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
  4134. }
  4135. return nb;
  4136. }
  4137. return 0;
  4138. }
  4139. // Validates as many continuation bytes for a multi-byte UTF-8 character as
  4140. // needed or are available. If we see a non-continuation byte where we expect
  4141. // one, we "replace" the validated continuation bytes we've seen so far with
  4142. // a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
  4143. // behavior. The continuation byte check is included three times in the case
  4144. // where all of the continuation bytes for a character exist in the same buffer.
  4145. // It is also done this way as a slight performance increase instead of using a
  4146. // loop.
  4147. function utf8CheckExtraBytes(self, buf, p) {
  4148. if ((buf[0] & 0xC0) !== 0x80) {
  4149. self.lastNeed = 0;
  4150. return '\ufffd';
  4151. }
  4152. if (self.lastNeed > 1 && buf.length > 1) {
  4153. if ((buf[1] & 0xC0) !== 0x80) {
  4154. self.lastNeed = 1;
  4155. return '\ufffd';
  4156. }
  4157. if (self.lastNeed > 2 && buf.length > 2) {
  4158. if ((buf[2] & 0xC0) !== 0x80) {
  4159. self.lastNeed = 2;
  4160. return '\ufffd';
  4161. }
  4162. }
  4163. }
  4164. }
  4165. // Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
  4166. function utf8FillLast(buf) {
  4167. var p = this.lastTotal - this.lastNeed;
  4168. var r = utf8CheckExtraBytes(this, buf, p);
  4169. if (r !== undefined) return r;
  4170. if (this.lastNeed <= buf.length) {
  4171. buf.copy(this.lastChar, p, 0, this.lastNeed);
  4172. return this.lastChar.toString(this.encoding, 0, this.lastTotal);
  4173. }
  4174. buf.copy(this.lastChar, p, 0, buf.length);
  4175. this.lastNeed -= buf.length;
  4176. }
  4177. // Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
  4178. // partial character, the character's bytes are buffered until the required
  4179. // number of bytes are available.
  4180. function utf8Text(buf, i) {
  4181. var total = utf8CheckIncomplete(this, buf, i);
  4182. if (!this.lastNeed) return buf.toString('utf8', i);
  4183. this.lastTotal = total;
  4184. var end = buf.length - (total - this.lastNeed);
  4185. buf.copy(this.lastChar, 0, end);
  4186. return buf.toString('utf8', i, end);
  4187. }
  4188. // For UTF-8, a replacement character is added when ending on a partial
  4189. // character.
  4190. function utf8End(buf) {
  4191. var r = buf && buf.length ? this.write(buf) : '';
  4192. if (this.lastNeed) return r + '\ufffd';
  4193. return r;
  4194. }
  4195. // UTF-16LE typically needs two bytes per character, but even if we have an even
  4196. // number of bytes available, we need to check if we end on a leading/high
  4197. // surrogate. In that case, we need to wait for the next two bytes in order to
  4198. // decode the last character properly.
  4199. function utf16Text(buf, i) {
  4200. if ((buf.length - i) % 2 === 0) {
  4201. var r = buf.toString('utf16le', i);
  4202. if (r) {
  4203. var c = r.charCodeAt(r.length - 1);
  4204. if (c >= 0xD800 && c <= 0xDBFF) {
  4205. this.lastNeed = 2;
  4206. this.lastTotal = 4;
  4207. this.lastChar[0] = buf[buf.length - 2];
  4208. this.lastChar[1] = buf[buf.length - 1];
  4209. return r.slice(0, -1);
  4210. }
  4211. }
  4212. return r;
  4213. }
  4214. this.lastNeed = 1;
  4215. this.lastTotal = 2;
  4216. this.lastChar[0] = buf[buf.length - 1];
  4217. return buf.toString('utf16le', i, buf.length - 1);
  4218. }
  4219. // For UTF-16LE we do not explicitly append special replacement characters if we
  4220. // end on a partial character, we simply let v8 handle that.
  4221. function utf16End(buf) {
  4222. var r = buf && buf.length ? this.write(buf) : '';
  4223. if (this.lastNeed) {
  4224. var end = this.lastTotal - this.lastNeed;
  4225. return r + this.lastChar.toString('utf16le', 0, end);
  4226. }
  4227. return r;
  4228. }
  4229. function base64Text(buf, i) {
  4230. var n = (buf.length - i) % 3;
  4231. if (n === 0) return buf.toString('base64', i);
  4232. this.lastNeed = 3 - n;
  4233. this.lastTotal = 3;
  4234. if (n === 1) {
  4235. this.lastChar[0] = buf[buf.length - 1];
  4236. } else {
  4237. this.lastChar[0] = buf[buf.length - 2];
  4238. this.lastChar[1] = buf[buf.length - 1];
  4239. }
  4240. return buf.toString('base64', i, buf.length - n);
  4241. }
  4242. function base64End(buf) {
  4243. var r = buf && buf.length ? this.write(buf) : '';
  4244. if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
  4245. return r;
  4246. }
  4247. // Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
  4248. function simpleWrite(buf) {
  4249. return buf.toString(this.encoding);
  4250. }
  4251. function simpleEnd(buf) {
  4252. return buf && buf.length ? this.write(buf) : '';
  4253. }
  4254. },{"safe-buffer":28}],38:[function(require,module,exports){
  4255. (function(nacl) {
  4256. 'use strict';
  4257. // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
  4258. // Public domain.
  4259. //
  4260. // Implementation derived from TweetNaCl version 20140427.
  4261. // See for details: http://tweetnacl.cr.yp.to/
  4262. var gf = function(init) {
  4263. var i, r = new Float64Array(16);
  4264. if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
  4265. return r;
  4266. };
  4267. // Pluggable, initialized in high-level API below.
  4268. var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
  4269. var _0 = new Uint8Array(16);
  4270. var _9 = new Uint8Array(32); _9[0] = 9;
  4271. var gf0 = gf(),
  4272. gf1 = gf([1]),
  4273. _121665 = gf([0xdb41, 1]),
  4274. D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
  4275. D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]),
  4276. X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]),
  4277. Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]),
  4278. I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
  4279. function ts64(x, i, h, l) {
  4280. x[i] = (h >> 24) & 0xff;
  4281. x[i+1] = (h >> 16) & 0xff;
  4282. x[i+2] = (h >> 8) & 0xff;
  4283. x[i+3] = h & 0xff;
  4284. x[i+4] = (l >> 24) & 0xff;
  4285. x[i+5] = (l >> 16) & 0xff;
  4286. x[i+6] = (l >> 8) & 0xff;
  4287. x[i+7] = l & 0xff;
  4288. }
  4289. function vn(x, xi, y, yi, n) {
  4290. var i,d = 0;
  4291. for (i = 0; i < n; i++) d |= x[xi+i]^y[yi+i];
  4292. return (1 & ((d - 1) >>> 8)) - 1;
  4293. }
  4294. function crypto_verify_16(x, xi, y, yi) {
  4295. return vn(x,xi,y,yi,16);
  4296. }
  4297. function crypto_verify_32(x, xi, y, yi) {
  4298. return vn(x,xi,y,yi,32);
  4299. }
  4300. function core_salsa20(o, p, k, c) {
  4301. var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
  4302. j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
  4303. j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
  4304. j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
  4305. j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
  4306. j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
  4307. j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
  4308. j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
  4309. j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
  4310. j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
  4311. j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
  4312. j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
  4313. j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
  4314. j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
  4315. j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
  4316. j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
  4317. var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
  4318. x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
  4319. x15 = j15, u;
  4320. for (var i = 0; i < 20; i += 2) {
  4321. u = x0 + x12 | 0;
  4322. x4 ^= u<<7 | u>>>(32-7);
  4323. u = x4 + x0 | 0;
  4324. x8 ^= u<<9 | u>>>(32-9);
  4325. u = x8 + x4 | 0;
  4326. x12 ^= u<<13 | u>>>(32-13);
  4327. u = x12 + x8 | 0;
  4328. x0 ^= u<<18 | u>>>(32-18);
  4329. u = x5 + x1 | 0;
  4330. x9 ^= u<<7 | u>>>(32-7);
  4331. u = x9 + x5 | 0;
  4332. x13 ^= u<<9 | u>>>(32-9);
  4333. u = x13 + x9 | 0;
  4334. x1 ^= u<<13 | u>>>(32-13);
  4335. u = x1 + x13 | 0;
  4336. x5 ^= u<<18 | u>>>(32-18);
  4337. u = x10 + x6 | 0;
  4338. x14 ^= u<<7 | u>>>(32-7);
  4339. u = x14 + x10 | 0;
  4340. x2 ^= u<<9 | u>>>(32-9);
  4341. u = x2 + x14 | 0;
  4342. x6 ^= u<<13 | u>>>(32-13);
  4343. u = x6 + x2 | 0;
  4344. x10 ^= u<<18 | u>>>(32-18);
  4345. u = x15 + x11 | 0;
  4346. x3 ^= u<<7 | u>>>(32-7);
  4347. u = x3 + x15 | 0;
  4348. x7 ^= u<<9 | u>>>(32-9);
  4349. u = x7 + x3 | 0;
  4350. x11 ^= u<<13 | u>>>(32-13);
  4351. u = x11 + x7 | 0;
  4352. x15 ^= u<<18 | u>>>(32-18);
  4353. u = x0 + x3 | 0;
  4354. x1 ^= u<<7 | u>>>(32-7);
  4355. u = x1 + x0 | 0;
  4356. x2 ^= u<<9 | u>>>(32-9);
  4357. u = x2 + x1 | 0;
  4358. x3 ^= u<<13 | u>>>(32-13);
  4359. u = x3 + x2 | 0;
  4360. x0 ^= u<<18 | u>>>(32-18);
  4361. u = x5 + x4 | 0;
  4362. x6 ^= u<<7 | u>>>(32-7);
  4363. u = x6 + x5 | 0;
  4364. x7 ^= u<<9 | u>>>(32-9);
  4365. u = x7 + x6 | 0;
  4366. x4 ^= u<<13 | u>>>(32-13);
  4367. u = x4 + x7 | 0;
  4368. x5 ^= u<<18 | u>>>(32-18);
  4369. u = x10 + x9 | 0;
  4370. x11 ^= u<<7 | u>>>(32-7);
  4371. u = x11 + x10 | 0;
  4372. x8 ^= u<<9 | u>>>(32-9);
  4373. u = x8 + x11 | 0;
  4374. x9 ^= u<<13 | u>>>(32-13);
  4375. u = x9 + x8 | 0;
  4376. x10 ^= u<<18 | u>>>(32-18);
  4377. u = x15 + x14 | 0;
  4378. x12 ^= u<<7 | u>>>(32-7);
  4379. u = x12 + x15 | 0;
  4380. x13 ^= u<<9 | u>>>(32-9);
  4381. u = x13 + x12 | 0;
  4382. x14 ^= u<<13 | u>>>(32-13);
  4383. u = x14 + x13 | 0;
  4384. x15 ^= u<<18 | u>>>(32-18);
  4385. }
  4386. x0 = x0 + j0 | 0;
  4387. x1 = x1 + j1 | 0;
  4388. x2 = x2 + j2 | 0;
  4389. x3 = x3 + j3 | 0;
  4390. x4 = x4 + j4 | 0;
  4391. x5 = x5 + j5 | 0;
  4392. x6 = x6 + j6 | 0;
  4393. x7 = x7 + j7 | 0;
  4394. x8 = x8 + j8 | 0;
  4395. x9 = x9 + j9 | 0;
  4396. x10 = x10 + j10 | 0;
  4397. x11 = x11 + j11 | 0;
  4398. x12 = x12 + j12 | 0;
  4399. x13 = x13 + j13 | 0;
  4400. x14 = x14 + j14 | 0;
  4401. x15 = x15 + j15 | 0;
  4402. o[ 0] = x0 >>> 0 & 0xff;
  4403. o[ 1] = x0 >>> 8 & 0xff;
  4404. o[ 2] = x0 >>> 16 & 0xff;
  4405. o[ 3] = x0 >>> 24 & 0xff;
  4406. o[ 4] = x1 >>> 0 & 0xff;
  4407. o[ 5] = x1 >>> 8 & 0xff;
  4408. o[ 6] = x1 >>> 16 & 0xff;
  4409. o[ 7] = x1 >>> 24 & 0xff;
  4410. o[ 8] = x2 >>> 0 & 0xff;
  4411. o[ 9] = x2 >>> 8 & 0xff;
  4412. o[10] = x2 >>> 16 & 0xff;
  4413. o[11] = x2 >>> 24 & 0xff;
  4414. o[12] = x3 >>> 0 & 0xff;
  4415. o[13] = x3 >>> 8 & 0xff;
  4416. o[14] = x3 >>> 16 & 0xff;
  4417. o[15] = x3 >>> 24 & 0xff;
  4418. o[16] = x4 >>> 0 & 0xff;
  4419. o[17] = x4 >>> 8 & 0xff;
  4420. o[18] = x4 >>> 16 & 0xff;
  4421. o[19] = x4 >>> 24 & 0xff;
  4422. o[20] = x5 >>> 0 & 0xff;
  4423. o[21] = x5 >>> 8 & 0xff;
  4424. o[22] = x5 >>> 16 & 0xff;
  4425. o[23] = x5 >>> 24 & 0xff;
  4426. o[24] = x6 >>> 0 & 0xff;
  4427. o[25] = x6 >>> 8 & 0xff;
  4428. o[26] = x6 >>> 16 & 0xff;
  4429. o[27] = x6 >>> 24 & 0xff;
  4430. o[28] = x7 >>> 0 & 0xff;
  4431. o[29] = x7 >>> 8 & 0xff;
  4432. o[30] = x7 >>> 16 & 0xff;
  4433. o[31] = x7 >>> 24 & 0xff;
  4434. o[32] = x8 >>> 0 & 0xff;
  4435. o[33] = x8 >>> 8 & 0xff;
  4436. o[34] = x8 >>> 16 & 0xff;
  4437. o[35] = x8 >>> 24 & 0xff;
  4438. o[36] = x9 >>> 0 & 0xff;
  4439. o[37] = x9 >>> 8 & 0xff;
  4440. o[38] = x9 >>> 16 & 0xff;
  4441. o[39] = x9 >>> 24 & 0xff;
  4442. o[40] = x10 >>> 0 & 0xff;
  4443. o[41] = x10 >>> 8 & 0xff;
  4444. o[42] = x10 >>> 16 & 0xff;
  4445. o[43] = x10 >>> 24 & 0xff;
  4446. o[44] = x11 >>> 0 & 0xff;
  4447. o[45] = x11 >>> 8 & 0xff;
  4448. o[46] = x11 >>> 16 & 0xff;
  4449. o[47] = x11 >>> 24 & 0xff;
  4450. o[48] = x12 >>> 0 & 0xff;
  4451. o[49] = x12 >>> 8 & 0xff;
  4452. o[50] = x12 >>> 16 & 0xff;
  4453. o[51] = x12 >>> 24 & 0xff;
  4454. o[52] = x13 >>> 0 & 0xff;
  4455. o[53] = x13 >>> 8 & 0xff;
  4456. o[54] = x13 >>> 16 & 0xff;
  4457. o[55] = x13 >>> 24 & 0xff;
  4458. o[56] = x14 >>> 0 & 0xff;
  4459. o[57] = x14 >>> 8 & 0xff;
  4460. o[58] = x14 >>> 16 & 0xff;
  4461. o[59] = x14 >>> 24 & 0xff;
  4462. o[60] = x15 >>> 0 & 0xff;
  4463. o[61] = x15 >>> 8 & 0xff;
  4464. o[62] = x15 >>> 16 & 0xff;
  4465. o[63] = x15 >>> 24 & 0xff;
  4466. }
  4467. function core_hsalsa20(o,p,k,c) {
  4468. var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
  4469. j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
  4470. j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
  4471. j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
  4472. j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
  4473. j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
  4474. j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
  4475. j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
  4476. j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
  4477. j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
  4478. j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
  4479. j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
  4480. j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
  4481. j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
  4482. j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
  4483. j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
  4484. var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
  4485. x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
  4486. x15 = j15, u;
  4487. for (var i = 0; i < 20; i += 2) {
  4488. u = x0 + x12 | 0;
  4489. x4 ^= u<<7 | u>>>(32-7);
  4490. u = x4 + x0 | 0;
  4491. x8 ^= u<<9 | u>>>(32-9);
  4492. u = x8 + x4 | 0;
  4493. x12 ^= u<<13 | u>>>(32-13);
  4494. u = x12 + x8 | 0;
  4495. x0 ^= u<<18 | u>>>(32-18);
  4496. u = x5 + x1 | 0;
  4497. x9 ^= u<<7 | u>>>(32-7);
  4498. u = x9 + x5 | 0;
  4499. x13 ^= u<<9 | u>>>(32-9);
  4500. u = x13 + x9 | 0;
  4501. x1 ^= u<<13 | u>>>(32-13);
  4502. u = x1 + x13 | 0;
  4503. x5 ^= u<<18 | u>>>(32-18);
  4504. u = x10 + x6 | 0;
  4505. x14 ^= u<<7 | u>>>(32-7);
  4506. u = x14 + x10 | 0;
  4507. x2 ^= u<<9 | u>>>(32-9);
  4508. u = x2 + x14 | 0;
  4509. x6 ^= u<<13 | u>>>(32-13);
  4510. u = x6 + x2 | 0;
  4511. x10 ^= u<<18 | u>>>(32-18);
  4512. u = x15 + x11 | 0;
  4513. x3 ^= u<<7 | u>>>(32-7);
  4514. u = x3 + x15 | 0;
  4515. x7 ^= u<<9 | u>>>(32-9);
  4516. u = x7 + x3 | 0;
  4517. x11 ^= u<<13 | u>>>(32-13);
  4518. u = x11 + x7 | 0;
  4519. x15 ^= u<<18 | u>>>(32-18);
  4520. u = x0 + x3 | 0;
  4521. x1 ^= u<<7 | u>>>(32-7);
  4522. u = x1 + x0 | 0;
  4523. x2 ^= u<<9 | u>>>(32-9);
  4524. u = x2 + x1 | 0;
  4525. x3 ^= u<<13 | u>>>(32-13);
  4526. u = x3 + x2 | 0;
  4527. x0 ^= u<<18 | u>>>(32-18);
  4528. u = x5 + x4 | 0;
  4529. x6 ^= u<<7 | u>>>(32-7);
  4530. u = x6 + x5 | 0;
  4531. x7 ^= u<<9 | u>>>(32-9);
  4532. u = x7 + x6 | 0;
  4533. x4 ^= u<<13 | u>>>(32-13);
  4534. u = x4 + x7 | 0;
  4535. x5 ^= u<<18 | u>>>(32-18);
  4536. u = x10 + x9 | 0;
  4537. x11 ^= u<<7 | u>>>(32-7);
  4538. u = x11 + x10 | 0;
  4539. x8 ^= u<<9 | u>>>(32-9);
  4540. u = x8 + x11 | 0;
  4541. x9 ^= u<<13 | u>>>(32-13);
  4542. u = x9 + x8 | 0;
  4543. x10 ^= u<<18 | u>>>(32-18);
  4544. u = x15 + x14 | 0;
  4545. x12 ^= u<<7 | u>>>(32-7);
  4546. u = x12 + x15 | 0;
  4547. x13 ^= u<<9 | u>>>(32-9);
  4548. u = x13 + x12 | 0;
  4549. x14 ^= u<<13 | u>>>(32-13);
  4550. u = x14 + x13 | 0;
  4551. x15 ^= u<<18 | u>>>(32-18);
  4552. }
  4553. o[ 0] = x0 >>> 0 & 0xff;
  4554. o[ 1] = x0 >>> 8 & 0xff;
  4555. o[ 2] = x0 >>> 16 & 0xff;
  4556. o[ 3] = x0 >>> 24 & 0xff;
  4557. o[ 4] = x5 >>> 0 & 0xff;
  4558. o[ 5] = x5 >>> 8 & 0xff;
  4559. o[ 6] = x5 >>> 16 & 0xff;
  4560. o[ 7] = x5 >>> 24 & 0xff;
  4561. o[ 8] = x10 >>> 0 & 0xff;
  4562. o[ 9] = x10 >>> 8 & 0xff;
  4563. o[10] = x10 >>> 16 & 0xff;
  4564. o[11] = x10 >>> 24 & 0xff;
  4565. o[12] = x15 >>> 0 & 0xff;
  4566. o[13] = x15 >>> 8 & 0xff;
  4567. o[14] = x15 >>> 16 & 0xff;
  4568. o[15] = x15 >>> 24 & 0xff;
  4569. o[16] = x6 >>> 0 & 0xff;
  4570. o[17] = x6 >>> 8 & 0xff;
  4571. o[18] = x6 >>> 16 & 0xff;
  4572. o[19] = x6 >>> 24 & 0xff;
  4573. o[20] = x7 >>> 0 & 0xff;
  4574. o[21] = x7 >>> 8 & 0xff;
  4575. o[22] = x7 >>> 16 & 0xff;
  4576. o[23] = x7 >>> 24 & 0xff;
  4577. o[24] = x8 >>> 0 & 0xff;
  4578. o[25] = x8 >>> 8 & 0xff;
  4579. o[26] = x8 >>> 16 & 0xff;
  4580. o[27] = x8 >>> 24 & 0xff;
  4581. o[28] = x9 >>> 0 & 0xff;
  4582. o[29] = x9 >>> 8 & 0xff;
  4583. o[30] = x9 >>> 16 & 0xff;
  4584. o[31] = x9 >>> 24 & 0xff;
  4585. }
  4586. function crypto_core_salsa20(out,inp,k,c) {
  4587. core_salsa20(out,inp,k,c);
  4588. }
  4589. function crypto_core_hsalsa20(out,inp,k,c) {
  4590. core_hsalsa20(out,inp,k,c);
  4591. }
  4592. var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
  4593. // "expand 32-byte k"
  4594. function crypto_stream_salsa20_xor(c,cpos,m,mpos,b,n,k) {
  4595. var z = new Uint8Array(16), x = new Uint8Array(64);
  4596. var u, i;
  4597. for (i = 0; i < 16; i++) z[i] = 0;
  4598. for (i = 0; i < 8; i++) z[i] = n[i];
  4599. while (b >= 64) {
  4600. crypto_core_salsa20(x,z,k,sigma);
  4601. for (i = 0; i < 64; i++) c[cpos+i] = m[mpos+i] ^ x[i];
  4602. u = 1;
  4603. for (i = 8; i < 16; i++) {
  4604. u = u + (z[i] & 0xff) | 0;
  4605. z[i] = u & 0xff;
  4606. u >>>= 8;
  4607. }
  4608. b -= 64;
  4609. cpos += 64;
  4610. mpos += 64;
  4611. }
  4612. if (b > 0) {
  4613. crypto_core_salsa20(x,z,k,sigma);
  4614. for (i = 0; i < b; i++) c[cpos+i] = m[mpos+i] ^ x[i];
  4615. }
  4616. return 0;
  4617. }
  4618. function crypto_stream_salsa20(c,cpos,b,n,k) {
  4619. var z = new Uint8Array(16), x = new Uint8Array(64);
  4620. var u, i;
  4621. for (i = 0; i < 16; i++) z[i] = 0;
  4622. for (i = 0; i < 8; i++) z[i] = n[i];
  4623. while (b >= 64) {
  4624. crypto_core_salsa20(x,z,k,sigma);
  4625. for (i = 0; i < 64; i++) c[cpos+i] = x[i];
  4626. u = 1;
  4627. for (i = 8; i < 16; i++) {
  4628. u = u + (z[i] & 0xff) | 0;
  4629. z[i] = u & 0xff;
  4630. u >>>= 8;
  4631. }
  4632. b -= 64;
  4633. cpos += 64;
  4634. }
  4635. if (b > 0) {
  4636. crypto_core_salsa20(x,z,k,sigma);
  4637. for (i = 0; i < b; i++) c[cpos+i] = x[i];
  4638. }
  4639. return 0;
  4640. }
  4641. function crypto_stream(c,cpos,d,n,k) {
  4642. var s = new Uint8Array(32);
  4643. crypto_core_hsalsa20(s,n,k,sigma);
  4644. var sn = new Uint8Array(8);
  4645. for (var i = 0; i < 8; i++) sn[i] = n[i+16];
  4646. return crypto_stream_salsa20(c,cpos,d,sn,s);
  4647. }
  4648. function crypto_stream_xor(c,cpos,m,mpos,d,n,k) {
  4649. var s = new Uint8Array(32);
  4650. crypto_core_hsalsa20(s,n,k,sigma);
  4651. var sn = new Uint8Array(8);
  4652. for (var i = 0; i < 8; i++) sn[i] = n[i+16];
  4653. return crypto_stream_salsa20_xor(c,cpos,m,mpos,d,sn,s);
  4654. }
  4655. /*
  4656. * Port of Andrew Moon's Poly1305-donna-16. Public domain.
  4657. * https://github.com/floodyberry/poly1305-donna
  4658. */
  4659. var poly1305 = function(key) {
  4660. this.buffer = new Uint8Array(16);
  4661. this.r = new Uint16Array(10);
  4662. this.h = new Uint16Array(10);
  4663. this.pad = new Uint16Array(8);
  4664. this.leftover = 0;
  4665. this.fin = 0;
  4666. var t0, t1, t2, t3, t4, t5, t6, t7;
  4667. t0 = key[ 0] & 0xff | (key[ 1] & 0xff) << 8; this.r[0] = ( t0 ) & 0x1fff;
  4668. t1 = key[ 2] & 0xff | (key[ 3] & 0xff) << 8; this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
  4669. t2 = key[ 4] & 0xff | (key[ 5] & 0xff) << 8; this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
  4670. t3 = key[ 6] & 0xff | (key[ 7] & 0xff) << 8; this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
  4671. t4 = key[ 8] & 0xff | (key[ 9] & 0xff) << 8; this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
  4672. this.r[5] = ((t4 >>> 1)) & 0x1ffe;
  4673. t5 = key[10] & 0xff | (key[11] & 0xff) << 8; this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
  4674. t6 = key[12] & 0xff | (key[13] & 0xff) << 8; this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
  4675. t7 = key[14] & 0xff | (key[15] & 0xff) << 8; this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
  4676. this.r[9] = ((t7 >>> 5)) & 0x007f;
  4677. this.pad[0] = key[16] & 0xff | (key[17] & 0xff) << 8;
  4678. this.pad[1] = key[18] & 0xff | (key[19] & 0xff) << 8;
  4679. this.pad[2] = key[20] & 0xff | (key[21] & 0xff) << 8;
  4680. this.pad[3] = key[22] & 0xff | (key[23] & 0xff) << 8;
  4681. this.pad[4] = key[24] & 0xff | (key[25] & 0xff) << 8;
  4682. this.pad[5] = key[26] & 0xff | (key[27] & 0xff) << 8;
  4683. this.pad[6] = key[28] & 0xff | (key[29] & 0xff) << 8;
  4684. this.pad[7] = key[30] & 0xff | (key[31] & 0xff) << 8;
  4685. };
  4686. poly1305.prototype.blocks = function(m, mpos, bytes) {
  4687. var hibit = this.fin ? 0 : (1 << 11);
  4688. var t0, t1, t2, t3, t4, t5, t6, t7, c;
  4689. var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
  4690. var h0 = this.h[0],
  4691. h1 = this.h[1],
  4692. h2 = this.h[2],
  4693. h3 = this.h[3],
  4694. h4 = this.h[4],
  4695. h5 = this.h[5],
  4696. h6 = this.h[6],
  4697. h7 = this.h[7],
  4698. h8 = this.h[8],
  4699. h9 = this.h[9];
  4700. var r0 = this.r[0],
  4701. r1 = this.r[1],
  4702. r2 = this.r[2],
  4703. r3 = this.r[3],
  4704. r4 = this.r[4],
  4705. r5 = this.r[5],
  4706. r6 = this.r[6],
  4707. r7 = this.r[7],
  4708. r8 = this.r[8],
  4709. r9 = this.r[9];
  4710. while (bytes >= 16) {
  4711. t0 = m[mpos+ 0] & 0xff | (m[mpos+ 1] & 0xff) << 8; h0 += ( t0 ) & 0x1fff;
  4712. t1 = m[mpos+ 2] & 0xff | (m[mpos+ 3] & 0xff) << 8; h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
  4713. t2 = m[mpos+ 4] & 0xff | (m[mpos+ 5] & 0xff) << 8; h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
  4714. t3 = m[mpos+ 6] & 0xff | (m[mpos+ 7] & 0xff) << 8; h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
  4715. t4 = m[mpos+ 8] & 0xff | (m[mpos+ 9] & 0xff) << 8; h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
  4716. h5 += ((t4 >>> 1)) & 0x1fff;
  4717. t5 = m[mpos+10] & 0xff | (m[mpos+11] & 0xff) << 8; h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
  4718. t6 = m[mpos+12] & 0xff | (m[mpos+13] & 0xff) << 8; h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
  4719. t7 = m[mpos+14] & 0xff | (m[mpos+15] & 0xff) << 8; h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
  4720. h9 += ((t7 >>> 5)) | hibit;
  4721. c = 0;
  4722. d0 = c;
  4723. d0 += h0 * r0;
  4724. d0 += h1 * (5 * r9);
  4725. d0 += h2 * (5 * r8);
  4726. d0 += h3 * (5 * r7);
  4727. d0 += h4 * (5 * r6);
  4728. c = (d0 >>> 13); d0 &= 0x1fff;
  4729. d0 += h5 * (5 * r5);
  4730. d0 += h6 * (5 * r4);
  4731. d0 += h7 * (5 * r3);
  4732. d0 += h8 * (5 * r2);
  4733. d0 += h9 * (5 * r1);
  4734. c += (d0 >>> 13); d0 &= 0x1fff;
  4735. d1 = c;
  4736. d1 += h0 * r1;
  4737. d1 += h1 * r0;
  4738. d1 += h2 * (5 * r9);
  4739. d1 += h3 * (5 * r8);
  4740. d1 += h4 * (5 * r7);
  4741. c = (d1 >>> 13); d1 &= 0x1fff;
  4742. d1 += h5 * (5 * r6);
  4743. d1 += h6 * (5 * r5);
  4744. d1 += h7 * (5 * r4);
  4745. d1 += h8 * (5 * r3);
  4746. d1 += h9 * (5 * r2);
  4747. c += (d1 >>> 13); d1 &= 0x1fff;
  4748. d2 = c;
  4749. d2 += h0 * r2;
  4750. d2 += h1 * r1;
  4751. d2 += h2 * r0;
  4752. d2 += h3 * (5 * r9);
  4753. d2 += h4 * (5 * r8);
  4754. c = (d2 >>> 13); d2 &= 0x1fff;
  4755. d2 += h5 * (5 * r7);
  4756. d2 += h6 * (5 * r6);
  4757. d2 += h7 * (5 * r5);
  4758. d2 += h8 * (5 * r4);
  4759. d2 += h9 * (5 * r3);
  4760. c += (d2 >>> 13); d2 &= 0x1fff;
  4761. d3 = c;
  4762. d3 += h0 * r3;
  4763. d3 += h1 * r2;
  4764. d3 += h2 * r1;
  4765. d3 += h3 * r0;
  4766. d3 += h4 * (5 * r9);
  4767. c = (d3 >>> 13); d3 &= 0x1fff;
  4768. d3 += h5 * (5 * r8);
  4769. d3 += h6 * (5 * r7);
  4770. d3 += h7 * (5 * r6);
  4771. d3 += h8 * (5 * r5);
  4772. d3 += h9 * (5 * r4);
  4773. c += (d3 >>> 13); d3 &= 0x1fff;
  4774. d4 = c;
  4775. d4 += h0 * r4;
  4776. d4 += h1 * r3;
  4777. d4 += h2 * r2;
  4778. d4 += h3 * r1;
  4779. d4 += h4 * r0;
  4780. c = (d4 >>> 13); d4 &= 0x1fff;
  4781. d4 += h5 * (5 * r9);
  4782. d4 += h6 * (5 * r8);
  4783. d4 += h7 * (5 * r7);
  4784. d4 += h8 * (5 * r6);
  4785. d4 += h9 * (5 * r5);
  4786. c += (d4 >>> 13); d4 &= 0x1fff;
  4787. d5 = c;
  4788. d5 += h0 * r5;
  4789. d5 += h1 * r4;
  4790. d5 += h2 * r3;
  4791. d5 += h3 * r2;
  4792. d5 += h4 * r1;
  4793. c = (d5 >>> 13); d5 &= 0x1fff;
  4794. d5 += h5 * r0;
  4795. d5 += h6 * (5 * r9);
  4796. d5 += h7 * (5 * r8);
  4797. d5 += h8 * (5 * r7);
  4798. d5 += h9 * (5 * r6);
  4799. c += (d5 >>> 13); d5 &= 0x1fff;
  4800. d6 = c;
  4801. d6 += h0 * r6;
  4802. d6 += h1 * r5;
  4803. d6 += h2 * r4;
  4804. d6 += h3 * r3;
  4805. d6 += h4 * r2;
  4806. c = (d6 >>> 13); d6 &= 0x1fff;
  4807. d6 += h5 * r1;
  4808. d6 += h6 * r0;
  4809. d6 += h7 * (5 * r9);
  4810. d6 += h8 * (5 * r8);
  4811. d6 += h9 * (5 * r7);
  4812. c += (d6 >>> 13); d6 &= 0x1fff;
  4813. d7 = c;
  4814. d7 += h0 * r7;
  4815. d7 += h1 * r6;
  4816. d7 += h2 * r5;
  4817. d7 += h3 * r4;
  4818. d7 += h4 * r3;
  4819. c = (d7 >>> 13); d7 &= 0x1fff;
  4820. d7 += h5 * r2;
  4821. d7 += h6 * r1;
  4822. d7 += h7 * r0;
  4823. d7 += h8 * (5 * r9);
  4824. d7 += h9 * (5 * r8);
  4825. c += (d7 >>> 13); d7 &= 0x1fff;
  4826. d8 = c;
  4827. d8 += h0 * r8;
  4828. d8 += h1 * r7;
  4829. d8 += h2 * r6;
  4830. d8 += h3 * r5;
  4831. d8 += h4 * r4;
  4832. c = (d8 >>> 13); d8 &= 0x1fff;
  4833. d8 += h5 * r3;
  4834. d8 += h6 * r2;
  4835. d8 += h7 * r1;
  4836. d8 += h8 * r0;
  4837. d8 += h9 * (5 * r9);
  4838. c += (d8 >>> 13); d8 &= 0x1fff;
  4839. d9 = c;
  4840. d9 += h0 * r9;
  4841. d9 += h1 * r8;
  4842. d9 += h2 * r7;
  4843. d9 += h3 * r6;
  4844. d9 += h4 * r5;
  4845. c = (d9 >>> 13); d9 &= 0x1fff;
  4846. d9 += h5 * r4;
  4847. d9 += h6 * r3;
  4848. d9 += h7 * r2;
  4849. d9 += h8 * r1;
  4850. d9 += h9 * r0;
  4851. c += (d9 >>> 13); d9 &= 0x1fff;
  4852. c = (((c << 2) + c)) | 0;
  4853. c = (c + d0) | 0;
  4854. d0 = c & 0x1fff;
  4855. c = (c >>> 13);
  4856. d1 += c;
  4857. h0 = d0;
  4858. h1 = d1;
  4859. h2 = d2;
  4860. h3 = d3;
  4861. h4 = d4;
  4862. h5 = d5;
  4863. h6 = d6;
  4864. h7 = d7;
  4865. h8 = d8;
  4866. h9 = d9;
  4867. mpos += 16;
  4868. bytes -= 16;
  4869. }
  4870. this.h[0] = h0;
  4871. this.h[1] = h1;
  4872. this.h[2] = h2;
  4873. this.h[3] = h3;
  4874. this.h[4] = h4;
  4875. this.h[5] = h5;
  4876. this.h[6] = h6;
  4877. this.h[7] = h7;
  4878. this.h[8] = h8;
  4879. this.h[9] = h9;
  4880. };
  4881. poly1305.prototype.finish = function(mac, macpos) {
  4882. var g = new Uint16Array(10);
  4883. var c, mask, f, i;
  4884. if (this.leftover) {
  4885. i = this.leftover;
  4886. this.buffer[i++] = 1;
  4887. for (; i < 16; i++) this.buffer[i] = 0;
  4888. this.fin = 1;
  4889. this.blocks(this.buffer, 0, 16);
  4890. }
  4891. c = this.h[1] >>> 13;
  4892. this.h[1] &= 0x1fff;
  4893. for (i = 2; i < 10; i++) {
  4894. this.h[i] += c;
  4895. c = this.h[i] >>> 13;
  4896. this.h[i] &= 0x1fff;
  4897. }
  4898. this.h[0] += (c * 5);
  4899. c = this.h[0] >>> 13;
  4900. this.h[0] &= 0x1fff;
  4901. this.h[1] += c;
  4902. c = this.h[1] >>> 13;
  4903. this.h[1] &= 0x1fff;
  4904. this.h[2] += c;
  4905. g[0] = this.h[0] + 5;
  4906. c = g[0] >>> 13;
  4907. g[0] &= 0x1fff;
  4908. for (i = 1; i < 10; i++) {
  4909. g[i] = this.h[i] + c;
  4910. c = g[i] >>> 13;
  4911. g[i] &= 0x1fff;
  4912. }
  4913. g[9] -= (1 << 13);
  4914. mask = (c ^ 1) - 1;
  4915. for (i = 0; i < 10; i++) g[i] &= mask;
  4916. mask = ~mask;
  4917. for (i = 0; i < 10; i++) this.h[i] = (this.h[i] & mask) | g[i];
  4918. this.h[0] = ((this.h[0] ) | (this.h[1] << 13) ) & 0xffff;
  4919. this.h[1] = ((this.h[1] >>> 3) | (this.h[2] << 10) ) & 0xffff;
  4920. this.h[2] = ((this.h[2] >>> 6) | (this.h[3] << 7) ) & 0xffff;
  4921. this.h[3] = ((this.h[3] >>> 9) | (this.h[4] << 4) ) & 0xffff;
  4922. this.h[4] = ((this.h[4] >>> 12) | (this.h[5] << 1) | (this.h[6] << 14)) & 0xffff;
  4923. this.h[5] = ((this.h[6] >>> 2) | (this.h[7] << 11) ) & 0xffff;
  4924. this.h[6] = ((this.h[7] >>> 5) | (this.h[8] << 8) ) & 0xffff;
  4925. this.h[7] = ((this.h[8] >>> 8) | (this.h[9] << 5) ) & 0xffff;
  4926. f = this.h[0] + this.pad[0];
  4927. this.h[0] = f & 0xffff;
  4928. for (i = 1; i < 8; i++) {
  4929. f = (((this.h[i] + this.pad[i]) | 0) + (f >>> 16)) | 0;
  4930. this.h[i] = f & 0xffff;
  4931. }
  4932. mac[macpos+ 0] = (this.h[0] >>> 0) & 0xff;
  4933. mac[macpos+ 1] = (this.h[0] >>> 8) & 0xff;
  4934. mac[macpos+ 2] = (this.h[1] >>> 0) & 0xff;
  4935. mac[macpos+ 3] = (this.h[1] >>> 8) & 0xff;
  4936. mac[macpos+ 4] = (this.h[2] >>> 0) & 0xff;
  4937. mac[macpos+ 5] = (this.h[2] >>> 8) & 0xff;
  4938. mac[macpos+ 6] = (this.h[3] >>> 0) & 0xff;
  4939. mac[macpos+ 7] = (this.h[3] >>> 8) & 0xff;
  4940. mac[macpos+ 8] = (this.h[4] >>> 0) & 0xff;
  4941. mac[macpos+ 9] = (this.h[4] >>> 8) & 0xff;
  4942. mac[macpos+10] = (this.h[5] >>> 0) & 0xff;
  4943. mac[macpos+11] = (this.h[5] >>> 8) & 0xff;
  4944. mac[macpos+12] = (this.h[6] >>> 0) & 0xff;
  4945. mac[macpos+13] = (this.h[6] >>> 8) & 0xff;
  4946. mac[macpos+14] = (this.h[7] >>> 0) & 0xff;
  4947. mac[macpos+15] = (this.h[7] >>> 8) & 0xff;
  4948. };
  4949. poly1305.prototype.update = function(m, mpos, bytes) {
  4950. var i, want;
  4951. if (this.leftover) {
  4952. want = (16 - this.leftover);
  4953. if (want > bytes)
  4954. want = bytes;
  4955. for (i = 0; i < want; i++)
  4956. this.buffer[this.leftover + i] = m[mpos+i];
  4957. bytes -= want;
  4958. mpos += want;
  4959. this.leftover += want;
  4960. if (this.leftover < 16)
  4961. return;
  4962. this.blocks(this.buffer, 0, 16);
  4963. this.leftover = 0;
  4964. }
  4965. if (bytes >= 16) {
  4966. want = bytes - (bytes % 16);
  4967. this.blocks(m, mpos, want);
  4968. mpos += want;
  4969. bytes -= want;
  4970. }
  4971. if (bytes) {
  4972. for (i = 0; i < bytes; i++)
  4973. this.buffer[this.leftover + i] = m[mpos+i];
  4974. this.leftover += bytes;
  4975. }
  4976. };
  4977. function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
  4978. var s = new poly1305(k);
  4979. s.update(m, mpos, n);
  4980. s.finish(out, outpos);
  4981. return 0;
  4982. }
  4983. function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
  4984. var x = new Uint8Array(16);
  4985. crypto_onetimeauth(x,0,m,mpos,n,k);
  4986. return crypto_verify_16(h,hpos,x,0);
  4987. }
  4988. function crypto_secretbox(c,m,d,n,k) {
  4989. var i;
  4990. if (d < 32) return -1;
  4991. crypto_stream_xor(c,0,m,0,d,n,k);
  4992. crypto_onetimeauth(c, 16, c, 32, d - 32, c);
  4993. for (i = 0; i < 16; i++) c[i] = 0;
  4994. return 0;
  4995. }
  4996. function crypto_secretbox_open(m,c,d,n,k) {
  4997. var i;
  4998. var x = new Uint8Array(32);
  4999. if (d < 32) return -1;
  5000. crypto_stream(x,0,32,n,k);
  5001. if (crypto_onetimeauth_verify(c, 16,c, 32,d - 32,x) !== 0) return -1;
  5002. crypto_stream_xor(m,0,c,0,d,n,k);
  5003. for (i = 0; i < 32; i++) m[i] = 0;
  5004. return 0;
  5005. }
  5006. function set25519(r, a) {
  5007. var i;
  5008. for (i = 0; i < 16; i++) r[i] = a[i]|0;
  5009. }
  5010. function car25519(o) {
  5011. var i, v, c = 1;
  5012. for (i = 0; i < 16; i++) {
  5013. v = o[i] + c + 65535;
  5014. c = Math.floor(v / 65536);
  5015. o[i] = v - c * 65536;
  5016. }
  5017. o[0] += c-1 + 37 * (c-1);
  5018. }
  5019. function sel25519(p, q, b) {
  5020. var t, c = ~(b-1);
  5021. for (var i = 0; i < 16; i++) {
  5022. t = c & (p[i] ^ q[i]);
  5023. p[i] ^= t;
  5024. q[i] ^= t;
  5025. }
  5026. }
  5027. function pack25519(o, n) {
  5028. var i, j, b;
  5029. var m = gf(), t = gf();
  5030. for (i = 0; i < 16; i++) t[i] = n[i];
  5031. car25519(t);
  5032. car25519(t);
  5033. car25519(t);
  5034. for (j = 0; j < 2; j++) {
  5035. m[0] = t[0] - 0xffed;
  5036. for (i = 1; i < 15; i++) {
  5037. m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
  5038. m[i-1] &= 0xffff;
  5039. }
  5040. m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
  5041. b = (m[15]>>16) & 1;
  5042. m[14] &= 0xffff;
  5043. sel25519(t, m, 1-b);
  5044. }
  5045. for (i = 0; i < 16; i++) {
  5046. o[2*i] = t[i] & 0xff;
  5047. o[2*i+1] = t[i]>>8;
  5048. }
  5049. }
  5050. function neq25519(a, b) {
  5051. var c = new Uint8Array(32), d = new Uint8Array(32);
  5052. pack25519(c, a);
  5053. pack25519(d, b);
  5054. return crypto_verify_32(c, 0, d, 0);
  5055. }
  5056. function par25519(a) {
  5057. var d = new Uint8Array(32);
  5058. pack25519(d, a);
  5059. return d[0] & 1;
  5060. }
  5061. function unpack25519(o, n) {
  5062. var i;
  5063. for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
  5064. o[15] &= 0x7fff;
  5065. }
  5066. function A(o, a, b) {
  5067. for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
  5068. }
  5069. function Z(o, a, b) {
  5070. for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
  5071. }
  5072. function M(o, a, b) {
  5073. var v, c,
  5074. t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
  5075. t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
  5076. t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
  5077. t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
  5078. b0 = b[0],
  5079. b1 = b[1],
  5080. b2 = b[2],
  5081. b3 = b[3],
  5082. b4 = b[4],
  5083. b5 = b[5],
  5084. b6 = b[6],
  5085. b7 = b[7],
  5086. b8 = b[8],
  5087. b9 = b[9],
  5088. b10 = b[10],
  5089. b11 = b[11],
  5090. b12 = b[12],
  5091. b13 = b[13],
  5092. b14 = b[14],
  5093. b15 = b[15];
  5094. v = a[0];
  5095. t0 += v * b0;
  5096. t1 += v * b1;
  5097. t2 += v * b2;
  5098. t3 += v * b3;
  5099. t4 += v * b4;
  5100. t5 += v * b5;
  5101. t6 += v * b6;
  5102. t7 += v * b7;
  5103. t8 += v * b8;
  5104. t9 += v * b9;
  5105. t10 += v * b10;
  5106. t11 += v * b11;
  5107. t12 += v * b12;
  5108. t13 += v * b13;
  5109. t14 += v * b14;
  5110. t15 += v * b15;
  5111. v = a[1];
  5112. t1 += v * b0;
  5113. t2 += v * b1;
  5114. t3 += v * b2;
  5115. t4 += v * b3;
  5116. t5 += v * b4;
  5117. t6 += v * b5;
  5118. t7 += v * b6;
  5119. t8 += v * b7;
  5120. t9 += v * b8;
  5121. t10 += v * b9;
  5122. t11 += v * b10;
  5123. t12 += v * b11;
  5124. t13 += v * b12;
  5125. t14 += v * b13;
  5126. t15 += v * b14;
  5127. t16 += v * b15;
  5128. v = a[2];
  5129. t2 += v * b0;
  5130. t3 += v * b1;
  5131. t4 += v * b2;
  5132. t5 += v * b3;
  5133. t6 += v * b4;
  5134. t7 += v * b5;
  5135. t8 += v * b6;
  5136. t9 += v * b7;
  5137. t10 += v * b8;
  5138. t11 += v * b9;
  5139. t12 += v * b10;
  5140. t13 += v * b11;
  5141. t14 += v * b12;
  5142. t15 += v * b13;
  5143. t16 += v * b14;
  5144. t17 += v * b15;
  5145. v = a[3];
  5146. t3 += v * b0;
  5147. t4 += v * b1;
  5148. t5 += v * b2;
  5149. t6 += v * b3;
  5150. t7 += v * b4;
  5151. t8 += v * b5;
  5152. t9 += v * b6;
  5153. t10 += v * b7;
  5154. t11 += v * b8;
  5155. t12 += v * b9;
  5156. t13 += v * b10;
  5157. t14 += v * b11;
  5158. t15 += v * b12;
  5159. t16 += v * b13;
  5160. t17 += v * b14;
  5161. t18 += v * b15;
  5162. v = a[4];
  5163. t4 += v * b0;
  5164. t5 += v * b1;
  5165. t6 += v * b2;
  5166. t7 += v * b3;
  5167. t8 += v * b4;
  5168. t9 += v * b5;
  5169. t10 += v * b6;
  5170. t11 += v * b7;
  5171. t12 += v * b8;
  5172. t13 += v * b9;
  5173. t14 += v * b10;
  5174. t15 += v * b11;
  5175. t16 += v * b12;
  5176. t17 += v * b13;
  5177. t18 += v * b14;
  5178. t19 += v * b15;
  5179. v = a[5];
  5180. t5 += v * b0;
  5181. t6 += v * b1;
  5182. t7 += v * b2;
  5183. t8 += v * b3;
  5184. t9 += v * b4;
  5185. t10 += v * b5;
  5186. t11 += v * b6;
  5187. t12 += v * b7;
  5188. t13 += v * b8;
  5189. t14 += v * b9;
  5190. t15 += v * b10;
  5191. t16 += v * b11;
  5192. t17 += v * b12;
  5193. t18 += v * b13;
  5194. t19 += v * b14;
  5195. t20 += v * b15;
  5196. v = a[6];
  5197. t6 += v * b0;
  5198. t7 += v * b1;
  5199. t8 += v * b2;
  5200. t9 += v * b3;
  5201. t10 += v * b4;
  5202. t11 += v * b5;
  5203. t12 += v * b6;
  5204. t13 += v * b7;
  5205. t14 += v * b8;
  5206. t15 += v * b9;
  5207. t16 += v * b10;
  5208. t17 += v * b11;
  5209. t18 += v * b12;
  5210. t19 += v * b13;
  5211. t20 += v * b14;
  5212. t21 += v * b15;
  5213. v = a[7];
  5214. t7 += v * b0;
  5215. t8 += v * b1;
  5216. t9 += v * b2;
  5217. t10 += v * b3;
  5218. t11 += v * b4;
  5219. t12 += v * b5;
  5220. t13 += v * b6;
  5221. t14 += v * b7;
  5222. t15 += v * b8;
  5223. t16 += v * b9;
  5224. t17 += v * b10;
  5225. t18 += v * b11;
  5226. t19 += v * b12;
  5227. t20 += v * b13;
  5228. t21 += v * b14;
  5229. t22 += v * b15;
  5230. v = a[8];
  5231. t8 += v * b0;
  5232. t9 += v * b1;
  5233. t10 += v * b2;
  5234. t11 += v * b3;
  5235. t12 += v * b4;
  5236. t13 += v * b5;
  5237. t14 += v * b6;
  5238. t15 += v * b7;
  5239. t16 += v * b8;
  5240. t17 += v * b9;
  5241. t18 += v * b10;
  5242. t19 += v * b11;
  5243. t20 += v * b12;
  5244. t21 += v * b13;
  5245. t22 += v * b14;
  5246. t23 += v * b15;
  5247. v = a[9];
  5248. t9 += v * b0;
  5249. t10 += v * b1;
  5250. t11 += v * b2;
  5251. t12 += v * b3;
  5252. t13 += v * b4;
  5253. t14 += v * b5;
  5254. t15 += v * b6;
  5255. t16 += v * b7;
  5256. t17 += v * b8;
  5257. t18 += v * b9;
  5258. t19 += v * b10;
  5259. t20 += v * b11;
  5260. t21 += v * b12;
  5261. t22 += v * b13;
  5262. t23 += v * b14;
  5263. t24 += v * b15;
  5264. v = a[10];
  5265. t10 += v * b0;
  5266. t11 += v * b1;
  5267. t12 += v * b2;
  5268. t13 += v * b3;
  5269. t14 += v * b4;
  5270. t15 += v * b5;
  5271. t16 += v * b6;
  5272. t17 += v * b7;
  5273. t18 += v * b8;
  5274. t19 += v * b9;
  5275. t20 += v * b10;
  5276. t21 += v * b11;
  5277. t22 += v * b12;
  5278. t23 += v * b13;
  5279. t24 += v * b14;
  5280. t25 += v * b15;
  5281. v = a[11];
  5282. t11 += v * b0;
  5283. t12 += v * b1;
  5284. t13 += v * b2;
  5285. t14 += v * b3;
  5286. t15 += v * b4;
  5287. t16 += v * b5;
  5288. t17 += v * b6;
  5289. t18 += v * b7;
  5290. t19 += v * b8;
  5291. t20 += v * b9;
  5292. t21 += v * b10;
  5293. t22 += v * b11;
  5294. t23 += v * b12;
  5295. t24 += v * b13;
  5296. t25 += v * b14;
  5297. t26 += v * b15;
  5298. v = a[12];
  5299. t12 += v * b0;
  5300. t13 += v * b1;
  5301. t14 += v * b2;
  5302. t15 += v * b3;
  5303. t16 += v * b4;
  5304. t17 += v * b5;
  5305. t18 += v * b6;
  5306. t19 += v * b7;
  5307. t20 += v * b8;
  5308. t21 += v * b9;
  5309. t22 += v * b10;
  5310. t23 += v * b11;
  5311. t24 += v * b12;
  5312. t25 += v * b13;
  5313. t26 += v * b14;
  5314. t27 += v * b15;
  5315. v = a[13];
  5316. t13 += v * b0;
  5317. t14 += v * b1;
  5318. t15 += v * b2;
  5319. t16 += v * b3;
  5320. t17 += v * b4;
  5321. t18 += v * b5;
  5322. t19 += v * b6;
  5323. t20 += v * b7;
  5324. t21 += v * b8;
  5325. t22 += v * b9;
  5326. t23 += v * b10;
  5327. t24 += v * b11;
  5328. t25 += v * b12;
  5329. t26 += v * b13;
  5330. t27 += v * b14;
  5331. t28 += v * b15;
  5332. v = a[14];
  5333. t14 += v * b0;
  5334. t15 += v * b1;
  5335. t16 += v * b2;
  5336. t17 += v * b3;
  5337. t18 += v * b4;
  5338. t19 += v * b5;
  5339. t20 += v * b6;
  5340. t21 += v * b7;
  5341. t22 += v * b8;
  5342. t23 += v * b9;
  5343. t24 += v * b10;
  5344. t25 += v * b11;
  5345. t26 += v * b12;
  5346. t27 += v * b13;
  5347. t28 += v * b14;
  5348. t29 += v * b15;
  5349. v = a[15];
  5350. t15 += v * b0;
  5351. t16 += v * b1;
  5352. t17 += v * b2;
  5353. t18 += v * b3;
  5354. t19 += v * b4;
  5355. t20 += v * b5;
  5356. t21 += v * b6;
  5357. t22 += v * b7;
  5358. t23 += v * b8;
  5359. t24 += v * b9;
  5360. t25 += v * b10;
  5361. t26 += v * b11;
  5362. t27 += v * b12;
  5363. t28 += v * b13;
  5364. t29 += v * b14;
  5365. t30 += v * b15;
  5366. t0 += 38 * t16;
  5367. t1 += 38 * t17;
  5368. t2 += 38 * t18;
  5369. t3 += 38 * t19;
  5370. t4 += 38 * t20;
  5371. t5 += 38 * t21;
  5372. t6 += 38 * t22;
  5373. t7 += 38 * t23;
  5374. t8 += 38 * t24;
  5375. t9 += 38 * t25;
  5376. t10 += 38 * t26;
  5377. t11 += 38 * t27;
  5378. t12 += 38 * t28;
  5379. t13 += 38 * t29;
  5380. t14 += 38 * t30;
  5381. // t15 left as is
  5382. // first car
  5383. c = 1;
  5384. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  5385. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  5386. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  5387. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  5388. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  5389. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  5390. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  5391. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  5392. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  5393. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  5394. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  5395. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  5396. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  5397. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  5398. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  5399. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  5400. t0 += c-1 + 37 * (c-1);
  5401. // second car
  5402. c = 1;
  5403. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  5404. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  5405. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  5406. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  5407. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  5408. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  5409. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  5410. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  5411. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  5412. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  5413. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  5414. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  5415. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  5416. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  5417. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  5418. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  5419. t0 += c-1 + 37 * (c-1);
  5420. o[ 0] = t0;
  5421. o[ 1] = t1;
  5422. o[ 2] = t2;
  5423. o[ 3] = t3;
  5424. o[ 4] = t4;
  5425. o[ 5] = t5;
  5426. o[ 6] = t6;
  5427. o[ 7] = t7;
  5428. o[ 8] = t8;
  5429. o[ 9] = t9;
  5430. o[10] = t10;
  5431. o[11] = t11;
  5432. o[12] = t12;
  5433. o[13] = t13;
  5434. o[14] = t14;
  5435. o[15] = t15;
  5436. }
  5437. function S(o, a) {
  5438. M(o, a, a);
  5439. }
  5440. function inv25519(o, i) {
  5441. var c = gf();
  5442. var a;
  5443. for (a = 0; a < 16; a++) c[a] = i[a];
  5444. for (a = 253; a >= 0; a--) {
  5445. S(c, c);
  5446. if(a !== 2 && a !== 4) M(c, c, i);
  5447. }
  5448. for (a = 0; a < 16; a++) o[a] = c[a];
  5449. }
  5450. function pow2523(o, i) {
  5451. var c = gf();
  5452. var a;
  5453. for (a = 0; a < 16; a++) c[a] = i[a];
  5454. for (a = 250; a >= 0; a--) {
  5455. S(c, c);
  5456. if(a !== 1) M(c, c, i);
  5457. }
  5458. for (a = 0; a < 16; a++) o[a] = c[a];
  5459. }
  5460. function crypto_scalarmult(q, n, p) {
  5461. var z = new Uint8Array(32);
  5462. var x = new Float64Array(80), r, i;
  5463. var a = gf(), b = gf(), c = gf(),
  5464. d = gf(), e = gf(), f = gf();
  5465. for (i = 0; i < 31; i++) z[i] = n[i];
  5466. z[31]=(n[31]&127)|64;
  5467. z[0]&=248;
  5468. unpack25519(x,p);
  5469. for (i = 0; i < 16; i++) {
  5470. b[i]=x[i];
  5471. d[i]=a[i]=c[i]=0;
  5472. }
  5473. a[0]=d[0]=1;
  5474. for (i=254; i>=0; --i) {
  5475. r=(z[i>>>3]>>>(i&7))&1;
  5476. sel25519(a,b,r);
  5477. sel25519(c,d,r);
  5478. A(e,a,c);
  5479. Z(a,a,c);
  5480. A(c,b,d);
  5481. Z(b,b,d);
  5482. S(d,e);
  5483. S(f,a);
  5484. M(a,c,a);
  5485. M(c,b,e);
  5486. A(e,a,c);
  5487. Z(a,a,c);
  5488. S(b,a);
  5489. Z(c,d,f);
  5490. M(a,c,_121665);
  5491. A(a,a,d);
  5492. M(c,c,a);
  5493. M(a,d,f);
  5494. M(d,b,x);
  5495. S(b,e);
  5496. sel25519(a,b,r);
  5497. sel25519(c,d,r);
  5498. }
  5499. for (i = 0; i < 16; i++) {
  5500. x[i+16]=a[i];
  5501. x[i+32]=c[i];
  5502. x[i+48]=b[i];
  5503. x[i+64]=d[i];
  5504. }
  5505. var x32 = x.subarray(32);
  5506. var x16 = x.subarray(16);
  5507. inv25519(x32,x32);
  5508. M(x16,x16,x32);
  5509. pack25519(q,x16);
  5510. return 0;
  5511. }
  5512. function crypto_scalarmult_base(q, n) {
  5513. return crypto_scalarmult(q, n, _9);
  5514. }
  5515. function crypto_box_keypair(y, x) {
  5516. randombytes(x, 32);
  5517. return crypto_scalarmult_base(y, x);
  5518. }
  5519. function crypto_box_beforenm(k, y, x) {
  5520. var s = new Uint8Array(32);
  5521. crypto_scalarmult(s, x, y);
  5522. return crypto_core_hsalsa20(k, _0, s, sigma);
  5523. }
  5524. var crypto_box_afternm = crypto_secretbox;
  5525. var crypto_box_open_afternm = crypto_secretbox_open;
  5526. function crypto_box(c, m, d, n, y, x) {
  5527. var k = new Uint8Array(32);
  5528. crypto_box_beforenm(k, y, x);
  5529. return crypto_box_afternm(c, m, d, n, k);
  5530. }
  5531. function crypto_box_open(m, c, d, n, y, x) {
  5532. var k = new Uint8Array(32);
  5533. crypto_box_beforenm(k, y, x);
  5534. return crypto_box_open_afternm(m, c, d, n, k);
  5535. }
  5536. var K = [
  5537. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  5538. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  5539. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  5540. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  5541. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  5542. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  5543. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  5544. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  5545. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  5546. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  5547. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  5548. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  5549. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  5550. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  5551. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  5552. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  5553. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  5554. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  5555. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  5556. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  5557. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  5558. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  5559. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  5560. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  5561. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  5562. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  5563. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  5564. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  5565. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  5566. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  5567. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  5568. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  5569. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  5570. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  5571. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  5572. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  5573. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  5574. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  5575. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  5576. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  5577. ];
  5578. function crypto_hashblocks_hl(hh, hl, m, n) {
  5579. var wh = new Int32Array(16), wl = new Int32Array(16),
  5580. bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7,
  5581. bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7,
  5582. th, tl, i, j, h, l, a, b, c, d;
  5583. var ah0 = hh[0],
  5584. ah1 = hh[1],
  5585. ah2 = hh[2],
  5586. ah3 = hh[3],
  5587. ah4 = hh[4],
  5588. ah5 = hh[5],
  5589. ah6 = hh[6],
  5590. ah7 = hh[7],
  5591. al0 = hl[0],
  5592. al1 = hl[1],
  5593. al2 = hl[2],
  5594. al3 = hl[3],
  5595. al4 = hl[4],
  5596. al5 = hl[5],
  5597. al6 = hl[6],
  5598. al7 = hl[7];
  5599. var pos = 0;
  5600. while (n >= 128) {
  5601. for (i = 0; i < 16; i++) {
  5602. j = 8 * i + pos;
  5603. wh[i] = (m[j+0] << 24) | (m[j+1] << 16) | (m[j+2] << 8) | m[j+3];
  5604. wl[i] = (m[j+4] << 24) | (m[j+5] << 16) | (m[j+6] << 8) | m[j+7];
  5605. }
  5606. for (i = 0; i < 80; i++) {
  5607. bh0 = ah0;
  5608. bh1 = ah1;
  5609. bh2 = ah2;
  5610. bh3 = ah3;
  5611. bh4 = ah4;
  5612. bh5 = ah5;
  5613. bh6 = ah6;
  5614. bh7 = ah7;
  5615. bl0 = al0;
  5616. bl1 = al1;
  5617. bl2 = al2;
  5618. bl3 = al3;
  5619. bl4 = al4;
  5620. bl5 = al5;
  5621. bl6 = al6;
  5622. bl7 = al7;
  5623. // add
  5624. h = ah7;
  5625. l = al7;
  5626. a = l & 0xffff; b = l >>> 16;
  5627. c = h & 0xffff; d = h >>> 16;
  5628. // Sigma1
  5629. h = ((ah4 >>> 14) | (al4 << (32-14))) ^ ((ah4 >>> 18) | (al4 << (32-18))) ^ ((al4 >>> (41-32)) | (ah4 << (32-(41-32))));
  5630. l = ((al4 >>> 14) | (ah4 << (32-14))) ^ ((al4 >>> 18) | (ah4 << (32-18))) ^ ((ah4 >>> (41-32)) | (al4 << (32-(41-32))));
  5631. a += l & 0xffff; b += l >>> 16;
  5632. c += h & 0xffff; d += h >>> 16;
  5633. // Ch
  5634. h = (ah4 & ah5) ^ (~ah4 & ah6);
  5635. l = (al4 & al5) ^ (~al4 & al6);
  5636. a += l & 0xffff; b += l >>> 16;
  5637. c += h & 0xffff; d += h >>> 16;
  5638. // K
  5639. h = K[i*2];
  5640. l = K[i*2+1];
  5641. a += l & 0xffff; b += l >>> 16;
  5642. c += h & 0xffff; d += h >>> 16;
  5643. // w
  5644. h = wh[i%16];
  5645. l = wl[i%16];
  5646. a += l & 0xffff; b += l >>> 16;
  5647. c += h & 0xffff; d += h >>> 16;
  5648. b += a >>> 16;
  5649. c += b >>> 16;
  5650. d += c >>> 16;
  5651. th = c & 0xffff | d << 16;
  5652. tl = a & 0xffff | b << 16;
  5653. // add
  5654. h = th;
  5655. l = tl;
  5656. a = l & 0xffff; b = l >>> 16;
  5657. c = h & 0xffff; d = h >>> 16;
  5658. // Sigma0
  5659. h = ((ah0 >>> 28) | (al0 << (32-28))) ^ ((al0 >>> (34-32)) | (ah0 << (32-(34-32)))) ^ ((al0 >>> (39-32)) | (ah0 << (32-(39-32))));
  5660. l = ((al0 >>> 28) | (ah0 << (32-28))) ^ ((ah0 >>> (34-32)) | (al0 << (32-(34-32)))) ^ ((ah0 >>> (39-32)) | (al0 << (32-(39-32))));
  5661. a += l & 0xffff; b += l >>> 16;
  5662. c += h & 0xffff; d += h >>> 16;
  5663. // Maj
  5664. h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
  5665. l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);
  5666. a += l & 0xffff; b += l >>> 16;
  5667. c += h & 0xffff; d += h >>> 16;
  5668. b += a >>> 16;
  5669. c += b >>> 16;
  5670. d += c >>> 16;
  5671. bh7 = (c & 0xffff) | (d << 16);
  5672. bl7 = (a & 0xffff) | (b << 16);
  5673. // add
  5674. h = bh3;
  5675. l = bl3;
  5676. a = l & 0xffff; b = l >>> 16;
  5677. c = h & 0xffff; d = h >>> 16;
  5678. h = th;
  5679. l = tl;
  5680. a += l & 0xffff; b += l >>> 16;
  5681. c += h & 0xffff; d += h >>> 16;
  5682. b += a >>> 16;
  5683. c += b >>> 16;
  5684. d += c >>> 16;
  5685. bh3 = (c & 0xffff) | (d << 16);
  5686. bl3 = (a & 0xffff) | (b << 16);
  5687. ah1 = bh0;
  5688. ah2 = bh1;
  5689. ah3 = bh2;
  5690. ah4 = bh3;
  5691. ah5 = bh4;
  5692. ah6 = bh5;
  5693. ah7 = bh6;
  5694. ah0 = bh7;
  5695. al1 = bl0;
  5696. al2 = bl1;
  5697. al3 = bl2;
  5698. al4 = bl3;
  5699. al5 = bl4;
  5700. al6 = bl5;
  5701. al7 = bl6;
  5702. al0 = bl7;
  5703. if (i%16 === 15) {
  5704. for (j = 0; j < 16; j++) {
  5705. // add
  5706. h = wh[j];
  5707. l = wl[j];
  5708. a = l & 0xffff; b = l >>> 16;
  5709. c = h & 0xffff; d = h >>> 16;
  5710. h = wh[(j+9)%16];
  5711. l = wl[(j+9)%16];
  5712. a += l & 0xffff; b += l >>> 16;
  5713. c += h & 0xffff; d += h >>> 16;
  5714. // sigma0
  5715. th = wh[(j+1)%16];
  5716. tl = wl[(j+1)%16];
  5717. h = ((th >>> 1) | (tl << (32-1))) ^ ((th >>> 8) | (tl << (32-8))) ^ (th >>> 7);
  5718. l = ((tl >>> 1) | (th << (32-1))) ^ ((tl >>> 8) | (th << (32-8))) ^ ((tl >>> 7) | (th << (32-7)));
  5719. a += l & 0xffff; b += l >>> 16;
  5720. c += h & 0xffff; d += h >>> 16;
  5721. // sigma1
  5722. th = wh[(j+14)%16];
  5723. tl = wl[(j+14)%16];
  5724. h = ((th >>> 19) | (tl << (32-19))) ^ ((tl >>> (61-32)) | (th << (32-(61-32)))) ^ (th >>> 6);
  5725. l = ((tl >>> 19) | (th << (32-19))) ^ ((th >>> (61-32)) | (tl << (32-(61-32)))) ^ ((tl >>> 6) | (th << (32-6)));
  5726. a += l & 0xffff; b += l >>> 16;
  5727. c += h & 0xffff; d += h >>> 16;
  5728. b += a >>> 16;
  5729. c += b >>> 16;
  5730. d += c >>> 16;
  5731. wh[j] = (c & 0xffff) | (d << 16);
  5732. wl[j] = (a & 0xffff) | (b << 16);
  5733. }
  5734. }
  5735. }
  5736. // add
  5737. h = ah0;
  5738. l = al0;
  5739. a = l & 0xffff; b = l >>> 16;
  5740. c = h & 0xffff; d = h >>> 16;
  5741. h = hh[0];
  5742. l = hl[0];
  5743. a += l & 0xffff; b += l >>> 16;
  5744. c += h & 0xffff; d += h >>> 16;
  5745. b += a >>> 16;
  5746. c += b >>> 16;
  5747. d += c >>> 16;
  5748. hh[0] = ah0 = (c & 0xffff) | (d << 16);
  5749. hl[0] = al0 = (a & 0xffff) | (b << 16);
  5750. h = ah1;
  5751. l = al1;
  5752. a = l & 0xffff; b = l >>> 16;
  5753. c = h & 0xffff; d = h >>> 16;
  5754. h = hh[1];
  5755. l = hl[1];
  5756. a += l & 0xffff; b += l >>> 16;
  5757. c += h & 0xffff; d += h >>> 16;
  5758. b += a >>> 16;
  5759. c += b >>> 16;
  5760. d += c >>> 16;
  5761. hh[1] = ah1 = (c & 0xffff) | (d << 16);
  5762. hl[1] = al1 = (a & 0xffff) | (b << 16);
  5763. h = ah2;
  5764. l = al2;
  5765. a = l & 0xffff; b = l >>> 16;
  5766. c = h & 0xffff; d = h >>> 16;
  5767. h = hh[2];
  5768. l = hl[2];
  5769. a += l & 0xffff; b += l >>> 16;
  5770. c += h & 0xffff; d += h >>> 16;
  5771. b += a >>> 16;
  5772. c += b >>> 16;
  5773. d += c >>> 16;
  5774. hh[2] = ah2 = (c & 0xffff) | (d << 16);
  5775. hl[2] = al2 = (a & 0xffff) | (b << 16);
  5776. h = ah3;
  5777. l = al3;
  5778. a = l & 0xffff; b = l >>> 16;
  5779. c = h & 0xffff; d = h >>> 16;
  5780. h = hh[3];
  5781. l = hl[3];
  5782. a += l & 0xffff; b += l >>> 16;
  5783. c += h & 0xffff; d += h >>> 16;
  5784. b += a >>> 16;
  5785. c += b >>> 16;
  5786. d += c >>> 16;
  5787. hh[3] = ah3 = (c & 0xffff) | (d << 16);
  5788. hl[3] = al3 = (a & 0xffff) | (b << 16);
  5789. h = ah4;
  5790. l = al4;
  5791. a = l & 0xffff; b = l >>> 16;
  5792. c = h & 0xffff; d = h >>> 16;
  5793. h = hh[4];
  5794. l = hl[4];
  5795. a += l & 0xffff; b += l >>> 16;
  5796. c += h & 0xffff; d += h >>> 16;
  5797. b += a >>> 16;
  5798. c += b >>> 16;
  5799. d += c >>> 16;
  5800. hh[4] = ah4 = (c & 0xffff) | (d << 16);
  5801. hl[4] = al4 = (a & 0xffff) | (b << 16);
  5802. h = ah5;
  5803. l = al5;
  5804. a = l & 0xffff; b = l >>> 16;
  5805. c = h & 0xffff; d = h >>> 16;
  5806. h = hh[5];
  5807. l = hl[5];
  5808. a += l & 0xffff; b += l >>> 16;
  5809. c += h & 0xffff; d += h >>> 16;
  5810. b += a >>> 16;
  5811. c += b >>> 16;
  5812. d += c >>> 16;
  5813. hh[5] = ah5 = (c & 0xffff) | (d << 16);
  5814. hl[5] = al5 = (a & 0xffff) | (b << 16);
  5815. h = ah6;
  5816. l = al6;
  5817. a = l & 0xffff; b = l >>> 16;
  5818. c = h & 0xffff; d = h >>> 16;
  5819. h = hh[6];
  5820. l = hl[6];
  5821. a += l & 0xffff; b += l >>> 16;
  5822. c += h & 0xffff; d += h >>> 16;
  5823. b += a >>> 16;
  5824. c += b >>> 16;
  5825. d += c >>> 16;
  5826. hh[6] = ah6 = (c & 0xffff) | (d << 16);
  5827. hl[6] = al6 = (a & 0xffff) | (b << 16);
  5828. h = ah7;
  5829. l = al7;
  5830. a = l & 0xffff; b = l >>> 16;
  5831. c = h & 0xffff; d = h >>> 16;
  5832. h = hh[7];
  5833. l = hl[7];
  5834. a += l & 0xffff; b += l >>> 16;
  5835. c += h & 0xffff; d += h >>> 16;
  5836. b += a >>> 16;
  5837. c += b >>> 16;
  5838. d += c >>> 16;
  5839. hh[7] = ah7 = (c & 0xffff) | (d << 16);
  5840. hl[7] = al7 = (a & 0xffff) | (b << 16);
  5841. pos += 128;
  5842. n -= 128;
  5843. }
  5844. return n;
  5845. }
  5846. function crypto_hash(out, m, n) {
  5847. var hh = new Int32Array(8),
  5848. hl = new Int32Array(8),
  5849. x = new Uint8Array(256),
  5850. i, b = n;
  5851. hh[0] = 0x6a09e667;
  5852. hh[1] = 0xbb67ae85;
  5853. hh[2] = 0x3c6ef372;
  5854. hh[3] = 0xa54ff53a;
  5855. hh[4] = 0x510e527f;
  5856. hh[5] = 0x9b05688c;
  5857. hh[6] = 0x1f83d9ab;
  5858. hh[7] = 0x5be0cd19;
  5859. hl[0] = 0xf3bcc908;
  5860. hl[1] = 0x84caa73b;
  5861. hl[2] = 0xfe94f82b;
  5862. hl[3] = 0x5f1d36f1;
  5863. hl[4] = 0xade682d1;
  5864. hl[5] = 0x2b3e6c1f;
  5865. hl[6] = 0xfb41bd6b;
  5866. hl[7] = 0x137e2179;
  5867. crypto_hashblocks_hl(hh, hl, m, n);
  5868. n %= 128;
  5869. for (i = 0; i < n; i++) x[i] = m[b-n+i];
  5870. x[n] = 128;
  5871. n = 256-128*(n<112?1:0);
  5872. x[n-9] = 0;
  5873. ts64(x, n-8, (b / 0x20000000) | 0, b << 3);
  5874. crypto_hashblocks_hl(hh, hl, x, n);
  5875. for (i = 0; i < 8; i++) ts64(out, 8*i, hh[i], hl[i]);
  5876. return 0;
  5877. }
  5878. function add(p, q) {
  5879. var a = gf(), b = gf(), c = gf(),
  5880. d = gf(), e = gf(), f = gf(),
  5881. g = gf(), h = gf(), t = gf();
  5882. Z(a, p[1], p[0]);
  5883. Z(t, q[1], q[0]);
  5884. M(a, a, t);
  5885. A(b, p[0], p[1]);
  5886. A(t, q[0], q[1]);
  5887. M(b, b, t);
  5888. M(c, p[3], q[3]);
  5889. M(c, c, D2);
  5890. M(d, p[2], q[2]);
  5891. A(d, d, d);
  5892. Z(e, b, a);
  5893. Z(f, d, c);
  5894. A(g, d, c);
  5895. A(h, b, a);
  5896. M(p[0], e, f);
  5897. M(p[1], h, g);
  5898. M(p[2], g, f);
  5899. M(p[3], e, h);
  5900. }
  5901. function cswap(p, q, b) {
  5902. var i;
  5903. for (i = 0; i < 4; i++) {
  5904. sel25519(p[i], q[i], b);
  5905. }
  5906. }
  5907. function pack(r, p) {
  5908. var tx = gf(), ty = gf(), zi = gf();
  5909. inv25519(zi, p[2]);
  5910. M(tx, p[0], zi);
  5911. M(ty, p[1], zi);
  5912. pack25519(r, ty);
  5913. r[31] ^= par25519(tx) << 7;
  5914. }
  5915. function scalarmult(p, q, s) {
  5916. var b, i;
  5917. set25519(p[0], gf0);
  5918. set25519(p[1], gf1);
  5919. set25519(p[2], gf1);
  5920. set25519(p[3], gf0);
  5921. for (i = 255; i >= 0; --i) {
  5922. b = (s[(i/8)|0] >> (i&7)) & 1;
  5923. cswap(p, q, b);
  5924. add(q, p);
  5925. add(p, p);
  5926. cswap(p, q, b);
  5927. }
  5928. }
  5929. function scalarbase(p, s) {
  5930. var q = [gf(), gf(), gf(), gf()];
  5931. set25519(q[0], X);
  5932. set25519(q[1], Y);
  5933. set25519(q[2], gf1);
  5934. M(q[3], X, Y);
  5935. scalarmult(p, q, s);
  5936. }
  5937. function crypto_sign_keypair(pk, sk, seeded) {
  5938. var d = new Uint8Array(64);
  5939. var p = [gf(), gf(), gf(), gf()];
  5940. var i;
  5941. if (!seeded) randombytes(sk, 32);
  5942. crypto_hash(d, sk, 32);
  5943. d[0] &= 248;
  5944. d[31] &= 127;
  5945. d[31] |= 64;
  5946. scalarbase(p, d);
  5947. pack(pk, p);
  5948. for (i = 0; i < 32; i++) sk[i+32] = pk[i];
  5949. return 0;
  5950. }
  5951. var L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
  5952. function modL(r, x) {
  5953. var carry, i, j, k;
  5954. for (i = 63; i >= 32; --i) {
  5955. carry = 0;
  5956. for (j = i - 32, k = i - 12; j < k; ++j) {
  5957. x[j] += carry - 16 * x[i] * L[j - (i - 32)];
  5958. carry = Math.floor((x[j] + 128) / 256);
  5959. x[j] -= carry * 256;
  5960. }
  5961. x[j] += carry;
  5962. x[i] = 0;
  5963. }
  5964. carry = 0;
  5965. for (j = 0; j < 32; j++) {
  5966. x[j] += carry - (x[31] >> 4) * L[j];
  5967. carry = x[j] >> 8;
  5968. x[j] &= 255;
  5969. }
  5970. for (j = 0; j < 32; j++) x[j] -= carry * L[j];
  5971. for (i = 0; i < 32; i++) {
  5972. x[i+1] += x[i] >> 8;
  5973. r[i] = x[i] & 255;
  5974. }
  5975. }
  5976. function reduce(r) {
  5977. var x = new Float64Array(64), i;
  5978. for (i = 0; i < 64; i++) x[i] = r[i];
  5979. for (i = 0; i < 64; i++) r[i] = 0;
  5980. modL(r, x);
  5981. }
  5982. // Note: difference from C - smlen returned, not passed as argument.
  5983. function crypto_sign(sm, m, n, sk) {
  5984. var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
  5985. var i, j, x = new Float64Array(64);
  5986. var p = [gf(), gf(), gf(), gf()];
  5987. crypto_hash(d, sk, 32);
  5988. d[0] &= 248;
  5989. d[31] &= 127;
  5990. d[31] |= 64;
  5991. var smlen = n + 64;
  5992. for (i = 0; i < n; i++) sm[64 + i] = m[i];
  5993. for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
  5994. crypto_hash(r, sm.subarray(32), n+32);
  5995. reduce(r);
  5996. scalarbase(p, r);
  5997. pack(sm, p);
  5998. for (i = 32; i < 64; i++) sm[i] = sk[i];
  5999. crypto_hash(h, sm, n + 64);
  6000. reduce(h);
  6001. for (i = 0; i < 64; i++) x[i] = 0;
  6002. for (i = 0; i < 32; i++) x[i] = r[i];
  6003. for (i = 0; i < 32; i++) {
  6004. for (j = 0; j < 32; j++) {
  6005. x[i+j] += h[i] * d[j];
  6006. }
  6007. }
  6008. modL(sm.subarray(32), x);
  6009. return smlen;
  6010. }
  6011. function unpackneg(r, p) {
  6012. var t = gf(), chk = gf(), num = gf(),
  6013. den = gf(), den2 = gf(), den4 = gf(),
  6014. den6 = gf();
  6015. set25519(r[2], gf1);
  6016. unpack25519(r[1], p);
  6017. S(num, r[1]);
  6018. M(den, num, D);
  6019. Z(num, num, r[2]);
  6020. A(den, r[2], den);
  6021. S(den2, den);
  6022. S(den4, den2);
  6023. M(den6, den4, den2);
  6024. M(t, den6, num);
  6025. M(t, t, den);
  6026. pow2523(t, t);
  6027. M(t, t, num);
  6028. M(t, t, den);
  6029. M(t, t, den);
  6030. M(r[0], t, den);
  6031. S(chk, r[0]);
  6032. M(chk, chk, den);
  6033. if (neq25519(chk, num)) M(r[0], r[0], I);
  6034. S(chk, r[0]);
  6035. M(chk, chk, den);
  6036. if (neq25519(chk, num)) return -1;
  6037. if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
  6038. M(r[3], r[0], r[1]);
  6039. return 0;
  6040. }
  6041. function crypto_sign_open(m, sm, n, pk) {
  6042. var i;
  6043. var t = new Uint8Array(32), h = new Uint8Array(64);
  6044. var p = [gf(), gf(), gf(), gf()],
  6045. q = [gf(), gf(), gf(), gf()];
  6046. if (n < 64) return -1;
  6047. if (unpackneg(q, pk)) return -1;
  6048. for (i = 0; i < n; i++) m[i] = sm[i];
  6049. for (i = 0; i < 32; i++) m[i+32] = pk[i];
  6050. crypto_hash(h, m, n);
  6051. reduce(h);
  6052. scalarmult(p, q, h);
  6053. scalarbase(q, sm.subarray(32));
  6054. add(p, q);
  6055. pack(t, p);
  6056. n -= 64;
  6057. if (crypto_verify_32(sm, 0, t, 0)) {
  6058. for (i = 0; i < n; i++) m[i] = 0;
  6059. return -1;
  6060. }
  6061. for (i = 0; i < n; i++) m[i] = sm[i + 64];
  6062. return n;
  6063. }
  6064. var crypto_secretbox_KEYBYTES = 32,
  6065. crypto_secretbox_NONCEBYTES = 24,
  6066. crypto_secretbox_ZEROBYTES = 32,
  6067. crypto_secretbox_BOXZEROBYTES = 16,
  6068. crypto_scalarmult_BYTES = 32,
  6069. crypto_scalarmult_SCALARBYTES = 32,
  6070. crypto_box_PUBLICKEYBYTES = 32,
  6071. crypto_box_SECRETKEYBYTES = 32,
  6072. crypto_box_BEFORENMBYTES = 32,
  6073. crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
  6074. crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
  6075. crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
  6076. crypto_sign_BYTES = 64,
  6077. crypto_sign_PUBLICKEYBYTES = 32,
  6078. crypto_sign_SECRETKEYBYTES = 64,
  6079. crypto_sign_SEEDBYTES = 32,
  6080. crypto_hash_BYTES = 64;
  6081. nacl.lowlevel = {
  6082. crypto_core_hsalsa20: crypto_core_hsalsa20,
  6083. crypto_stream_xor: crypto_stream_xor,
  6084. crypto_stream: crypto_stream,
  6085. crypto_stream_salsa20_xor: crypto_stream_salsa20_xor,
  6086. crypto_stream_salsa20: crypto_stream_salsa20,
  6087. crypto_onetimeauth: crypto_onetimeauth,
  6088. crypto_onetimeauth_verify: crypto_onetimeauth_verify,
  6089. crypto_verify_16: crypto_verify_16,
  6090. crypto_verify_32: crypto_verify_32,
  6091. crypto_secretbox: crypto_secretbox,
  6092. crypto_secretbox_open: crypto_secretbox_open,
  6093. crypto_scalarmult: crypto_scalarmult,
  6094. crypto_scalarmult_base: crypto_scalarmult_base,
  6095. crypto_box_beforenm: crypto_box_beforenm,
  6096. crypto_box_afternm: crypto_box_afternm,
  6097. crypto_box: crypto_box,
  6098. crypto_box_open: crypto_box_open,
  6099. crypto_box_keypair: crypto_box_keypair,
  6100. crypto_hash: crypto_hash,
  6101. crypto_sign: crypto_sign,
  6102. crypto_sign_keypair: crypto_sign_keypair,
  6103. crypto_sign_open: crypto_sign_open,
  6104. crypto_secretbox_KEYBYTES: crypto_secretbox_KEYBYTES,
  6105. crypto_secretbox_NONCEBYTES: crypto_secretbox_NONCEBYTES,
  6106. crypto_secretbox_ZEROBYTES: crypto_secretbox_ZEROBYTES,
  6107. crypto_secretbox_BOXZEROBYTES: crypto_secretbox_BOXZEROBYTES,
  6108. crypto_scalarmult_BYTES: crypto_scalarmult_BYTES,
  6109. crypto_scalarmult_SCALARBYTES: crypto_scalarmult_SCALARBYTES,
  6110. crypto_box_PUBLICKEYBYTES: crypto_box_PUBLICKEYBYTES,
  6111. crypto_box_SECRETKEYBYTES: crypto_box_SECRETKEYBYTES,
  6112. crypto_box_BEFORENMBYTES: crypto_box_BEFORENMBYTES,
  6113. crypto_box_NONCEBYTES: crypto_box_NONCEBYTES,
  6114. crypto_box_ZEROBYTES: crypto_box_ZEROBYTES,
  6115. crypto_box_BOXZEROBYTES: crypto_box_BOXZEROBYTES,
  6116. crypto_sign_BYTES: crypto_sign_BYTES,
  6117. crypto_sign_PUBLICKEYBYTES: crypto_sign_PUBLICKEYBYTES,
  6118. crypto_sign_SECRETKEYBYTES: crypto_sign_SECRETKEYBYTES,
  6119. crypto_sign_SEEDBYTES: crypto_sign_SEEDBYTES,
  6120. crypto_hash_BYTES: crypto_hash_BYTES,
  6121. gf: gf,
  6122. D: D,
  6123. L: L,
  6124. pack25519: pack25519,
  6125. unpack25519: unpack25519,
  6126. M: M,
  6127. A: A,
  6128. S: S,
  6129. Z: Z,
  6130. pow2523: pow2523,
  6131. add: add,
  6132. set25519: set25519,
  6133. modL: modL,
  6134. scalarmult: scalarmult,
  6135. scalarbase: scalarbase,
  6136. };
  6137. /* High-level API */
  6138. function checkLengths(k, n) {
  6139. if (k.length !== crypto_secretbox_KEYBYTES) throw new Error('bad key size');
  6140. if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error('bad nonce size');
  6141. }
  6142. function checkBoxLengths(pk, sk) {
  6143. if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error('bad public key size');
  6144. if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');
  6145. }
  6146. function checkArrayTypes() {
  6147. for (var i = 0; i < arguments.length; i++) {
  6148. if (!(arguments[i] instanceof Uint8Array))
  6149. throw new TypeError('unexpected type, use Uint8Array');
  6150. }
  6151. }
  6152. function cleanup(arr) {
  6153. for (var i = 0; i < arr.length; i++) arr[i] = 0;
  6154. }
  6155. nacl.randomBytes = function(n) {
  6156. var b = new Uint8Array(n);
  6157. randombytes(b, n);
  6158. return b;
  6159. };
  6160. nacl.secretbox = function(msg, nonce, key) {
  6161. checkArrayTypes(msg, nonce, key);
  6162. checkLengths(key, nonce);
  6163. var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
  6164. var c = new Uint8Array(m.length);
  6165. for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];
  6166. crypto_secretbox(c, m, m.length, nonce, key);
  6167. return c.subarray(crypto_secretbox_BOXZEROBYTES);
  6168. };
  6169. nacl.secretbox.open = function(box, nonce, key) {
  6170. checkArrayTypes(box, nonce, key);
  6171. checkLengths(key, nonce);
  6172. var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
  6173. var m = new Uint8Array(c.length);
  6174. for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];
  6175. if (c.length < 32) return null;
  6176. if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
  6177. return m.subarray(crypto_secretbox_ZEROBYTES);
  6178. };
  6179. nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
  6180. nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
  6181. nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
  6182. nacl.scalarMult = function(n, p) {
  6183. checkArrayTypes(n, p);
  6184. if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
  6185. if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');
  6186. var q = new Uint8Array(crypto_scalarmult_BYTES);
  6187. crypto_scalarmult(q, n, p);
  6188. return q;
  6189. };
  6190. nacl.scalarMult.base = function(n) {
  6191. checkArrayTypes(n);
  6192. if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
  6193. var q = new Uint8Array(crypto_scalarmult_BYTES);
  6194. crypto_scalarmult_base(q, n);
  6195. return q;
  6196. };
  6197. nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
  6198. nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
  6199. nacl.box = function(msg, nonce, publicKey, secretKey) {
  6200. var k = nacl.box.before(publicKey, secretKey);
  6201. return nacl.secretbox(msg, nonce, k);
  6202. };
  6203. nacl.box.before = function(publicKey, secretKey) {
  6204. checkArrayTypes(publicKey, secretKey);
  6205. checkBoxLengths(publicKey, secretKey);
  6206. var k = new Uint8Array(crypto_box_BEFORENMBYTES);
  6207. crypto_box_beforenm(k, publicKey, secretKey);
  6208. return k;
  6209. };
  6210. nacl.box.after = nacl.secretbox;
  6211. nacl.box.open = function(msg, nonce, publicKey, secretKey) {
  6212. var k = nacl.box.before(publicKey, secretKey);
  6213. return nacl.secretbox.open(msg, nonce, k);
  6214. };
  6215. nacl.box.open.after = nacl.secretbox.open;
  6216. nacl.box.keyPair = function() {
  6217. var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
  6218. var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
  6219. crypto_box_keypair(pk, sk);
  6220. return {publicKey: pk, secretKey: sk};
  6221. };
  6222. nacl.box.keyPair.fromSecretKey = function(secretKey) {
  6223. checkArrayTypes(secretKey);
  6224. if (secretKey.length !== crypto_box_SECRETKEYBYTES)
  6225. throw new Error('bad secret key size');
  6226. var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
  6227. crypto_scalarmult_base(pk, secretKey);
  6228. return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
  6229. };
  6230. nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
  6231. nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
  6232. nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
  6233. nacl.box.nonceLength = crypto_box_NONCEBYTES;
  6234. nacl.box.overheadLength = nacl.secretbox.overheadLength;
  6235. nacl.sign = function(msg, secretKey) {
  6236. checkArrayTypes(msg, secretKey);
  6237. if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
  6238. throw new Error('bad secret key size');
  6239. var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);
  6240. crypto_sign(signedMsg, msg, msg.length, secretKey);
  6241. return signedMsg;
  6242. };
  6243. nacl.sign.open = function(signedMsg, publicKey) {
  6244. checkArrayTypes(signedMsg, publicKey);
  6245. if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
  6246. throw new Error('bad public key size');
  6247. var tmp = new Uint8Array(signedMsg.length);
  6248. var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
  6249. if (mlen < 0) return null;
  6250. var m = new Uint8Array(mlen);
  6251. for (var i = 0; i < m.length; i++) m[i] = tmp[i];
  6252. return m;
  6253. };
  6254. nacl.sign.detached = function(msg, secretKey) {
  6255. var signedMsg = nacl.sign(msg, secretKey);
  6256. var sig = new Uint8Array(crypto_sign_BYTES);
  6257. for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
  6258. return sig;
  6259. };
  6260. nacl.sign.detached.verify = function(msg, sig, publicKey) {
  6261. checkArrayTypes(msg, sig, publicKey);
  6262. if (sig.length !== crypto_sign_BYTES)
  6263. throw new Error('bad signature size');
  6264. if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
  6265. throw new Error('bad public key size');
  6266. var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
  6267. var m = new Uint8Array(crypto_sign_BYTES + msg.length);
  6268. var i;
  6269. for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
  6270. for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
  6271. return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
  6272. };
  6273. nacl.sign.keyPair = function() {
  6274. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  6275. var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
  6276. crypto_sign_keypair(pk, sk);
  6277. return {publicKey: pk, secretKey: sk};
  6278. };
  6279. nacl.sign.keyPair.fromSecretKey = function(secretKey) {
  6280. checkArrayTypes(secretKey);
  6281. if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
  6282. throw new Error('bad secret key size');
  6283. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  6284. for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];
  6285. return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
  6286. };
  6287. nacl.sign.keyPair.fromSeed = function(seed) {
  6288. checkArrayTypes(seed);
  6289. if (seed.length !== crypto_sign_SEEDBYTES)
  6290. throw new Error('bad seed size');
  6291. var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
  6292. var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
  6293. for (var i = 0; i < 32; i++) sk[i] = seed[i];
  6294. crypto_sign_keypair(pk, sk, true);
  6295. return {publicKey: pk, secretKey: sk};
  6296. };
  6297. nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
  6298. nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
  6299. nacl.sign.seedLength = crypto_sign_SEEDBYTES;
  6300. nacl.sign.signatureLength = crypto_sign_BYTES;
  6301. nacl.hash = function(msg) {
  6302. checkArrayTypes(msg);
  6303. var h = new Uint8Array(crypto_hash_BYTES);
  6304. crypto_hash(h, msg, msg.length);
  6305. return h;
  6306. };
  6307. nacl.hash.hashLength = crypto_hash_BYTES;
  6308. nacl.verify = function(x, y) {
  6309. checkArrayTypes(x, y);
  6310. // Zero length arguments are considered not equal.
  6311. if (x.length === 0 || y.length === 0) return false;
  6312. if (x.length !== y.length) return false;
  6313. return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
  6314. };
  6315. nacl.setPRNG = function(fn) {
  6316. randombytes = fn;
  6317. };
  6318. (function() {
  6319. // Initialize PRNG if environment provides CSPRNG.
  6320. // If not, methods calling randombytes will throw.
  6321. var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
  6322. if (crypto && crypto.getRandomValues) {
  6323. // Browsers.
  6324. var QUOTA = 65536;
  6325. nacl.setPRNG(function(x, n) {
  6326. var i, v = new Uint8Array(n);
  6327. for (i = 0; i < n; i += QUOTA) {
  6328. crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
  6329. }
  6330. for (i = 0; i < n; i++) x[i] = v[i];
  6331. cleanup(v);
  6332. });
  6333. } else if (typeof require !== 'undefined') {
  6334. // Node.js.
  6335. crypto = require('crypto');
  6336. if (crypto && crypto.randomBytes) {
  6337. nacl.setPRNG(function(x, n) {
  6338. var i, v = crypto.randomBytes(n);
  6339. for (i = 0; i < n; i++) x[i] = v[i];
  6340. cleanup(v);
  6341. });
  6342. }
  6343. }
  6344. })();
  6345. })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));
  6346. },{"crypto":42}],39:[function(require,module,exports){
  6347. (function (global){(function (){
  6348. /**
  6349. * Module exports.
  6350. */
  6351. module.exports = deprecate;
  6352. /**
  6353. * Mark that a method should not be used.
  6354. * Returns a modified function which warns once by default.
  6355. *
  6356. * If `localStorage.noDeprecation = true` is set, then it is a no-op.
  6357. *
  6358. * If `localStorage.throwDeprecation = true` is set, then deprecated functions
  6359. * will throw an Error when invoked.
  6360. *
  6361. * If `localStorage.traceDeprecation = true` is set, then deprecated functions
  6362. * will invoke `console.trace()` instead of `console.error()`.
  6363. *
  6364. * @param {Function} fn - the function to deprecate
  6365. * @param {String} msg - the string to print to the console when `fn` is invoked
  6366. * @returns {Function} a new "deprecated" version of `fn`
  6367. * @api public
  6368. */
  6369. function deprecate (fn, msg) {
  6370. if (config('noDeprecation')) {
  6371. return fn;
  6372. }
  6373. var warned = false;
  6374. function deprecated() {
  6375. if (!warned) {
  6376. if (config('throwDeprecation')) {
  6377. throw new Error(msg);
  6378. } else if (config('traceDeprecation')) {
  6379. console.trace(msg);
  6380. } else {
  6381. console.warn(msg);
  6382. }
  6383. warned = true;
  6384. }
  6385. return fn.apply(this, arguments);
  6386. }
  6387. return deprecated;
  6388. }
  6389. /**
  6390. * Checks `localStorage` for boolean values for the given `name`.
  6391. *
  6392. * @param {String} name
  6393. * @returns {Boolean}
  6394. * @api private
  6395. */
  6396. function config (name) {
  6397. // accessing global.localStorage can trigger a DOMException in sandboxed iframes
  6398. try {
  6399. if (!global.localStorage) return false;
  6400. } catch (_) {
  6401. return false;
  6402. }
  6403. var val = global.localStorage[name];
  6404. if (null == val) return false;
  6405. return String(val).toLowerCase() === 'true';
  6406. }
  6407. }).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  6408. },{}],40:[function(require,module,exports){
  6409. var nacl = require('tweetnacl');
  6410. var bs58 = require('bs58');
  6411. var ed25519HdKey = require('ed25519-hd-key');
  6412. var tpUtils = window.tpUtils || {};
  6413. tpUtils.nacl = nacl;
  6414. tpUtils.bs58 = bs58;
  6415. tpUtils.ed25519HdKey = ed25519HdKey;
  6416. window.tpUtils = tpUtils;
  6417. },{"bs58":2,"ed25519-hd-key":7,"tweetnacl":38}],41:[function(require,module,exports){
  6418. 'use strict'
  6419. exports.byteLength = byteLength
  6420. exports.toByteArray = toByteArray
  6421. exports.fromByteArray = fromByteArray
  6422. var lookup = []
  6423. var revLookup = []
  6424. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  6425. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  6426. for (var i = 0, len = code.length; i < len; ++i) {
  6427. lookup[i] = code[i]
  6428. revLookup[code.charCodeAt(i)] = i
  6429. }
  6430. // Support decoding URL-safe base64 strings, as Node.js does.
  6431. // See: https://en.wikipedia.org/wiki/Base64#URL_applications
  6432. revLookup['-'.charCodeAt(0)] = 62
  6433. revLookup['_'.charCodeAt(0)] = 63
  6434. function getLens (b64) {
  6435. var len = b64.length
  6436. if (len % 4 > 0) {
  6437. throw new Error('Invalid string. Length must be a multiple of 4')
  6438. }
  6439. // Trim off extra bytes after placeholder bytes are found
  6440. // See: https://github.com/beatgammit/base64-js/issues/42
  6441. var validLen = b64.indexOf('=')
  6442. if (validLen === -1) validLen = len
  6443. var placeHoldersLen = validLen === len
  6444. ? 0
  6445. : 4 - (validLen % 4)
  6446. return [validLen, placeHoldersLen]
  6447. }
  6448. // base64 is 4/3 + up to two characters of the original data
  6449. function byteLength (b64) {
  6450. var lens = getLens(b64)
  6451. var validLen = lens[0]
  6452. var placeHoldersLen = lens[1]
  6453. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  6454. }
  6455. function _byteLength (b64, validLen, placeHoldersLen) {
  6456. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  6457. }
  6458. function toByteArray (b64) {
  6459. var tmp
  6460. var lens = getLens(b64)
  6461. var validLen = lens[0]
  6462. var placeHoldersLen = lens[1]
  6463. var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
  6464. var curByte = 0
  6465. // if there are placeholders, only get up to the last complete 4 chars
  6466. var len = placeHoldersLen > 0
  6467. ? validLen - 4
  6468. : validLen
  6469. var i
  6470. for (i = 0; i < len; i += 4) {
  6471. tmp =
  6472. (revLookup[b64.charCodeAt(i)] << 18) |
  6473. (revLookup[b64.charCodeAt(i + 1)] << 12) |
  6474. (revLookup[b64.charCodeAt(i + 2)] << 6) |
  6475. revLookup[b64.charCodeAt(i + 3)]
  6476. arr[curByte++] = (tmp >> 16) & 0xFF
  6477. arr[curByte++] = (tmp >> 8) & 0xFF
  6478. arr[curByte++] = tmp & 0xFF
  6479. }
  6480. if (placeHoldersLen === 2) {
  6481. tmp =
  6482. (revLookup[b64.charCodeAt(i)] << 2) |
  6483. (revLookup[b64.charCodeAt(i + 1)] >> 4)
  6484. arr[curByte++] = tmp & 0xFF
  6485. }
  6486. if (placeHoldersLen === 1) {
  6487. tmp =
  6488. (revLookup[b64.charCodeAt(i)] << 10) |
  6489. (revLookup[b64.charCodeAt(i + 1)] << 4) |
  6490. (revLookup[b64.charCodeAt(i + 2)] >> 2)
  6491. arr[curByte++] = (tmp >> 8) & 0xFF
  6492. arr[curByte++] = tmp & 0xFF
  6493. }
  6494. return arr
  6495. }
  6496. function tripletToBase64 (num) {
  6497. return lookup[num >> 18 & 0x3F] +
  6498. lookup[num >> 12 & 0x3F] +
  6499. lookup[num >> 6 & 0x3F] +
  6500. lookup[num & 0x3F]
  6501. }
  6502. function encodeChunk (uint8, start, end) {
  6503. var tmp
  6504. var output = []
  6505. for (var i = start; i < end; i += 3) {
  6506. tmp =
  6507. ((uint8[i] << 16) & 0xFF0000) +
  6508. ((uint8[i + 1] << 8) & 0xFF00) +
  6509. (uint8[i + 2] & 0xFF)
  6510. output.push(tripletToBase64(tmp))
  6511. }
  6512. return output.join('')
  6513. }
  6514. function fromByteArray (uint8) {
  6515. var tmp
  6516. var len = uint8.length
  6517. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  6518. var parts = []
  6519. var maxChunkLength = 16383 // must be multiple of 3
  6520. // go through the array every three bytes, we'll deal with trailing stuff later
  6521. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  6522. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
  6523. }
  6524. // pad the end with zeros, but make sure to not forget the extra bytes
  6525. if (extraBytes === 1) {
  6526. tmp = uint8[len - 1]
  6527. parts.push(
  6528. lookup[tmp >> 2] +
  6529. lookup[(tmp << 4) & 0x3F] +
  6530. '=='
  6531. )
  6532. } else if (extraBytes === 2) {
  6533. tmp = (uint8[len - 2] << 8) + uint8[len - 1]
  6534. parts.push(
  6535. lookup[tmp >> 10] +
  6536. lookup[(tmp >> 4) & 0x3F] +
  6537. lookup[(tmp << 2) & 0x3F] +
  6538. '='
  6539. )
  6540. }
  6541. return parts.join('')
  6542. }
  6543. },{}],42:[function(require,module,exports){
  6544. },{}],43:[function(require,module,exports){
  6545. (function (Buffer){(function (){
  6546. /*!
  6547. * The buffer module from node.js, for the browser.
  6548. *
  6549. * @author Feross Aboukhadijeh <https://feross.org>
  6550. * @license MIT
  6551. */
  6552. /* eslint-disable no-proto */
  6553. 'use strict'
  6554. var base64 = require('base64-js')
  6555. var ieee754 = require('ieee754')
  6556. exports.Buffer = Buffer
  6557. exports.SlowBuffer = SlowBuffer
  6558. exports.INSPECT_MAX_BYTES = 50
  6559. var K_MAX_LENGTH = 0x7fffffff
  6560. exports.kMaxLength = K_MAX_LENGTH
  6561. /**
  6562. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  6563. * === true Use Uint8Array implementation (fastest)
  6564. * === false Print warning and recommend using `buffer` v4.x which has an Object
  6565. * implementation (most compatible, even IE6)
  6566. *
  6567. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  6568. * Opera 11.6+, iOS 4.2+.
  6569. *
  6570. * We report that the browser does not support typed arrays if the are not subclassable
  6571. * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
  6572. * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
  6573. * for __proto__ and has a buggy typed array implementation.
  6574. */
  6575. Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
  6576. if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
  6577. typeof console.error === 'function') {
  6578. console.error(
  6579. 'This browser lacks typed array (Uint8Array) support which is required by ' +
  6580. '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
  6581. )
  6582. }
  6583. function typedArraySupport () {
  6584. // Can typed array instances can be augmented?
  6585. try {
  6586. var arr = new Uint8Array(1)
  6587. arr.__proto__ = { __proto__: Uint8Array.prototype, foo: function () { return 42 } }
  6588. return arr.foo() === 42
  6589. } catch (e) {
  6590. return false
  6591. }
  6592. }
  6593. Object.defineProperty(Buffer.prototype, 'parent', {
  6594. enumerable: true,
  6595. get: function () {
  6596. if (!Buffer.isBuffer(this)) return undefined
  6597. return this.buffer
  6598. }
  6599. })
  6600. Object.defineProperty(Buffer.prototype, 'offset', {
  6601. enumerable: true,
  6602. get: function () {
  6603. if (!Buffer.isBuffer(this)) return undefined
  6604. return this.byteOffset
  6605. }
  6606. })
  6607. function createBuffer (length) {
  6608. if (length > K_MAX_LENGTH) {
  6609. throw new RangeError('The value "' + length + '" is invalid for option "size"')
  6610. }
  6611. // Return an augmented `Uint8Array` instance
  6612. var buf = new Uint8Array(length)
  6613. buf.__proto__ = Buffer.prototype
  6614. return buf
  6615. }
  6616. /**
  6617. * The Buffer constructor returns instances of `Uint8Array` that have their
  6618. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  6619. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  6620. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  6621. * returns a single octet.
  6622. *
  6623. * The `Uint8Array` prototype remains unmodified.
  6624. */
  6625. function Buffer (arg, encodingOrOffset, length) {
  6626. // Common case.
  6627. if (typeof arg === 'number') {
  6628. if (typeof encodingOrOffset === 'string') {
  6629. throw new TypeError(
  6630. 'The "string" argument must be of type string. Received type number'
  6631. )
  6632. }
  6633. return allocUnsafe(arg)
  6634. }
  6635. return from(arg, encodingOrOffset, length)
  6636. }
  6637. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  6638. if (typeof Symbol !== 'undefined' && Symbol.species != null &&
  6639. Buffer[Symbol.species] === Buffer) {
  6640. Object.defineProperty(Buffer, Symbol.species, {
  6641. value: null,
  6642. configurable: true,
  6643. enumerable: false,
  6644. writable: false
  6645. })
  6646. }
  6647. Buffer.poolSize = 8192 // not used by this implementation
  6648. function from (value, encodingOrOffset, length) {
  6649. if (typeof value === 'string') {
  6650. return fromString(value, encodingOrOffset)
  6651. }
  6652. if (ArrayBuffer.isView(value)) {
  6653. return fromArrayLike(value)
  6654. }
  6655. if (value == null) {
  6656. throw TypeError(
  6657. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  6658. 'or Array-like Object. Received type ' + (typeof value)
  6659. )
  6660. }
  6661. if (isInstance(value, ArrayBuffer) ||
  6662. (value && isInstance(value.buffer, ArrayBuffer))) {
  6663. return fromArrayBuffer(value, encodingOrOffset, length)
  6664. }
  6665. if (typeof value === 'number') {
  6666. throw new TypeError(
  6667. 'The "value" argument must not be of type number. Received type number'
  6668. )
  6669. }
  6670. var valueOf = value.valueOf && value.valueOf()
  6671. if (valueOf != null && valueOf !== value) {
  6672. return Buffer.from(valueOf, encodingOrOffset, length)
  6673. }
  6674. var b = fromObject(value)
  6675. if (b) return b
  6676. if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
  6677. typeof value[Symbol.toPrimitive] === 'function') {
  6678. return Buffer.from(
  6679. value[Symbol.toPrimitive]('string'), encodingOrOffset, length
  6680. )
  6681. }
  6682. throw new TypeError(
  6683. 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
  6684. 'or Array-like Object. Received type ' + (typeof value)
  6685. )
  6686. }
  6687. /**
  6688. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  6689. * if value is a number.
  6690. * Buffer.from(str[, encoding])
  6691. * Buffer.from(array)
  6692. * Buffer.from(buffer)
  6693. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  6694. **/
  6695. Buffer.from = function (value, encodingOrOffset, length) {
  6696. return from(value, encodingOrOffset, length)
  6697. }
  6698. // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
  6699. // https://github.com/feross/buffer/pull/148
  6700. Buffer.prototype.__proto__ = Uint8Array.prototype
  6701. Buffer.__proto__ = Uint8Array
  6702. function assertSize (size) {
  6703. if (typeof size !== 'number') {
  6704. throw new TypeError('"size" argument must be of type number')
  6705. } else if (size < 0) {
  6706. throw new RangeError('The value "' + size + '" is invalid for option "size"')
  6707. }
  6708. }
  6709. function alloc (size, fill, encoding) {
  6710. assertSize(size)
  6711. if (size <= 0) {
  6712. return createBuffer(size)
  6713. }
  6714. if (fill !== undefined) {
  6715. // Only pay attention to encoding if it's a string. This
  6716. // prevents accidentally sending in a number that would
  6717. // be interpretted as a start offset.
  6718. return typeof encoding === 'string'
  6719. ? createBuffer(size).fill(fill, encoding)
  6720. : createBuffer(size).fill(fill)
  6721. }
  6722. return createBuffer(size)
  6723. }
  6724. /**
  6725. * Creates a new filled Buffer instance.
  6726. * alloc(size[, fill[, encoding]])
  6727. **/
  6728. Buffer.alloc = function (size, fill, encoding) {
  6729. return alloc(size, fill, encoding)
  6730. }
  6731. function allocUnsafe (size) {
  6732. assertSize(size)
  6733. return createBuffer(size < 0 ? 0 : checked(size) | 0)
  6734. }
  6735. /**
  6736. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  6737. * */
  6738. Buffer.allocUnsafe = function (size) {
  6739. return allocUnsafe(size)
  6740. }
  6741. /**
  6742. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  6743. */
  6744. Buffer.allocUnsafeSlow = function (size) {
  6745. return allocUnsafe(size)
  6746. }
  6747. function fromString (string, encoding) {
  6748. if (typeof encoding !== 'string' || encoding === '') {
  6749. encoding = 'utf8'
  6750. }
  6751. if (!Buffer.isEncoding(encoding)) {
  6752. throw new TypeError('Unknown encoding: ' + encoding)
  6753. }
  6754. var length = byteLength(string, encoding) | 0
  6755. var buf = createBuffer(length)
  6756. var actual = buf.write(string, encoding)
  6757. if (actual !== length) {
  6758. // Writing a hex string, for example, that contains invalid characters will
  6759. // cause everything after the first invalid character to be ignored. (e.g.
  6760. // 'abxxcd' will be treated as 'ab')
  6761. buf = buf.slice(0, actual)
  6762. }
  6763. return buf
  6764. }
  6765. function fromArrayLike (array) {
  6766. var length = array.length < 0 ? 0 : checked(array.length) | 0
  6767. var buf = createBuffer(length)
  6768. for (var i = 0; i < length; i += 1) {
  6769. buf[i] = array[i] & 255
  6770. }
  6771. return buf
  6772. }
  6773. function fromArrayBuffer (array, byteOffset, length) {
  6774. if (byteOffset < 0 || array.byteLength < byteOffset) {
  6775. throw new RangeError('"offset" is outside of buffer bounds')
  6776. }
  6777. if (array.byteLength < byteOffset + (length || 0)) {
  6778. throw new RangeError('"length" is outside of buffer bounds')
  6779. }
  6780. var buf
  6781. if (byteOffset === undefined && length === undefined) {
  6782. buf = new Uint8Array(array)
  6783. } else if (length === undefined) {
  6784. buf = new Uint8Array(array, byteOffset)
  6785. } else {
  6786. buf = new Uint8Array(array, byteOffset, length)
  6787. }
  6788. // Return an augmented `Uint8Array` instance
  6789. buf.__proto__ = Buffer.prototype
  6790. return buf
  6791. }
  6792. function fromObject (obj) {
  6793. if (Buffer.isBuffer(obj)) {
  6794. var len = checked(obj.length) | 0
  6795. var buf = createBuffer(len)
  6796. if (buf.length === 0) {
  6797. return buf
  6798. }
  6799. obj.copy(buf, 0, 0, len)
  6800. return buf
  6801. }
  6802. if (obj.length !== undefined) {
  6803. if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
  6804. return createBuffer(0)
  6805. }
  6806. return fromArrayLike(obj)
  6807. }
  6808. if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
  6809. return fromArrayLike(obj.data)
  6810. }
  6811. }
  6812. function checked (length) {
  6813. // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
  6814. // length is NaN (which is otherwise coerced to zero.)
  6815. if (length >= K_MAX_LENGTH) {
  6816. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  6817. 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
  6818. }
  6819. return length | 0
  6820. }
  6821. function SlowBuffer (length) {
  6822. if (+length != length) { // eslint-disable-line eqeqeq
  6823. length = 0
  6824. }
  6825. return Buffer.alloc(+length)
  6826. }
  6827. Buffer.isBuffer = function isBuffer (b) {
  6828. return b != null && b._isBuffer === true &&
  6829. b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
  6830. }
  6831. Buffer.compare = function compare (a, b) {
  6832. if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
  6833. if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
  6834. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  6835. throw new TypeError(
  6836. 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
  6837. )
  6838. }
  6839. if (a === b) return 0
  6840. var x = a.length
  6841. var y = b.length
  6842. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  6843. if (a[i] !== b[i]) {
  6844. x = a[i]
  6845. y = b[i]
  6846. break
  6847. }
  6848. }
  6849. if (x < y) return -1
  6850. if (y < x) return 1
  6851. return 0
  6852. }
  6853. Buffer.isEncoding = function isEncoding (encoding) {
  6854. switch (String(encoding).toLowerCase()) {
  6855. case 'hex':
  6856. case 'utf8':
  6857. case 'utf-8':
  6858. case 'ascii':
  6859. case 'latin1':
  6860. case 'binary':
  6861. case 'base64':
  6862. case 'ucs2':
  6863. case 'ucs-2':
  6864. case 'utf16le':
  6865. case 'utf-16le':
  6866. return true
  6867. default:
  6868. return false
  6869. }
  6870. }
  6871. Buffer.concat = function concat (list, length) {
  6872. if (!Array.isArray(list)) {
  6873. throw new TypeError('"list" argument must be an Array of Buffers')
  6874. }
  6875. if (list.length === 0) {
  6876. return Buffer.alloc(0)
  6877. }
  6878. var i
  6879. if (length === undefined) {
  6880. length = 0
  6881. for (i = 0; i < list.length; ++i) {
  6882. length += list[i].length
  6883. }
  6884. }
  6885. var buffer = Buffer.allocUnsafe(length)
  6886. var pos = 0
  6887. for (i = 0; i < list.length; ++i) {
  6888. var buf = list[i]
  6889. if (isInstance(buf, Uint8Array)) {
  6890. buf = Buffer.from(buf)
  6891. }
  6892. if (!Buffer.isBuffer(buf)) {
  6893. throw new TypeError('"list" argument must be an Array of Buffers')
  6894. }
  6895. buf.copy(buffer, pos)
  6896. pos += buf.length
  6897. }
  6898. return buffer
  6899. }
  6900. function byteLength (string, encoding) {
  6901. if (Buffer.isBuffer(string)) {
  6902. return string.length
  6903. }
  6904. if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
  6905. return string.byteLength
  6906. }
  6907. if (typeof string !== 'string') {
  6908. throw new TypeError(
  6909. 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
  6910. 'Received type ' + typeof string
  6911. )
  6912. }
  6913. var len = string.length
  6914. var mustMatch = (arguments.length > 2 && arguments[2] === true)
  6915. if (!mustMatch && len === 0) return 0
  6916. // Use a for loop to avoid recursion
  6917. var loweredCase = false
  6918. for (;;) {
  6919. switch (encoding) {
  6920. case 'ascii':
  6921. case 'latin1':
  6922. case 'binary':
  6923. return len
  6924. case 'utf8':
  6925. case 'utf-8':
  6926. return utf8ToBytes(string).length
  6927. case 'ucs2':
  6928. case 'ucs-2':
  6929. case 'utf16le':
  6930. case 'utf-16le':
  6931. return len * 2
  6932. case 'hex':
  6933. return len >>> 1
  6934. case 'base64':
  6935. return base64ToBytes(string).length
  6936. default:
  6937. if (loweredCase) {
  6938. return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
  6939. }
  6940. encoding = ('' + encoding).toLowerCase()
  6941. loweredCase = true
  6942. }
  6943. }
  6944. }
  6945. Buffer.byteLength = byteLength
  6946. function slowToString (encoding, start, end) {
  6947. var loweredCase = false
  6948. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  6949. // property of a typed array.
  6950. // This behaves neither like String nor Uint8Array in that we set start/end
  6951. // to their upper/lower bounds if the value passed is out of range.
  6952. // undefined is handled specially as per ECMA-262 6th Edition,
  6953. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  6954. if (start === undefined || start < 0) {
  6955. start = 0
  6956. }
  6957. // Return early if start > this.length. Done here to prevent potential uint32
  6958. // coercion fail below.
  6959. if (start > this.length) {
  6960. return ''
  6961. }
  6962. if (end === undefined || end > this.length) {
  6963. end = this.length
  6964. }
  6965. if (end <= 0) {
  6966. return ''
  6967. }
  6968. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  6969. end >>>= 0
  6970. start >>>= 0
  6971. if (end <= start) {
  6972. return ''
  6973. }
  6974. if (!encoding) encoding = 'utf8'
  6975. while (true) {
  6976. switch (encoding) {
  6977. case 'hex':
  6978. return hexSlice(this, start, end)
  6979. case 'utf8':
  6980. case 'utf-8':
  6981. return utf8Slice(this, start, end)
  6982. case 'ascii':
  6983. return asciiSlice(this, start, end)
  6984. case 'latin1':
  6985. case 'binary':
  6986. return latin1Slice(this, start, end)
  6987. case 'base64':
  6988. return base64Slice(this, start, end)
  6989. case 'ucs2':
  6990. case 'ucs-2':
  6991. case 'utf16le':
  6992. case 'utf-16le':
  6993. return utf16leSlice(this, start, end)
  6994. default:
  6995. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  6996. encoding = (encoding + '').toLowerCase()
  6997. loweredCase = true
  6998. }
  6999. }
  7000. }
  7001. // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
  7002. // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
  7003. // reliably in a browserify context because there could be multiple different
  7004. // copies of the 'buffer' package in use. This method works even for Buffer
  7005. // instances that were created from another copy of the `buffer` package.
  7006. // See: https://github.com/feross/buffer/issues/154
  7007. Buffer.prototype._isBuffer = true
  7008. function swap (b, n, m) {
  7009. var i = b[n]
  7010. b[n] = b[m]
  7011. b[m] = i
  7012. }
  7013. Buffer.prototype.swap16 = function swap16 () {
  7014. var len = this.length
  7015. if (len % 2 !== 0) {
  7016. throw new RangeError('Buffer size must be a multiple of 16-bits')
  7017. }
  7018. for (var i = 0; i < len; i += 2) {
  7019. swap(this, i, i + 1)
  7020. }
  7021. return this
  7022. }
  7023. Buffer.prototype.swap32 = function swap32 () {
  7024. var len = this.length
  7025. if (len % 4 !== 0) {
  7026. throw new RangeError('Buffer size must be a multiple of 32-bits')
  7027. }
  7028. for (var i = 0; i < len; i += 4) {
  7029. swap(this, i, i + 3)
  7030. swap(this, i + 1, i + 2)
  7031. }
  7032. return this
  7033. }
  7034. Buffer.prototype.swap64 = function swap64 () {
  7035. var len = this.length
  7036. if (len % 8 !== 0) {
  7037. throw new RangeError('Buffer size must be a multiple of 64-bits')
  7038. }
  7039. for (var i = 0; i < len; i += 8) {
  7040. swap(this, i, i + 7)
  7041. swap(this, i + 1, i + 6)
  7042. swap(this, i + 2, i + 5)
  7043. swap(this, i + 3, i + 4)
  7044. }
  7045. return this
  7046. }
  7047. Buffer.prototype.toString = function toString () {
  7048. var length = this.length
  7049. if (length === 0) return ''
  7050. if (arguments.length === 0) return utf8Slice(this, 0, length)
  7051. return slowToString.apply(this, arguments)
  7052. }
  7053. Buffer.prototype.toLocaleString = Buffer.prototype.toString
  7054. Buffer.prototype.equals = function equals (b) {
  7055. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  7056. if (this === b) return true
  7057. return Buffer.compare(this, b) === 0
  7058. }
  7059. Buffer.prototype.inspect = function inspect () {
  7060. var str = ''
  7061. var max = exports.INSPECT_MAX_BYTES
  7062. str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
  7063. if (this.length > max) str += ' ... '
  7064. return '<Buffer ' + str + '>'
  7065. }
  7066. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  7067. if (isInstance(target, Uint8Array)) {
  7068. target = Buffer.from(target, target.offset, target.byteLength)
  7069. }
  7070. if (!Buffer.isBuffer(target)) {
  7071. throw new TypeError(
  7072. 'The "target" argument must be one of type Buffer or Uint8Array. ' +
  7073. 'Received type ' + (typeof target)
  7074. )
  7075. }
  7076. if (start === undefined) {
  7077. start = 0
  7078. }
  7079. if (end === undefined) {
  7080. end = target ? target.length : 0
  7081. }
  7082. if (thisStart === undefined) {
  7083. thisStart = 0
  7084. }
  7085. if (thisEnd === undefined) {
  7086. thisEnd = this.length
  7087. }
  7088. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  7089. throw new RangeError('out of range index')
  7090. }
  7091. if (thisStart >= thisEnd && start >= end) {
  7092. return 0
  7093. }
  7094. if (thisStart >= thisEnd) {
  7095. return -1
  7096. }
  7097. if (start >= end) {
  7098. return 1
  7099. }
  7100. start >>>= 0
  7101. end >>>= 0
  7102. thisStart >>>= 0
  7103. thisEnd >>>= 0
  7104. if (this === target) return 0
  7105. var x = thisEnd - thisStart
  7106. var y = end - start
  7107. var len = Math.min(x, y)
  7108. var thisCopy = this.slice(thisStart, thisEnd)
  7109. var targetCopy = target.slice(start, end)
  7110. for (var i = 0; i < len; ++i) {
  7111. if (thisCopy[i] !== targetCopy[i]) {
  7112. x = thisCopy[i]
  7113. y = targetCopy[i]
  7114. break
  7115. }
  7116. }
  7117. if (x < y) return -1
  7118. if (y < x) return 1
  7119. return 0
  7120. }
  7121. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  7122. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  7123. //
  7124. // Arguments:
  7125. // - buffer - a Buffer to search
  7126. // - val - a string, Buffer, or number
  7127. // - byteOffset - an index into `buffer`; will be clamped to an int32
  7128. // - encoding - an optional encoding, relevant is val is a string
  7129. // - dir - true for indexOf, false for lastIndexOf
  7130. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  7131. // Empty buffer means no match
  7132. if (buffer.length === 0) return -1
  7133. // Normalize byteOffset
  7134. if (typeof byteOffset === 'string') {
  7135. encoding = byteOffset
  7136. byteOffset = 0
  7137. } else if (byteOffset > 0x7fffffff) {
  7138. byteOffset = 0x7fffffff
  7139. } else if (byteOffset < -0x80000000) {
  7140. byteOffset = -0x80000000
  7141. }
  7142. byteOffset = +byteOffset // Coerce to Number.
  7143. if (numberIsNaN(byteOffset)) {
  7144. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  7145. byteOffset = dir ? 0 : (buffer.length - 1)
  7146. }
  7147. // Normalize byteOffset: negative offsets start from the end of the buffer
  7148. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  7149. if (byteOffset >= buffer.length) {
  7150. if (dir) return -1
  7151. else byteOffset = buffer.length - 1
  7152. } else if (byteOffset < 0) {
  7153. if (dir) byteOffset = 0
  7154. else return -1
  7155. }
  7156. // Normalize val
  7157. if (typeof val === 'string') {
  7158. val = Buffer.from(val, encoding)
  7159. }
  7160. // Finally, search either indexOf (if dir is true) or lastIndexOf
  7161. if (Buffer.isBuffer(val)) {
  7162. // Special case: looking for empty string/buffer always fails
  7163. if (val.length === 0) {
  7164. return -1
  7165. }
  7166. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  7167. } else if (typeof val === 'number') {
  7168. val = val & 0xFF // Search for a byte value [0-255]
  7169. if (typeof Uint8Array.prototype.indexOf === 'function') {
  7170. if (dir) {
  7171. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  7172. } else {
  7173. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  7174. }
  7175. }
  7176. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  7177. }
  7178. throw new TypeError('val must be string, number or Buffer')
  7179. }
  7180. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  7181. var indexSize = 1
  7182. var arrLength = arr.length
  7183. var valLength = val.length
  7184. if (encoding !== undefined) {
  7185. encoding = String(encoding).toLowerCase()
  7186. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  7187. encoding === 'utf16le' || encoding === 'utf-16le') {
  7188. if (arr.length < 2 || val.length < 2) {
  7189. return -1
  7190. }
  7191. indexSize = 2
  7192. arrLength /= 2
  7193. valLength /= 2
  7194. byteOffset /= 2
  7195. }
  7196. }
  7197. function read (buf, i) {
  7198. if (indexSize === 1) {
  7199. return buf[i]
  7200. } else {
  7201. return buf.readUInt16BE(i * indexSize)
  7202. }
  7203. }
  7204. var i
  7205. if (dir) {
  7206. var foundIndex = -1
  7207. for (i = byteOffset; i < arrLength; i++) {
  7208. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  7209. if (foundIndex === -1) foundIndex = i
  7210. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  7211. } else {
  7212. if (foundIndex !== -1) i -= i - foundIndex
  7213. foundIndex = -1
  7214. }
  7215. }
  7216. } else {
  7217. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  7218. for (i = byteOffset; i >= 0; i--) {
  7219. var found = true
  7220. for (var j = 0; j < valLength; j++) {
  7221. if (read(arr, i + j) !== read(val, j)) {
  7222. found = false
  7223. break
  7224. }
  7225. }
  7226. if (found) return i
  7227. }
  7228. }
  7229. return -1
  7230. }
  7231. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  7232. return this.indexOf(val, byteOffset, encoding) !== -1
  7233. }
  7234. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  7235. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  7236. }
  7237. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  7238. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  7239. }
  7240. function hexWrite (buf, string, offset, length) {
  7241. offset = Number(offset) || 0
  7242. var remaining = buf.length - offset
  7243. if (!length) {
  7244. length = remaining
  7245. } else {
  7246. length = Number(length)
  7247. if (length > remaining) {
  7248. length = remaining
  7249. }
  7250. }
  7251. var strLen = string.length
  7252. if (length > strLen / 2) {
  7253. length = strLen / 2
  7254. }
  7255. for (var i = 0; i < length; ++i) {
  7256. var parsed = parseInt(string.substr(i * 2, 2), 16)
  7257. if (numberIsNaN(parsed)) return i
  7258. buf[offset + i] = parsed
  7259. }
  7260. return i
  7261. }
  7262. function utf8Write (buf, string, offset, length) {
  7263. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  7264. }
  7265. function asciiWrite (buf, string, offset, length) {
  7266. return blitBuffer(asciiToBytes(string), buf, offset, length)
  7267. }
  7268. function latin1Write (buf, string, offset, length) {
  7269. return asciiWrite(buf, string, offset, length)
  7270. }
  7271. function base64Write (buf, string, offset, length) {
  7272. return blitBuffer(base64ToBytes(string), buf, offset, length)
  7273. }
  7274. function ucs2Write (buf, string, offset, length) {
  7275. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  7276. }
  7277. Buffer.prototype.write = function write (string, offset, length, encoding) {
  7278. // Buffer#write(string)
  7279. if (offset === undefined) {
  7280. encoding = 'utf8'
  7281. length = this.length
  7282. offset = 0
  7283. // Buffer#write(string, encoding)
  7284. } else if (length === undefined && typeof offset === 'string') {
  7285. encoding = offset
  7286. length = this.length
  7287. offset = 0
  7288. // Buffer#write(string, offset[, length][, encoding])
  7289. } else if (isFinite(offset)) {
  7290. offset = offset >>> 0
  7291. if (isFinite(length)) {
  7292. length = length >>> 0
  7293. if (encoding === undefined) encoding = 'utf8'
  7294. } else {
  7295. encoding = length
  7296. length = undefined
  7297. }
  7298. } else {
  7299. throw new Error(
  7300. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  7301. )
  7302. }
  7303. var remaining = this.length - offset
  7304. if (length === undefined || length > remaining) length = remaining
  7305. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  7306. throw new RangeError('Attempt to write outside buffer bounds')
  7307. }
  7308. if (!encoding) encoding = 'utf8'
  7309. var loweredCase = false
  7310. for (;;) {
  7311. switch (encoding) {
  7312. case 'hex':
  7313. return hexWrite(this, string, offset, length)
  7314. case 'utf8':
  7315. case 'utf-8':
  7316. return utf8Write(this, string, offset, length)
  7317. case 'ascii':
  7318. return asciiWrite(this, string, offset, length)
  7319. case 'latin1':
  7320. case 'binary':
  7321. return latin1Write(this, string, offset, length)
  7322. case 'base64':
  7323. // Warning: maxLength not taken into account in base64Write
  7324. return base64Write(this, string, offset, length)
  7325. case 'ucs2':
  7326. case 'ucs-2':
  7327. case 'utf16le':
  7328. case 'utf-16le':
  7329. return ucs2Write(this, string, offset, length)
  7330. default:
  7331. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  7332. encoding = ('' + encoding).toLowerCase()
  7333. loweredCase = true
  7334. }
  7335. }
  7336. }
  7337. Buffer.prototype.toJSON = function toJSON () {
  7338. return {
  7339. type: 'Buffer',
  7340. data: Array.prototype.slice.call(this._arr || this, 0)
  7341. }
  7342. }
  7343. function base64Slice (buf, start, end) {
  7344. if (start === 0 && end === buf.length) {
  7345. return base64.fromByteArray(buf)
  7346. } else {
  7347. return base64.fromByteArray(buf.slice(start, end))
  7348. }
  7349. }
  7350. function utf8Slice (buf, start, end) {
  7351. end = Math.min(buf.length, end)
  7352. var res = []
  7353. var i = start
  7354. while (i < end) {
  7355. var firstByte = buf[i]
  7356. var codePoint = null
  7357. var bytesPerSequence = (firstByte > 0xEF) ? 4
  7358. : (firstByte > 0xDF) ? 3
  7359. : (firstByte > 0xBF) ? 2
  7360. : 1
  7361. if (i + bytesPerSequence <= end) {
  7362. var secondByte, thirdByte, fourthByte, tempCodePoint
  7363. switch (bytesPerSequence) {
  7364. case 1:
  7365. if (firstByte < 0x80) {
  7366. codePoint = firstByte
  7367. }
  7368. break
  7369. case 2:
  7370. secondByte = buf[i + 1]
  7371. if ((secondByte & 0xC0) === 0x80) {
  7372. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  7373. if (tempCodePoint > 0x7F) {
  7374. codePoint = tempCodePoint
  7375. }
  7376. }
  7377. break
  7378. case 3:
  7379. secondByte = buf[i + 1]
  7380. thirdByte = buf[i + 2]
  7381. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  7382. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  7383. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  7384. codePoint = tempCodePoint
  7385. }
  7386. }
  7387. break
  7388. case 4:
  7389. secondByte = buf[i + 1]
  7390. thirdByte = buf[i + 2]
  7391. fourthByte = buf[i + 3]
  7392. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  7393. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  7394. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  7395. codePoint = tempCodePoint
  7396. }
  7397. }
  7398. }
  7399. }
  7400. if (codePoint === null) {
  7401. // we did not generate a valid codePoint so insert a
  7402. // replacement char (U+FFFD) and advance only 1 byte
  7403. codePoint = 0xFFFD
  7404. bytesPerSequence = 1
  7405. } else if (codePoint > 0xFFFF) {
  7406. // encode to utf16 (surrogate pair dance)
  7407. codePoint -= 0x10000
  7408. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  7409. codePoint = 0xDC00 | codePoint & 0x3FF
  7410. }
  7411. res.push(codePoint)
  7412. i += bytesPerSequence
  7413. }
  7414. return decodeCodePointsArray(res)
  7415. }
  7416. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  7417. // the lowest limit is Chrome, with 0x10000 args.
  7418. // We go 1 magnitude less, for safety
  7419. var MAX_ARGUMENTS_LENGTH = 0x1000
  7420. function decodeCodePointsArray (codePoints) {
  7421. var len = codePoints.length
  7422. if (len <= MAX_ARGUMENTS_LENGTH) {
  7423. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  7424. }
  7425. // Decode in chunks to avoid "call stack size exceeded".
  7426. var res = ''
  7427. var i = 0
  7428. while (i < len) {
  7429. res += String.fromCharCode.apply(
  7430. String,
  7431. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  7432. )
  7433. }
  7434. return res
  7435. }
  7436. function asciiSlice (buf, start, end) {
  7437. var ret = ''
  7438. end = Math.min(buf.length, end)
  7439. for (var i = start; i < end; ++i) {
  7440. ret += String.fromCharCode(buf[i] & 0x7F)
  7441. }
  7442. return ret
  7443. }
  7444. function latin1Slice (buf, start, end) {
  7445. var ret = ''
  7446. end = Math.min(buf.length, end)
  7447. for (var i = start; i < end; ++i) {
  7448. ret += String.fromCharCode(buf[i])
  7449. }
  7450. return ret
  7451. }
  7452. function hexSlice (buf, start, end) {
  7453. var len = buf.length
  7454. if (!start || start < 0) start = 0
  7455. if (!end || end < 0 || end > len) end = len
  7456. var out = ''
  7457. for (var i = start; i < end; ++i) {
  7458. out += toHex(buf[i])
  7459. }
  7460. return out
  7461. }
  7462. function utf16leSlice (buf, start, end) {
  7463. var bytes = buf.slice(start, end)
  7464. var res = ''
  7465. for (var i = 0; i < bytes.length; i += 2) {
  7466. res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
  7467. }
  7468. return res
  7469. }
  7470. Buffer.prototype.slice = function slice (start, end) {
  7471. var len = this.length
  7472. start = ~~start
  7473. end = end === undefined ? len : ~~end
  7474. if (start < 0) {
  7475. start += len
  7476. if (start < 0) start = 0
  7477. } else if (start > len) {
  7478. start = len
  7479. }
  7480. if (end < 0) {
  7481. end += len
  7482. if (end < 0) end = 0
  7483. } else if (end > len) {
  7484. end = len
  7485. }
  7486. if (end < start) end = start
  7487. var newBuf = this.subarray(start, end)
  7488. // Return an augmented `Uint8Array` instance
  7489. newBuf.__proto__ = Buffer.prototype
  7490. return newBuf
  7491. }
  7492. /*
  7493. * Need to make sure that buffer isn't trying to write out of bounds.
  7494. */
  7495. function checkOffset (offset, ext, length) {
  7496. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  7497. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  7498. }
  7499. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  7500. offset = offset >>> 0
  7501. byteLength = byteLength >>> 0
  7502. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7503. var val = this[offset]
  7504. var mul = 1
  7505. var i = 0
  7506. while (++i < byteLength && (mul *= 0x100)) {
  7507. val += this[offset + i] * mul
  7508. }
  7509. return val
  7510. }
  7511. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  7512. offset = offset >>> 0
  7513. byteLength = byteLength >>> 0
  7514. if (!noAssert) {
  7515. checkOffset(offset, byteLength, this.length)
  7516. }
  7517. var val = this[offset + --byteLength]
  7518. var mul = 1
  7519. while (byteLength > 0 && (mul *= 0x100)) {
  7520. val += this[offset + --byteLength] * mul
  7521. }
  7522. return val
  7523. }
  7524. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  7525. offset = offset >>> 0
  7526. if (!noAssert) checkOffset(offset, 1, this.length)
  7527. return this[offset]
  7528. }
  7529. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  7530. offset = offset >>> 0
  7531. if (!noAssert) checkOffset(offset, 2, this.length)
  7532. return this[offset] | (this[offset + 1] << 8)
  7533. }
  7534. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  7535. offset = offset >>> 0
  7536. if (!noAssert) checkOffset(offset, 2, this.length)
  7537. return (this[offset] << 8) | this[offset + 1]
  7538. }
  7539. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  7540. offset = offset >>> 0
  7541. if (!noAssert) checkOffset(offset, 4, this.length)
  7542. return ((this[offset]) |
  7543. (this[offset + 1] << 8) |
  7544. (this[offset + 2] << 16)) +
  7545. (this[offset + 3] * 0x1000000)
  7546. }
  7547. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  7548. offset = offset >>> 0
  7549. if (!noAssert) checkOffset(offset, 4, this.length)
  7550. return (this[offset] * 0x1000000) +
  7551. ((this[offset + 1] << 16) |
  7552. (this[offset + 2] << 8) |
  7553. this[offset + 3])
  7554. }
  7555. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  7556. offset = offset >>> 0
  7557. byteLength = byteLength >>> 0
  7558. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7559. var val = this[offset]
  7560. var mul = 1
  7561. var i = 0
  7562. while (++i < byteLength && (mul *= 0x100)) {
  7563. val += this[offset + i] * mul
  7564. }
  7565. mul *= 0x80
  7566. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  7567. return val
  7568. }
  7569. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  7570. offset = offset >>> 0
  7571. byteLength = byteLength >>> 0
  7572. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7573. var i = byteLength
  7574. var mul = 1
  7575. var val = this[offset + --i]
  7576. while (i > 0 && (mul *= 0x100)) {
  7577. val += this[offset + --i] * mul
  7578. }
  7579. mul *= 0x80
  7580. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  7581. return val
  7582. }
  7583. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  7584. offset = offset >>> 0
  7585. if (!noAssert) checkOffset(offset, 1, this.length)
  7586. if (!(this[offset] & 0x80)) return (this[offset])
  7587. return ((0xff - this[offset] + 1) * -1)
  7588. }
  7589. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  7590. offset = offset >>> 0
  7591. if (!noAssert) checkOffset(offset, 2, this.length)
  7592. var val = this[offset] | (this[offset + 1] << 8)
  7593. return (val & 0x8000) ? val | 0xFFFF0000 : val
  7594. }
  7595. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  7596. offset = offset >>> 0
  7597. if (!noAssert) checkOffset(offset, 2, this.length)
  7598. var val = this[offset + 1] | (this[offset] << 8)
  7599. return (val & 0x8000) ? val | 0xFFFF0000 : val
  7600. }
  7601. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  7602. offset = offset >>> 0
  7603. if (!noAssert) checkOffset(offset, 4, this.length)
  7604. return (this[offset]) |
  7605. (this[offset + 1] << 8) |
  7606. (this[offset + 2] << 16) |
  7607. (this[offset + 3] << 24)
  7608. }
  7609. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  7610. offset = offset >>> 0
  7611. if (!noAssert) checkOffset(offset, 4, this.length)
  7612. return (this[offset] << 24) |
  7613. (this[offset + 1] << 16) |
  7614. (this[offset + 2] << 8) |
  7615. (this[offset + 3])
  7616. }
  7617. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  7618. offset = offset >>> 0
  7619. if (!noAssert) checkOffset(offset, 4, this.length)
  7620. return ieee754.read(this, offset, true, 23, 4)
  7621. }
  7622. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  7623. offset = offset >>> 0
  7624. if (!noAssert) checkOffset(offset, 4, this.length)
  7625. return ieee754.read(this, offset, false, 23, 4)
  7626. }
  7627. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  7628. offset = offset >>> 0
  7629. if (!noAssert) checkOffset(offset, 8, this.length)
  7630. return ieee754.read(this, offset, true, 52, 8)
  7631. }
  7632. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  7633. offset = offset >>> 0
  7634. if (!noAssert) checkOffset(offset, 8, this.length)
  7635. return ieee754.read(this, offset, false, 52, 8)
  7636. }
  7637. function checkInt (buf, value, offset, ext, max, min) {
  7638. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  7639. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  7640. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  7641. }
  7642. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  7643. value = +value
  7644. offset = offset >>> 0
  7645. byteLength = byteLength >>> 0
  7646. if (!noAssert) {
  7647. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  7648. checkInt(this, value, offset, byteLength, maxBytes, 0)
  7649. }
  7650. var mul = 1
  7651. var i = 0
  7652. this[offset] = value & 0xFF
  7653. while (++i < byteLength && (mul *= 0x100)) {
  7654. this[offset + i] = (value / mul) & 0xFF
  7655. }
  7656. return offset + byteLength
  7657. }
  7658. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  7659. value = +value
  7660. offset = offset >>> 0
  7661. byteLength = byteLength >>> 0
  7662. if (!noAssert) {
  7663. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  7664. checkInt(this, value, offset, byteLength, maxBytes, 0)
  7665. }
  7666. var i = byteLength - 1
  7667. var mul = 1
  7668. this[offset + i] = value & 0xFF
  7669. while (--i >= 0 && (mul *= 0x100)) {
  7670. this[offset + i] = (value / mul) & 0xFF
  7671. }
  7672. return offset + byteLength
  7673. }
  7674. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  7675. value = +value
  7676. offset = offset >>> 0
  7677. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  7678. this[offset] = (value & 0xff)
  7679. return offset + 1
  7680. }
  7681. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  7682. value = +value
  7683. offset = offset >>> 0
  7684. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  7685. this[offset] = (value & 0xff)
  7686. this[offset + 1] = (value >>> 8)
  7687. return offset + 2
  7688. }
  7689. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  7690. value = +value
  7691. offset = offset >>> 0
  7692. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  7693. this[offset] = (value >>> 8)
  7694. this[offset + 1] = (value & 0xff)
  7695. return offset + 2
  7696. }
  7697. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  7698. value = +value
  7699. offset = offset >>> 0
  7700. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  7701. this[offset + 3] = (value >>> 24)
  7702. this[offset + 2] = (value >>> 16)
  7703. this[offset + 1] = (value >>> 8)
  7704. this[offset] = (value & 0xff)
  7705. return offset + 4
  7706. }
  7707. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  7708. value = +value
  7709. offset = offset >>> 0
  7710. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  7711. this[offset] = (value >>> 24)
  7712. this[offset + 1] = (value >>> 16)
  7713. this[offset + 2] = (value >>> 8)
  7714. this[offset + 3] = (value & 0xff)
  7715. return offset + 4
  7716. }
  7717. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  7718. value = +value
  7719. offset = offset >>> 0
  7720. if (!noAssert) {
  7721. var limit = Math.pow(2, (8 * byteLength) - 1)
  7722. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  7723. }
  7724. var i = 0
  7725. var mul = 1
  7726. var sub = 0
  7727. this[offset] = value & 0xFF
  7728. while (++i < byteLength && (mul *= 0x100)) {
  7729. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  7730. sub = 1
  7731. }
  7732. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  7733. }
  7734. return offset + byteLength
  7735. }
  7736. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  7737. value = +value
  7738. offset = offset >>> 0
  7739. if (!noAssert) {
  7740. var limit = Math.pow(2, (8 * byteLength) - 1)
  7741. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  7742. }
  7743. var i = byteLength - 1
  7744. var mul = 1
  7745. var sub = 0
  7746. this[offset + i] = value & 0xFF
  7747. while (--i >= 0 && (mul *= 0x100)) {
  7748. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  7749. sub = 1
  7750. }
  7751. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  7752. }
  7753. return offset + byteLength
  7754. }
  7755. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  7756. value = +value
  7757. offset = offset >>> 0
  7758. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  7759. if (value < 0) value = 0xff + value + 1
  7760. this[offset] = (value & 0xff)
  7761. return offset + 1
  7762. }
  7763. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  7764. value = +value
  7765. offset = offset >>> 0
  7766. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  7767. this[offset] = (value & 0xff)
  7768. this[offset + 1] = (value >>> 8)
  7769. return offset + 2
  7770. }
  7771. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  7772. value = +value
  7773. offset = offset >>> 0
  7774. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  7775. this[offset] = (value >>> 8)
  7776. this[offset + 1] = (value & 0xff)
  7777. return offset + 2
  7778. }
  7779. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  7780. value = +value
  7781. offset = offset >>> 0
  7782. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  7783. this[offset] = (value & 0xff)
  7784. this[offset + 1] = (value >>> 8)
  7785. this[offset + 2] = (value >>> 16)
  7786. this[offset + 3] = (value >>> 24)
  7787. return offset + 4
  7788. }
  7789. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  7790. value = +value
  7791. offset = offset >>> 0
  7792. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  7793. if (value < 0) value = 0xffffffff + value + 1
  7794. this[offset] = (value >>> 24)
  7795. this[offset + 1] = (value >>> 16)
  7796. this[offset + 2] = (value >>> 8)
  7797. this[offset + 3] = (value & 0xff)
  7798. return offset + 4
  7799. }
  7800. function checkIEEE754 (buf, value, offset, ext, max, min) {
  7801. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  7802. if (offset < 0) throw new RangeError('Index out of range')
  7803. }
  7804. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  7805. value = +value
  7806. offset = offset >>> 0
  7807. if (!noAssert) {
  7808. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  7809. }
  7810. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  7811. return offset + 4
  7812. }
  7813. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  7814. return writeFloat(this, value, offset, true, noAssert)
  7815. }
  7816. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  7817. return writeFloat(this, value, offset, false, noAssert)
  7818. }
  7819. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  7820. value = +value
  7821. offset = offset >>> 0
  7822. if (!noAssert) {
  7823. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  7824. }
  7825. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  7826. return offset + 8
  7827. }
  7828. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  7829. return writeDouble(this, value, offset, true, noAssert)
  7830. }
  7831. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  7832. return writeDouble(this, value, offset, false, noAssert)
  7833. }
  7834. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  7835. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  7836. if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
  7837. if (!start) start = 0
  7838. if (!end && end !== 0) end = this.length
  7839. if (targetStart >= target.length) targetStart = target.length
  7840. if (!targetStart) targetStart = 0
  7841. if (end > 0 && end < start) end = start
  7842. // Copy 0 bytes; we're done
  7843. if (end === start) return 0
  7844. if (target.length === 0 || this.length === 0) return 0
  7845. // Fatal error conditions
  7846. if (targetStart < 0) {
  7847. throw new RangeError('targetStart out of bounds')
  7848. }
  7849. if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
  7850. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  7851. // Are we oob?
  7852. if (end > this.length) end = this.length
  7853. if (target.length - targetStart < end - start) {
  7854. end = target.length - targetStart + start
  7855. }
  7856. var len = end - start
  7857. if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
  7858. // Use built-in when available, missing from IE11
  7859. this.copyWithin(targetStart, start, end)
  7860. } else if (this === target && start < targetStart && targetStart < end) {
  7861. // descending copy from end
  7862. for (var i = len - 1; i >= 0; --i) {
  7863. target[i + targetStart] = this[i + start]
  7864. }
  7865. } else {
  7866. Uint8Array.prototype.set.call(
  7867. target,
  7868. this.subarray(start, end),
  7869. targetStart
  7870. )
  7871. }
  7872. return len
  7873. }
  7874. // Usage:
  7875. // buffer.fill(number[, offset[, end]])
  7876. // buffer.fill(buffer[, offset[, end]])
  7877. // buffer.fill(string[, offset[, end]][, encoding])
  7878. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  7879. // Handle string cases:
  7880. if (typeof val === 'string') {
  7881. if (typeof start === 'string') {
  7882. encoding = start
  7883. start = 0
  7884. end = this.length
  7885. } else if (typeof end === 'string') {
  7886. encoding = end
  7887. end = this.length
  7888. }
  7889. if (encoding !== undefined && typeof encoding !== 'string') {
  7890. throw new TypeError('encoding must be a string')
  7891. }
  7892. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  7893. throw new TypeError('Unknown encoding: ' + encoding)
  7894. }
  7895. if (val.length === 1) {
  7896. var code = val.charCodeAt(0)
  7897. if ((encoding === 'utf8' && code < 128) ||
  7898. encoding === 'latin1') {
  7899. // Fast path: If `val` fits into a single byte, use that numeric value.
  7900. val = code
  7901. }
  7902. }
  7903. } else if (typeof val === 'number') {
  7904. val = val & 255
  7905. }
  7906. // Invalid ranges are not set to a default, so can range check early.
  7907. if (start < 0 || this.length < start || this.length < end) {
  7908. throw new RangeError('Out of range index')
  7909. }
  7910. if (end <= start) {
  7911. return this
  7912. }
  7913. start = start >>> 0
  7914. end = end === undefined ? this.length : end >>> 0
  7915. if (!val) val = 0
  7916. var i
  7917. if (typeof val === 'number') {
  7918. for (i = start; i < end; ++i) {
  7919. this[i] = val
  7920. }
  7921. } else {
  7922. var bytes = Buffer.isBuffer(val)
  7923. ? val
  7924. : Buffer.from(val, encoding)
  7925. var len = bytes.length
  7926. if (len === 0) {
  7927. throw new TypeError('The value "' + val +
  7928. '" is invalid for argument "value"')
  7929. }
  7930. for (i = 0; i < end - start; ++i) {
  7931. this[i + start] = bytes[i % len]
  7932. }
  7933. }
  7934. return this
  7935. }
  7936. // HELPER FUNCTIONS
  7937. // ================
  7938. var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
  7939. function base64clean (str) {
  7940. // Node takes equal signs as end of the Base64 encoding
  7941. str = str.split('=')[0]
  7942. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  7943. str = str.trim().replace(INVALID_BASE64_RE, '')
  7944. // Node converts strings with length < 2 to ''
  7945. if (str.length < 2) return ''
  7946. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  7947. while (str.length % 4 !== 0) {
  7948. str = str + '='
  7949. }
  7950. return str
  7951. }
  7952. function toHex (n) {
  7953. if (n < 16) return '0' + n.toString(16)
  7954. return n.toString(16)
  7955. }
  7956. function utf8ToBytes (string, units) {
  7957. units = units || Infinity
  7958. var codePoint
  7959. var length = string.length
  7960. var leadSurrogate = null
  7961. var bytes = []
  7962. for (var i = 0; i < length; ++i) {
  7963. codePoint = string.charCodeAt(i)
  7964. // is surrogate component
  7965. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  7966. // last char was a lead
  7967. if (!leadSurrogate) {
  7968. // no lead yet
  7969. if (codePoint > 0xDBFF) {
  7970. // unexpected trail
  7971. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7972. continue
  7973. } else if (i + 1 === length) {
  7974. // unpaired lead
  7975. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7976. continue
  7977. }
  7978. // valid lead
  7979. leadSurrogate = codePoint
  7980. continue
  7981. }
  7982. // 2 leads in a row
  7983. if (codePoint < 0xDC00) {
  7984. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7985. leadSurrogate = codePoint
  7986. continue
  7987. }
  7988. // valid surrogate pair
  7989. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  7990. } else if (leadSurrogate) {
  7991. // valid bmp char, but last char was a lead
  7992. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7993. }
  7994. leadSurrogate = null
  7995. // encode utf8
  7996. if (codePoint < 0x80) {
  7997. if ((units -= 1) < 0) break
  7998. bytes.push(codePoint)
  7999. } else if (codePoint < 0x800) {
  8000. if ((units -= 2) < 0) break
  8001. bytes.push(
  8002. codePoint >> 0x6 | 0xC0,
  8003. codePoint & 0x3F | 0x80
  8004. )
  8005. } else if (codePoint < 0x10000) {
  8006. if ((units -= 3) < 0) break
  8007. bytes.push(
  8008. codePoint >> 0xC | 0xE0,
  8009. codePoint >> 0x6 & 0x3F | 0x80,
  8010. codePoint & 0x3F | 0x80
  8011. )
  8012. } else if (codePoint < 0x110000) {
  8013. if ((units -= 4) < 0) break
  8014. bytes.push(
  8015. codePoint >> 0x12 | 0xF0,
  8016. codePoint >> 0xC & 0x3F | 0x80,
  8017. codePoint >> 0x6 & 0x3F | 0x80,
  8018. codePoint & 0x3F | 0x80
  8019. )
  8020. } else {
  8021. throw new Error('Invalid code point')
  8022. }
  8023. }
  8024. return bytes
  8025. }
  8026. function asciiToBytes (str) {
  8027. var byteArray = []
  8028. for (var i = 0; i < str.length; ++i) {
  8029. // Node's code seems to be doing this and not & 0x7F..
  8030. byteArray.push(str.charCodeAt(i) & 0xFF)
  8031. }
  8032. return byteArray
  8033. }
  8034. function utf16leToBytes (str, units) {
  8035. var c, hi, lo
  8036. var byteArray = []
  8037. for (var i = 0; i < str.length; ++i) {
  8038. if ((units -= 2) < 0) break
  8039. c = str.charCodeAt(i)
  8040. hi = c >> 8
  8041. lo = c % 256
  8042. byteArray.push(lo)
  8043. byteArray.push(hi)
  8044. }
  8045. return byteArray
  8046. }
  8047. function base64ToBytes (str) {
  8048. return base64.toByteArray(base64clean(str))
  8049. }
  8050. function blitBuffer (src, dst, offset, length) {
  8051. for (var i = 0; i < length; ++i) {
  8052. if ((i + offset >= dst.length) || (i >= src.length)) break
  8053. dst[i + offset] = src[i]
  8054. }
  8055. return i
  8056. }
  8057. // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
  8058. // the `instanceof` check but they should be treated as of that type.
  8059. // See: https://github.com/feross/buffer/issues/166
  8060. function isInstance (obj, type) {
  8061. return obj instanceof type ||
  8062. (obj != null && obj.constructor != null && obj.constructor.name != null &&
  8063. obj.constructor.name === type.name)
  8064. }
  8065. function numberIsNaN (obj) {
  8066. // For IE11 support
  8067. return obj !== obj // eslint-disable-line no-self-compare
  8068. }
  8069. }).call(this)}).call(this,require("buffer").Buffer)
  8070. },{"base64-js":41,"buffer":43,"ieee754":45}],44:[function(require,module,exports){
  8071. // Copyright Joyent, Inc. and other Node contributors.
  8072. //
  8073. // Permission is hereby granted, free of charge, to any person obtaining a
  8074. // copy of this software and associated documentation files (the
  8075. // "Software"), to deal in the Software without restriction, including
  8076. // without limitation the rights to use, copy, modify, merge, publish,
  8077. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8078. // persons to whom the Software is furnished to do so, subject to the
  8079. // following conditions:
  8080. //
  8081. // The above copyright notice and this permission notice shall be included
  8082. // in all copies or substantial portions of the Software.
  8083. //
  8084. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8085. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  8086. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  8087. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  8088. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  8089. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  8090. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  8091. 'use strict';
  8092. var R = typeof Reflect === 'object' ? Reflect : null
  8093. var ReflectApply = R && typeof R.apply === 'function'
  8094. ? R.apply
  8095. : function ReflectApply(target, receiver, args) {
  8096. return Function.prototype.apply.call(target, receiver, args);
  8097. }
  8098. var ReflectOwnKeys
  8099. if (R && typeof R.ownKeys === 'function') {
  8100. ReflectOwnKeys = R.ownKeys
  8101. } else if (Object.getOwnPropertySymbols) {
  8102. ReflectOwnKeys = function ReflectOwnKeys(target) {
  8103. return Object.getOwnPropertyNames(target)
  8104. .concat(Object.getOwnPropertySymbols(target));
  8105. };
  8106. } else {
  8107. ReflectOwnKeys = function ReflectOwnKeys(target) {
  8108. return Object.getOwnPropertyNames(target);
  8109. };
  8110. }
  8111. function ProcessEmitWarning(warning) {
  8112. if (console && console.warn) console.warn(warning);
  8113. }
  8114. var NumberIsNaN = Number.isNaN || function NumberIsNaN(value) {
  8115. return value !== value;
  8116. }
  8117. function EventEmitter() {
  8118. EventEmitter.init.call(this);
  8119. }
  8120. module.exports = EventEmitter;
  8121. module.exports.once = once;
  8122. // Backwards-compat with node 0.10.x
  8123. EventEmitter.EventEmitter = EventEmitter;
  8124. EventEmitter.prototype._events = undefined;
  8125. EventEmitter.prototype._eventsCount = 0;
  8126. EventEmitter.prototype._maxListeners = undefined;
  8127. // By default EventEmitters will print a warning if more than 10 listeners are
  8128. // added to it. This is a useful default which helps finding memory leaks.
  8129. var defaultMaxListeners = 10;
  8130. function checkListener(listener) {
  8131. if (typeof listener !== 'function') {
  8132. throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
  8133. }
  8134. }
  8135. Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
  8136. enumerable: true,
  8137. get: function() {
  8138. return defaultMaxListeners;
  8139. },
  8140. set: function(arg) {
  8141. if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
  8142. throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + '.');
  8143. }
  8144. defaultMaxListeners = arg;
  8145. }
  8146. });
  8147. EventEmitter.init = function() {
  8148. if (this._events === undefined ||
  8149. this._events === Object.getPrototypeOf(this)._events) {
  8150. this._events = Object.create(null);
  8151. this._eventsCount = 0;
  8152. }
  8153. this._maxListeners = this._maxListeners || undefined;
  8154. };
  8155. // Obviously not all Emitters should be limited to 10. This function allows
  8156. // that to be increased. Set to zero for unlimited.
  8157. EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
  8158. if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
  8159. throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
  8160. }
  8161. this._maxListeners = n;
  8162. return this;
  8163. };
  8164. function _getMaxListeners(that) {
  8165. if (that._maxListeners === undefined)
  8166. return EventEmitter.defaultMaxListeners;
  8167. return that._maxListeners;
  8168. }
  8169. EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
  8170. return _getMaxListeners(this);
  8171. };
  8172. EventEmitter.prototype.emit = function emit(type) {
  8173. var args = [];
  8174. for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
  8175. var doError = (type === 'error');
  8176. var events = this._events;
  8177. if (events !== undefined)
  8178. doError = (doError && events.error === undefined);
  8179. else if (!doError)
  8180. return false;
  8181. // If there is no 'error' event listener then throw.
  8182. if (doError) {
  8183. var er;
  8184. if (args.length > 0)
  8185. er = args[0];
  8186. if (er instanceof Error) {
  8187. // Note: The comments on the `throw` lines are intentional, they show
  8188. // up in Node's output if this results in an unhandled exception.
  8189. throw er; // Unhandled 'error' event
  8190. }
  8191. // At least give some kind of context to the user
  8192. var err = new Error('Unhandled error.' + (er ? ' (' + er.message + ')' : ''));
  8193. err.context = er;
  8194. throw err; // Unhandled 'error' event
  8195. }
  8196. var handler = events[type];
  8197. if (handler === undefined)
  8198. return false;
  8199. if (typeof handler === 'function') {
  8200. ReflectApply(handler, this, args);
  8201. } else {
  8202. var len = handler.length;
  8203. var listeners = arrayClone(handler, len);
  8204. for (var i = 0; i < len; ++i)
  8205. ReflectApply(listeners[i], this, args);
  8206. }
  8207. return true;
  8208. };
  8209. function _addListener(target, type, listener, prepend) {
  8210. var m;
  8211. var events;
  8212. var existing;
  8213. checkListener(listener);
  8214. events = target._events;
  8215. if (events === undefined) {
  8216. events = target._events = Object.create(null);
  8217. target._eventsCount = 0;
  8218. } else {
  8219. // To avoid recursion in the case that type === "newListener"! Before
  8220. // adding it to the listeners, first emit "newListener".
  8221. if (events.newListener !== undefined) {
  8222. target.emit('newListener', type,
  8223. listener.listener ? listener.listener : listener);
  8224. // Re-assign `events` because a newListener handler could have caused the
  8225. // this._events to be assigned to a new object
  8226. events = target._events;
  8227. }
  8228. existing = events[type];
  8229. }
  8230. if (existing === undefined) {
  8231. // Optimize the case of one listener. Don't need the extra array object.
  8232. existing = events[type] = listener;
  8233. ++target._eventsCount;
  8234. } else {
  8235. if (typeof existing === 'function') {
  8236. // Adding the second element, need to change to array.
  8237. existing = events[type] =
  8238. prepend ? [listener, existing] : [existing, listener];
  8239. // If we've already got an array, just append.
  8240. } else if (prepend) {
  8241. existing.unshift(listener);
  8242. } else {
  8243. existing.push(listener);
  8244. }
  8245. // Check for listener leak
  8246. m = _getMaxListeners(target);
  8247. if (m > 0 && existing.length > m && !existing.warned) {
  8248. existing.warned = true;
  8249. // No error code for this since it is a Warning
  8250. // eslint-disable-next-line no-restricted-syntax
  8251. var w = new Error('Possible EventEmitter memory leak detected. ' +
  8252. existing.length + ' ' + String(type) + ' listeners ' +
  8253. 'added. Use emitter.setMaxListeners() to ' +
  8254. 'increase limit');
  8255. w.name = 'MaxListenersExceededWarning';
  8256. w.emitter = target;
  8257. w.type = type;
  8258. w.count = existing.length;
  8259. ProcessEmitWarning(w);
  8260. }
  8261. }
  8262. return target;
  8263. }
  8264. EventEmitter.prototype.addListener = function addListener(type, listener) {
  8265. return _addListener(this, type, listener, false);
  8266. };
  8267. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  8268. EventEmitter.prototype.prependListener =
  8269. function prependListener(type, listener) {
  8270. return _addListener(this, type, listener, true);
  8271. };
  8272. function onceWrapper() {
  8273. if (!this.fired) {
  8274. this.target.removeListener(this.type, this.wrapFn);
  8275. this.fired = true;
  8276. if (arguments.length === 0)
  8277. return this.listener.call(this.target);
  8278. return this.listener.apply(this.target, arguments);
  8279. }
  8280. }
  8281. function _onceWrap(target, type, listener) {
  8282. var state = { fired: false, wrapFn: undefined, target: target, type: type, listener: listener };
  8283. var wrapped = onceWrapper.bind(state);
  8284. wrapped.listener = listener;
  8285. state.wrapFn = wrapped;
  8286. return wrapped;
  8287. }
  8288. EventEmitter.prototype.once = function once(type, listener) {
  8289. checkListener(listener);
  8290. this.on(type, _onceWrap(this, type, listener));
  8291. return this;
  8292. };
  8293. EventEmitter.prototype.prependOnceListener =
  8294. function prependOnceListener(type, listener) {
  8295. checkListener(listener);
  8296. this.prependListener(type, _onceWrap(this, type, listener));
  8297. return this;
  8298. };
  8299. // Emits a 'removeListener' event if and only if the listener was removed.
  8300. EventEmitter.prototype.removeListener =
  8301. function removeListener(type, listener) {
  8302. var list, events, position, i, originalListener;
  8303. checkListener(listener);
  8304. events = this._events;
  8305. if (events === undefined)
  8306. return this;
  8307. list = events[type];
  8308. if (list === undefined)
  8309. return this;
  8310. if (list === listener || list.listener === listener) {
  8311. if (--this._eventsCount === 0)
  8312. this._events = Object.create(null);
  8313. else {
  8314. delete events[type];
  8315. if (events.removeListener)
  8316. this.emit('removeListener', type, list.listener || listener);
  8317. }
  8318. } else if (typeof list !== 'function') {
  8319. position = -1;
  8320. for (i = list.length - 1; i >= 0; i--) {
  8321. if (list[i] === listener || list[i].listener === listener) {
  8322. originalListener = list[i].listener;
  8323. position = i;
  8324. break;
  8325. }
  8326. }
  8327. if (position < 0)
  8328. return this;
  8329. if (position === 0)
  8330. list.shift();
  8331. else {
  8332. spliceOne(list, position);
  8333. }
  8334. if (list.length === 1)
  8335. events[type] = list[0];
  8336. if (events.removeListener !== undefined)
  8337. this.emit('removeListener', type, originalListener || listener);
  8338. }
  8339. return this;
  8340. };
  8341. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  8342. EventEmitter.prototype.removeAllListeners =
  8343. function removeAllListeners(type) {
  8344. var listeners, events, i;
  8345. events = this._events;
  8346. if (events === undefined)
  8347. return this;
  8348. // not listening for removeListener, no need to emit
  8349. if (events.removeListener === undefined) {
  8350. if (arguments.length === 0) {
  8351. this._events = Object.create(null);
  8352. this._eventsCount = 0;
  8353. } else if (events[type] !== undefined) {
  8354. if (--this._eventsCount === 0)
  8355. this._events = Object.create(null);
  8356. else
  8357. delete events[type];
  8358. }
  8359. return this;
  8360. }
  8361. // emit removeListener for all listeners on all events
  8362. if (arguments.length === 0) {
  8363. var keys = Object.keys(events);
  8364. var key;
  8365. for (i = 0; i < keys.length; ++i) {
  8366. key = keys[i];
  8367. if (key === 'removeListener') continue;
  8368. this.removeAllListeners(key);
  8369. }
  8370. this.removeAllListeners('removeListener');
  8371. this._events = Object.create(null);
  8372. this._eventsCount = 0;
  8373. return this;
  8374. }
  8375. listeners = events[type];
  8376. if (typeof listeners === 'function') {
  8377. this.removeListener(type, listeners);
  8378. } else if (listeners !== undefined) {
  8379. // LIFO order
  8380. for (i = listeners.length - 1; i >= 0; i--) {
  8381. this.removeListener(type, listeners[i]);
  8382. }
  8383. }
  8384. return this;
  8385. };
  8386. function _listeners(target, type, unwrap) {
  8387. var events = target._events;
  8388. if (events === undefined)
  8389. return [];
  8390. var evlistener = events[type];
  8391. if (evlistener === undefined)
  8392. return [];
  8393. if (typeof evlistener === 'function')
  8394. return unwrap ? [evlistener.listener || evlistener] : [evlistener];
  8395. return unwrap ?
  8396. unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
  8397. }
  8398. EventEmitter.prototype.listeners = function listeners(type) {
  8399. return _listeners(this, type, true);
  8400. };
  8401. EventEmitter.prototype.rawListeners = function rawListeners(type) {
  8402. return _listeners(this, type, false);
  8403. };
  8404. EventEmitter.listenerCount = function(emitter, type) {
  8405. if (typeof emitter.listenerCount === 'function') {
  8406. return emitter.listenerCount(type);
  8407. } else {
  8408. return listenerCount.call(emitter, type);
  8409. }
  8410. };
  8411. EventEmitter.prototype.listenerCount = listenerCount;
  8412. function listenerCount(type) {
  8413. var events = this._events;
  8414. if (events !== undefined) {
  8415. var evlistener = events[type];
  8416. if (typeof evlistener === 'function') {
  8417. return 1;
  8418. } else if (evlistener !== undefined) {
  8419. return evlistener.length;
  8420. }
  8421. }
  8422. return 0;
  8423. }
  8424. EventEmitter.prototype.eventNames = function eventNames() {
  8425. return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
  8426. };
  8427. function arrayClone(arr, n) {
  8428. var copy = new Array(n);
  8429. for (var i = 0; i < n; ++i)
  8430. copy[i] = arr[i];
  8431. return copy;
  8432. }
  8433. function spliceOne(list, index) {
  8434. for (; index + 1 < list.length; index++)
  8435. list[index] = list[index + 1];
  8436. list.pop();
  8437. }
  8438. function unwrapListeners(arr) {
  8439. var ret = new Array(arr.length);
  8440. for (var i = 0; i < ret.length; ++i) {
  8441. ret[i] = arr[i].listener || arr[i];
  8442. }
  8443. return ret;
  8444. }
  8445. function once(emitter, name) {
  8446. return new Promise(function (resolve, reject) {
  8447. function eventListener() {
  8448. if (errorListener !== undefined) {
  8449. emitter.removeListener('error', errorListener);
  8450. }
  8451. resolve([].slice.call(arguments));
  8452. };
  8453. var errorListener;
  8454. // Adding an error listener is not optional because
  8455. // if an error is thrown on an event emitter we cannot
  8456. // guarantee that the actual event we are waiting will
  8457. // be fired. The result could be a silent way to create
  8458. // memory or file descriptor leaks, which is something
  8459. // we should avoid.
  8460. if (name !== 'error') {
  8461. errorListener = function errorListener(err) {
  8462. emitter.removeListener(name, eventListener);
  8463. reject(err);
  8464. };
  8465. emitter.once('error', errorListener);
  8466. }
  8467. emitter.once(name, eventListener);
  8468. });
  8469. }
  8470. },{}],45:[function(require,module,exports){
  8471. /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
  8472. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  8473. var e, m
  8474. var eLen = (nBytes * 8) - mLen - 1
  8475. var eMax = (1 << eLen) - 1
  8476. var eBias = eMax >> 1
  8477. var nBits = -7
  8478. var i = isLE ? (nBytes - 1) : 0
  8479. var d = isLE ? -1 : 1
  8480. var s = buffer[offset + i]
  8481. i += d
  8482. e = s & ((1 << (-nBits)) - 1)
  8483. s >>= (-nBits)
  8484. nBits += eLen
  8485. for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  8486. m = e & ((1 << (-nBits)) - 1)
  8487. e >>= (-nBits)
  8488. nBits += mLen
  8489. for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
  8490. if (e === 0) {
  8491. e = 1 - eBias
  8492. } else if (e === eMax) {
  8493. return m ? NaN : ((s ? -1 : 1) * Infinity)
  8494. } else {
  8495. m = m + Math.pow(2, mLen)
  8496. e = e - eBias
  8497. }
  8498. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  8499. }
  8500. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  8501. var e, m, c
  8502. var eLen = (nBytes * 8) - mLen - 1
  8503. var eMax = (1 << eLen) - 1
  8504. var eBias = eMax >> 1
  8505. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  8506. var i = isLE ? 0 : (nBytes - 1)
  8507. var d = isLE ? 1 : -1
  8508. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  8509. value = Math.abs(value)
  8510. if (isNaN(value) || value === Infinity) {
  8511. m = isNaN(value) ? 1 : 0
  8512. e = eMax
  8513. } else {
  8514. e = Math.floor(Math.log(value) / Math.LN2)
  8515. if (value * (c = Math.pow(2, -e)) < 1) {
  8516. e--
  8517. c *= 2
  8518. }
  8519. if (e + eBias >= 1) {
  8520. value += rt / c
  8521. } else {
  8522. value += rt * Math.pow(2, 1 - eBias)
  8523. }
  8524. if (value * c >= 2) {
  8525. e++
  8526. c /= 2
  8527. }
  8528. if (e + eBias >= eMax) {
  8529. m = 0
  8530. e = eMax
  8531. } else if (e + eBias >= 1) {
  8532. m = ((value * c) - 1) * Math.pow(2, mLen)
  8533. e = e + eBias
  8534. } else {
  8535. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  8536. e = 0
  8537. }
  8538. }
  8539. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  8540. e = (e << mLen) | m
  8541. eLen += mLen
  8542. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  8543. buffer[offset + i - d] |= s * 128
  8544. }
  8545. },{}],46:[function(require,module,exports){
  8546. arguments[4][10][0].apply(exports,arguments)
  8547. },{"dup":10}],47:[function(require,module,exports){
  8548. // shim for using process in browser
  8549. var process = module.exports = {};
  8550. // cached from whatever global is present so that test runners that stub it
  8551. // don't break things. But we need to wrap it in a try catch in case it is
  8552. // wrapped in strict mode code which doesn't define any globals. It's inside a
  8553. // function because try/catches deoptimize in certain engines.
  8554. var cachedSetTimeout;
  8555. var cachedClearTimeout;
  8556. function defaultSetTimout() {
  8557. throw new Error('setTimeout has not been defined');
  8558. }
  8559. function defaultClearTimeout () {
  8560. throw new Error('clearTimeout has not been defined');
  8561. }
  8562. (function () {
  8563. try {
  8564. if (typeof setTimeout === 'function') {
  8565. cachedSetTimeout = setTimeout;
  8566. } else {
  8567. cachedSetTimeout = defaultSetTimout;
  8568. }
  8569. } catch (e) {
  8570. cachedSetTimeout = defaultSetTimout;
  8571. }
  8572. try {
  8573. if (typeof clearTimeout === 'function') {
  8574. cachedClearTimeout = clearTimeout;
  8575. } else {
  8576. cachedClearTimeout = defaultClearTimeout;
  8577. }
  8578. } catch (e) {
  8579. cachedClearTimeout = defaultClearTimeout;
  8580. }
  8581. } ())
  8582. function runTimeout(fun) {
  8583. if (cachedSetTimeout === setTimeout) {
  8584. //normal enviroments in sane situations
  8585. return setTimeout(fun, 0);
  8586. }
  8587. // if setTimeout wasn't available but was latter defined
  8588. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  8589. cachedSetTimeout = setTimeout;
  8590. return setTimeout(fun, 0);
  8591. }
  8592. try {
  8593. // when when somebody has screwed with setTimeout but no I.E. maddness
  8594. return cachedSetTimeout(fun, 0);
  8595. } catch(e){
  8596. try {
  8597. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  8598. return cachedSetTimeout.call(null, fun, 0);
  8599. } catch(e){
  8600. // 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
  8601. return cachedSetTimeout.call(this, fun, 0);
  8602. }
  8603. }
  8604. }
  8605. function runClearTimeout(marker) {
  8606. if (cachedClearTimeout === clearTimeout) {
  8607. //normal enviroments in sane situations
  8608. return clearTimeout(marker);
  8609. }
  8610. // if clearTimeout wasn't available but was latter defined
  8611. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  8612. cachedClearTimeout = clearTimeout;
  8613. return clearTimeout(marker);
  8614. }
  8615. try {
  8616. // when when somebody has screwed with setTimeout but no I.E. maddness
  8617. return cachedClearTimeout(marker);
  8618. } catch (e){
  8619. try {
  8620. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  8621. return cachedClearTimeout.call(null, marker);
  8622. } catch (e){
  8623. // 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.
  8624. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  8625. return cachedClearTimeout.call(this, marker);
  8626. }
  8627. }
  8628. }
  8629. var queue = [];
  8630. var draining = false;
  8631. var currentQueue;
  8632. var queueIndex = -1;
  8633. function cleanUpNextTick() {
  8634. if (!draining || !currentQueue) {
  8635. return;
  8636. }
  8637. draining = false;
  8638. if (currentQueue.length) {
  8639. queue = currentQueue.concat(queue);
  8640. } else {
  8641. queueIndex = -1;
  8642. }
  8643. if (queue.length) {
  8644. drainQueue();
  8645. }
  8646. }
  8647. function drainQueue() {
  8648. if (draining) {
  8649. return;
  8650. }
  8651. var timeout = runTimeout(cleanUpNextTick);
  8652. draining = true;
  8653. var len = queue.length;
  8654. while(len) {
  8655. currentQueue = queue;
  8656. queue = [];
  8657. while (++queueIndex < len) {
  8658. if (currentQueue) {
  8659. currentQueue[queueIndex].run();
  8660. }
  8661. }
  8662. queueIndex = -1;
  8663. len = queue.length;
  8664. }
  8665. currentQueue = null;
  8666. draining = false;
  8667. runClearTimeout(timeout);
  8668. }
  8669. process.nextTick = function (fun) {
  8670. var args = new Array(arguments.length - 1);
  8671. if (arguments.length > 1) {
  8672. for (var i = 1; i < arguments.length; i++) {
  8673. args[i - 1] = arguments[i];
  8674. }
  8675. }
  8676. queue.push(new Item(fun, args));
  8677. if (queue.length === 1 && !draining) {
  8678. runTimeout(drainQueue);
  8679. }
  8680. };
  8681. // v8 likes predictible objects
  8682. function Item(fun, array) {
  8683. this.fun = fun;
  8684. this.array = array;
  8685. }
  8686. Item.prototype.run = function () {
  8687. this.fun.apply(null, this.array);
  8688. };
  8689. process.title = 'browser';
  8690. process.browser = true;
  8691. process.env = {};
  8692. process.argv = [];
  8693. process.version = ''; // empty string to avoid regexp issues
  8694. process.versions = {};
  8695. function noop() {}
  8696. process.on = noop;
  8697. process.addListener = noop;
  8698. process.once = noop;
  8699. process.off = noop;
  8700. process.removeListener = noop;
  8701. process.removeAllListeners = noop;
  8702. process.emit = noop;
  8703. process.prependListener = noop;
  8704. process.prependOnceListener = noop;
  8705. process.listeners = function (name) { return [] }
  8706. process.binding = function (name) {
  8707. throw new Error('process.binding is not supported');
  8708. };
  8709. process.cwd = function () { return '/' };
  8710. process.chdir = function (dir) {
  8711. throw new Error('process.chdir is not supported');
  8712. };
  8713. process.umask = function() { return 0; };
  8714. },{}],48:[function(require,module,exports){
  8715. arguments[4][28][0].apply(exports,arguments)
  8716. },{"buffer":43,"dup":28}],49:[function(require,module,exports){
  8717. // Copyright Joyent, Inc. and other Node contributors.
  8718. //
  8719. // Permission is hereby granted, free of charge, to any person obtaining a
  8720. // copy of this software and associated documentation files (the
  8721. // "Software"), to deal in the Software without restriction, including
  8722. // without limitation the rights to use, copy, modify, merge, publish,
  8723. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8724. // persons to whom the Software is furnished to do so, subject to the
  8725. // following conditions:
  8726. //
  8727. // The above copyright notice and this permission notice shall be included
  8728. // in all copies or substantial portions of the Software.
  8729. //
  8730. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8731. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  8732. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  8733. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  8734. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  8735. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  8736. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  8737. module.exports = Stream;
  8738. var EE = require('events').EventEmitter;
  8739. var inherits = require('inherits');
  8740. inherits(Stream, EE);
  8741. Stream.Readable = require('readable-stream/lib/_stream_readable.js');
  8742. Stream.Writable = require('readable-stream/lib/_stream_writable.js');
  8743. Stream.Duplex = require('readable-stream/lib/_stream_duplex.js');
  8744. Stream.Transform = require('readable-stream/lib/_stream_transform.js');
  8745. Stream.PassThrough = require('readable-stream/lib/_stream_passthrough.js');
  8746. Stream.finished = require('readable-stream/lib/internal/streams/end-of-stream.js')
  8747. Stream.pipeline = require('readable-stream/lib/internal/streams/pipeline.js')
  8748. // Backwards-compat with node 0.4.x
  8749. Stream.Stream = Stream;
  8750. // old-style streams. Note that the pipe method (the only relevant
  8751. // part of this class) is overridden in the Readable class.
  8752. function Stream() {
  8753. EE.call(this);
  8754. }
  8755. Stream.prototype.pipe = function(dest, options) {
  8756. var source = this;
  8757. function ondata(chunk) {
  8758. if (dest.writable) {
  8759. if (false === dest.write(chunk) && source.pause) {
  8760. source.pause();
  8761. }
  8762. }
  8763. }
  8764. source.on('data', ondata);
  8765. function ondrain() {
  8766. if (source.readable && source.resume) {
  8767. source.resume();
  8768. }
  8769. }
  8770. dest.on('drain', ondrain);
  8771. // If the 'end' option is not supplied, dest.end() will be called when
  8772. // source gets the 'end' or 'close' events. Only dest.end() once.
  8773. if (!dest._isStdio && (!options || options.end !== false)) {
  8774. source.on('end', onend);
  8775. source.on('close', onclose);
  8776. }
  8777. var didOnEnd = false;
  8778. function onend() {
  8779. if (didOnEnd) return;
  8780. didOnEnd = true;
  8781. dest.end();
  8782. }
  8783. function onclose() {
  8784. if (didOnEnd) return;
  8785. didOnEnd = true;
  8786. if (typeof dest.destroy === 'function') dest.destroy();
  8787. }
  8788. // don't leave dangling pipes when there are errors.
  8789. function onerror(er) {
  8790. cleanup();
  8791. if (EE.listenerCount(this, 'error') === 0) {
  8792. throw er; // Unhandled stream error in pipe.
  8793. }
  8794. }
  8795. source.on('error', onerror);
  8796. dest.on('error', onerror);
  8797. // remove all the event listeners that were added.
  8798. function cleanup() {
  8799. source.removeListener('data', ondata);
  8800. dest.removeListener('drain', ondrain);
  8801. source.removeListener('end', onend);
  8802. source.removeListener('close', onclose);
  8803. source.removeListener('error', onerror);
  8804. dest.removeListener('error', onerror);
  8805. source.removeListener('end', cleanup);
  8806. source.removeListener('close', cleanup);
  8807. dest.removeListener('close', cleanup);
  8808. }
  8809. source.on('end', cleanup);
  8810. source.on('close', cleanup);
  8811. dest.on('close', cleanup);
  8812. dest.emit('pipe', source);
  8813. // Allow for unix-like usage: A.pipe(B).pipe(C)
  8814. return dest;
  8815. };
  8816. },{"events":44,"inherits":46,"readable-stream/lib/_stream_duplex.js":51,"readable-stream/lib/_stream_passthrough.js":52,"readable-stream/lib/_stream_readable.js":53,"readable-stream/lib/_stream_transform.js":54,"readable-stream/lib/_stream_writable.js":55,"readable-stream/lib/internal/streams/end-of-stream.js":59,"readable-stream/lib/internal/streams/pipeline.js":61}],50:[function(require,module,exports){
  8817. arguments[4][12][0].apply(exports,arguments)
  8818. },{"dup":12}],51:[function(require,module,exports){
  8819. arguments[4][13][0].apply(exports,arguments)
  8820. },{"./_stream_readable":53,"./_stream_writable":55,"_process":47,"dup":13,"inherits":46}],52:[function(require,module,exports){
  8821. arguments[4][14][0].apply(exports,arguments)
  8822. },{"./_stream_transform":54,"dup":14,"inherits":46}],53:[function(require,module,exports){
  8823. arguments[4][15][0].apply(exports,arguments)
  8824. },{"../errors":50,"./_stream_duplex":51,"./internal/streams/async_iterator":56,"./internal/streams/buffer_list":57,"./internal/streams/destroy":58,"./internal/streams/from":60,"./internal/streams/state":62,"./internal/streams/stream":63,"_process":47,"buffer":43,"dup":15,"events":44,"inherits":46,"string_decoder/":64,"util":42}],54:[function(require,module,exports){
  8825. arguments[4][16][0].apply(exports,arguments)
  8826. },{"../errors":50,"./_stream_duplex":51,"dup":16,"inherits":46}],55:[function(require,module,exports){
  8827. arguments[4][17][0].apply(exports,arguments)
  8828. },{"../errors":50,"./_stream_duplex":51,"./internal/streams/destroy":58,"./internal/streams/state":62,"./internal/streams/stream":63,"_process":47,"buffer":43,"dup":17,"inherits":46,"util-deprecate":65}],56:[function(require,module,exports){
  8829. arguments[4][18][0].apply(exports,arguments)
  8830. },{"./end-of-stream":59,"_process":47,"dup":18}],57:[function(require,module,exports){
  8831. arguments[4][19][0].apply(exports,arguments)
  8832. },{"buffer":43,"dup":19,"util":42}],58:[function(require,module,exports){
  8833. arguments[4][20][0].apply(exports,arguments)
  8834. },{"_process":47,"dup":20}],59:[function(require,module,exports){
  8835. arguments[4][21][0].apply(exports,arguments)
  8836. },{"../../../errors":50,"dup":21}],60:[function(require,module,exports){
  8837. arguments[4][22][0].apply(exports,arguments)
  8838. },{"dup":22}],61:[function(require,module,exports){
  8839. arguments[4][23][0].apply(exports,arguments)
  8840. },{"../../../errors":50,"./end-of-stream":59,"dup":23}],62:[function(require,module,exports){
  8841. arguments[4][24][0].apply(exports,arguments)
  8842. },{"../../../errors":50,"dup":24}],63:[function(require,module,exports){
  8843. arguments[4][25][0].apply(exports,arguments)
  8844. },{"dup":25,"events":44}],64:[function(require,module,exports){
  8845. arguments[4][37][0].apply(exports,arguments)
  8846. },{"dup":37,"safe-buffer":48}],65:[function(require,module,exports){
  8847. arguments[4][39][0].apply(exports,arguments)
  8848. },{"dup":39}]},{},[40]);