12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205 |
- import {
- __commonJS,
- __publicField,
- __require
- } from "/node_modules/.vite/deps/chunk-WQG2LZMB.js?v=9548b9c3";
- // browser-external:crypto
- var require_crypto = __commonJS({
- "browser-external:crypto"(exports, module2) {
- module2.exports = Object.create(new Proxy({}, {
- get(_, key) {
- if (key !== "__esModule" && key !== "__proto__" && key !== "constructor" && key !== "splice") {
- console.warn(`Module "crypto" has been externalized for browser compatibility. Cannot access "crypto.${key}" in client code. See http://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.`);
- }
- }
- }));
- }
- });
- // node_modules/nats.ws/esm/nats.js
- var Empty = new Uint8Array(0);
- var TE = new TextEncoder();
- var TD = new TextDecoder();
- function concat(...bufs) {
- let max = 0;
- for (let i = 0; i < bufs.length; i++) {
- max += bufs[i].length;
- }
- const out = new Uint8Array(max);
- let index = 0;
- for (let i = 0; i < bufs.length; i++) {
- out.set(bufs[i], index);
- index += bufs[i].length;
- }
- return out;
- }
- function encode(...a) {
- const bufs = [];
- for (let i = 0; i < a.length; i++) {
- bufs.push(TE.encode(a[i]));
- }
- if (bufs.length === 0) {
- return Empty;
- }
- if (bufs.length === 1) {
- return bufs[0];
- }
- return concat(...bufs);
- }
- function decode(a) {
- if (!a || a.length === 0) {
- return "";
- }
- return TD.decode(a);
- }
- var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- var base = 36;
- var maxSeq = 3656158440062976;
- var minInc = 33;
- var maxInc = 333;
- var totalLen = 12 + 10;
- function _getRandomValues(a) {
- for (let i = 0; i < a.length; i++) {
- a[i] = Math.floor(Math.random() * 255);
- }
- }
- function fillRandom(a) {
- var _a;
- if ((_a = globalThis == null ? void 0 : globalThis.crypto) == null ? void 0 : _a.getRandomValues) {
- globalThis.crypto.getRandomValues(a);
- } else {
- _getRandomValues(a);
- }
- }
- var Nuid = class {
- constructor() {
- __publicField(this, "buf");
- __publicField(this, "seq");
- __publicField(this, "inc");
- this.buf = new Uint8Array(totalLen);
- this.init();
- }
- init() {
- this.setPre();
- this.initSeqAndInc();
- this.fillSeq();
- }
- initSeqAndInc() {
- this.seq = Math.floor(Math.random() * maxSeq);
- this.inc = Math.floor(Math.random() * (maxInc - minInc) + minInc);
- }
- setPre() {
- const cbuf = new Uint8Array(12);
- fillRandom(cbuf);
- for (let i = 0; i < 12; i++) {
- const di = cbuf[i] % 36;
- this.buf[i] = digits.charCodeAt(di);
- }
- }
- fillSeq() {
- let n = this.seq;
- for (let i = totalLen - 1; i >= 12; i--) {
- this.buf[i] = digits.charCodeAt(n % base);
- n = Math.floor(n / base);
- }
- }
- next() {
- this.seq += this.inc;
- if (this.seq > 3656158440062976) {
- this.setPre();
- this.initSeqAndInc();
- }
- this.fillSeq();
- return String.fromCharCode.apply(String, this.buf);
- }
- reset() {
- this.init();
- }
- };
- var nuid = new Nuid();
- var Events;
- (function(Events2) {
- Events2["Disconnect"] = "disconnect";
- Events2["Reconnect"] = "reconnect";
- Events2["Update"] = "update";
- Events2["LDM"] = "ldm";
- Events2["Error"] = "error";
- })(Events || (Events = {}));
- var DebugEvents;
- (function(DebugEvents2) {
- DebugEvents2["Reconnecting"] = "reconnecting";
- DebugEvents2["PingTimer"] = "pingTimer";
- DebugEvents2["StaleConnection"] = "staleConnection";
- DebugEvents2["ClientInitiatedReconnect"] = "client initiated reconnect";
- })(DebugEvents || (DebugEvents = {}));
- var ErrorCode;
- (function(ErrorCode2) {
- ErrorCode2["ApiError"] = "BAD API";
- ErrorCode2["BadAuthentication"] = "BAD_AUTHENTICATION";
- ErrorCode2["BadCreds"] = "BAD_CREDS";
- ErrorCode2["BadHeader"] = "BAD_HEADER";
- ErrorCode2["BadJson"] = "BAD_JSON";
- ErrorCode2["BadPayload"] = "BAD_PAYLOAD";
- ErrorCode2["BadSubject"] = "BAD_SUBJECT";
- ErrorCode2["Cancelled"] = "CANCELLED";
- ErrorCode2["ConnectionClosed"] = "CONNECTION_CLOSED";
- ErrorCode2["ConnectionDraining"] = "CONNECTION_DRAINING";
- ErrorCode2["ConnectionRefused"] = "CONNECTION_REFUSED";
- ErrorCode2["ConnectionTimeout"] = "CONNECTION_TIMEOUT";
- ErrorCode2["Disconnect"] = "DISCONNECT";
- ErrorCode2["InvalidOption"] = "INVALID_OPTION";
- ErrorCode2["InvalidPayload"] = "INVALID_PAYLOAD";
- ErrorCode2["MaxPayloadExceeded"] = "MAX_PAYLOAD_EXCEEDED";
- ErrorCode2["NoResponders"] = "503";
- ErrorCode2["NotFunction"] = "NOT_FUNC";
- ErrorCode2["RequestError"] = "REQUEST_ERROR";
- ErrorCode2["ServerOptionNotAvailable"] = "SERVER_OPT_NA";
- ErrorCode2["SubClosed"] = "SUB_CLOSED";
- ErrorCode2["SubDraining"] = "SUB_DRAINING";
- ErrorCode2["Timeout"] = "TIMEOUT";
- ErrorCode2["Tls"] = "TLS";
- ErrorCode2["Unknown"] = "UNKNOWN_ERROR";
- ErrorCode2["WssRequired"] = "WSS_REQUIRED";
- ErrorCode2["JetStreamInvalidAck"] = "JESTREAM_INVALID_ACK";
- ErrorCode2["JetStream404NoMessages"] = "404";
- ErrorCode2["JetStream408RequestTimeout"] = "408";
- ErrorCode2["JetStream409MaxAckPendingExceeded"] = "409";
- ErrorCode2["JetStream409"] = "409";
- ErrorCode2["JetStreamNotEnabled"] = "503";
- ErrorCode2["JetStreamIdleHeartBeat"] = "IDLE_HEARTBEAT";
- ErrorCode2["AuthorizationViolation"] = "AUTHORIZATION_VIOLATION";
- ErrorCode2["AuthenticationExpired"] = "AUTHENTICATION_EXPIRED";
- ErrorCode2["ProtocolError"] = "NATS_PROTOCOL_ERR";
- ErrorCode2["PermissionsViolation"] = "PERMISSIONS_VIOLATION";
- ErrorCode2["AuthenticationTimeout"] = "AUTHENTICATION_TIMEOUT";
- })(ErrorCode || (ErrorCode = {}));
- function isNatsError(err) {
- return typeof err.code === "string";
- }
- var Messages = class {
- constructor() {
- __publicField(this, "messages");
- this.messages = /* @__PURE__ */ new Map();
- this.messages.set(ErrorCode.InvalidPayload, "Invalid payload type - payloads can be 'binary', 'string', or 'json'");
- this.messages.set(ErrorCode.BadJson, "Bad JSON");
- this.messages.set(ErrorCode.WssRequired, "TLS is required, therefore a secure websocket connection is also required");
- }
- static getMessage(s) {
- return messages.getMessage(s);
- }
- getMessage(s) {
- return this.messages.get(s) || s;
- }
- };
- var messages = new Messages();
- var NatsError = class extends Error {
- constructor(message, code2, chainedError) {
- super(message);
- __publicField(this, "name");
- __publicField(this, "message");
- __publicField(this, "code");
- __publicField(this, "permissionContext");
- __publicField(this, "chainedError");
- __publicField(this, "api_error");
- this.name = "NatsError";
- this.message = message;
- this.code = code2;
- this.chainedError = chainedError;
- }
- static errorForCode(code2, chainedError) {
- const m = Messages.getMessage(code2);
- return new NatsError(m, code2, chainedError);
- }
- isAuthError() {
- return this.code === ErrorCode.AuthenticationExpired || this.code === ErrorCode.AuthorizationViolation;
- }
- isAuthTimeout() {
- return this.code === ErrorCode.AuthenticationTimeout;
- }
- isPermissionError() {
- return this.code === ErrorCode.PermissionsViolation;
- }
- isProtocolError() {
- return this.code === ErrorCode.ProtocolError;
- }
- isJetStreamError() {
- return this.api_error !== void 0;
- }
- jsError() {
- return this.api_error ? this.api_error : null;
- }
- };
- var Match;
- (function(Match2) {
- Match2[Match2["Exact"] = 0] = "Exact";
- Match2[Match2["CanonicalMIME"] = 1] = "CanonicalMIME";
- Match2[Match2["IgnoreCase"] = 2] = "IgnoreCase";
- })(Match || (Match = {}));
- var RequestStrategy;
- (function(RequestStrategy2) {
- RequestStrategy2["Timer"] = "timer";
- RequestStrategy2["Count"] = "count";
- RequestStrategy2["JitterTimer"] = "jitterTimer";
- RequestStrategy2["SentinelMsg"] = "sentinelMsg";
- })(RequestStrategy || (RequestStrategy = {}));
- function syncIterator(src) {
- const iter = src[Symbol.asyncIterator]();
- return {
- async next() {
- const m = await iter.next();
- if (m.done) {
- return Promise.resolve(null);
- }
- return Promise.resolve(m.value);
- }
- };
- }
- var ServiceResponseType;
- (function(ServiceResponseType2) {
- ServiceResponseType2["STATS"] = "io.nats.micro.v1.stats_response";
- ServiceResponseType2["INFO"] = "io.nats.micro.v1.info_response";
- ServiceResponseType2["PING"] = "io.nats.micro.v1.ping_response";
- })(ServiceResponseType || (ServiceResponseType = {}));
- var ServiceErrorHeader = "Nats-Service-Error";
- var ServiceErrorCodeHeader = "Nats-Service-Error-Code";
- var ServiceError = class extends Error {
- constructor(code2, message) {
- super(message);
- __publicField(this, "code");
- this.code = code2;
- }
- static isServiceError(msg) {
- return ServiceError.toServiceError(msg) !== null;
- }
- static toServiceError(msg) {
- var _a, _b;
- const scode = ((_a = msg == null ? void 0 : msg.headers) == null ? void 0 : _a.get(ServiceErrorCodeHeader)) || "";
- if (scode !== "") {
- const code2 = parseInt(scode) || 400;
- const description = ((_b = msg == null ? void 0 : msg.headers) == null ? void 0 : _b.get(ServiceErrorHeader)) || "";
- return new ServiceError(code2, description.length ? description : scode);
- }
- return null;
- }
- };
- function createInbox(prefix = "") {
- prefix = prefix || "_INBOX";
- if (typeof prefix !== "string") {
- throw new Error("prefix must be a string");
- }
- prefix.split(".").forEach((v) => {
- if (v === "*" || v === ">") {
- throw new Error(`inbox prefixes cannot have wildcards '${prefix}'`);
- }
- });
- return `${prefix}.${nuid.next()}`;
- }
- var DEFAULT_HOST = "127.0.0.1";
- var ServiceVerb;
- (function(ServiceVerb2) {
- ServiceVerb2["PING"] = "PING";
- ServiceVerb2["STATS"] = "STATS";
- ServiceVerb2["INFO"] = "INFO";
- })(ServiceVerb || (ServiceVerb = {}));
- function extend(a, ...b) {
- for (let i = 0; i < b.length; i++) {
- const o = b[i];
- Object.keys(o).forEach(function(k) {
- a[k] = o[k];
- });
- }
- return a;
- }
- function render(frame) {
- const cr = "␍";
- const lf = "␊";
- return TD.decode(frame).replace(/\n/g, lf).replace(/\r/g, cr);
- }
- function timeout(ms, asyncTraces = true) {
- const err = asyncTraces ? NatsError.errorForCode(ErrorCode.Timeout) : null;
- let methods;
- let timer;
- const p = new Promise((_resolve, reject) => {
- const cancel = () => {
- if (timer) {
- clearTimeout(timer);
- }
- };
- methods = {
- cancel
- };
- timer = setTimeout(() => {
- if (err === null) {
- reject(NatsError.errorForCode(ErrorCode.Timeout));
- } else {
- reject(err);
- }
- }, ms);
- });
- return Object.assign(p, methods);
- }
- function delay(ms = 0) {
- return new Promise((resolve) => {
- setTimeout(() => {
- resolve();
- }, ms);
- });
- }
- function deadline(p, millis2 = 1e3) {
- const err = new Error(`deadline exceeded`);
- const d = deferred();
- const timer = setTimeout(() => d.reject(err), millis2);
- return Promise.race([
- p,
- d
- ]).finally(() => clearTimeout(timer));
- }
- function deferred() {
- let methods = {};
- const p = new Promise((resolve, reject) => {
- methods = {
- resolve,
- reject
- };
- });
- return Object.assign(p, methods);
- }
- function shuffle(a) {
- for (let i = a.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- [a[i], a[j]] = [
- a[j],
- a[i]
- ];
- }
- return a;
- }
- var Perf = class {
- constructor() {
- __publicField(this, "timers");
- __publicField(this, "measures");
- this.timers = /* @__PURE__ */ new Map();
- this.measures = /* @__PURE__ */ new Map();
- }
- mark(key) {
- this.timers.set(key, performance.now());
- }
- measure(key, startKey, endKey) {
- const s = this.timers.get(startKey);
- if (s === void 0) {
- throw new Error(`${startKey} is not defined`);
- }
- const e = this.timers.get(endKey);
- if (e === void 0) {
- throw new Error(`${endKey} is not defined`);
- }
- this.measures.set(key, e - s);
- }
- getEntries() {
- const values = [];
- this.measures.forEach((v, k) => {
- values.push({
- name: k,
- duration: v
- });
- });
- return values;
- }
- };
- function jitter(n) {
- if (n === 0) {
- return 0;
- }
- return Math.floor(n / 2 + Math.random() * n);
- }
- function backoff(policy = [
- 0,
- 250,
- 250,
- 500,
- 500,
- 3e3,
- 5e3
- ]) {
- if (!Array.isArray(policy)) {
- policy = [
- 0,
- 250,
- 250,
- 500,
- 500,
- 3e3,
- 5e3
- ];
- }
- const max = policy.length - 1;
- return {
- backoff(attempt) {
- return jitter(attempt > max ? policy[max] : policy[attempt]);
- }
- };
- }
- var DataBuffer = class {
- constructor() {
- __publicField(this, "buffers");
- __publicField(this, "byteLength");
- this.buffers = [];
- this.byteLength = 0;
- }
- static concat(...bufs) {
- let max = 0;
- for (let i = 0; i < bufs.length; i++) {
- max += bufs[i].length;
- }
- const out = new Uint8Array(max);
- let index = 0;
- for (let i = 0; i < bufs.length; i++) {
- out.set(bufs[i], index);
- index += bufs[i].length;
- }
- return out;
- }
- static fromAscii(m) {
- if (!m) {
- m = "";
- }
- return TE.encode(m);
- }
- static toAscii(a) {
- return TD.decode(a);
- }
- reset() {
- this.buffers.length = 0;
- this.byteLength = 0;
- }
- pack() {
- if (this.buffers.length > 1) {
- const v = new Uint8Array(this.byteLength);
- let index = 0;
- for (let i = 0; i < this.buffers.length; i++) {
- v.set(this.buffers[i], index);
- index += this.buffers[i].length;
- }
- this.buffers.length = 0;
- this.buffers.push(v);
- }
- }
- shift() {
- if (this.buffers.length) {
- const a = this.buffers.shift();
- if (a) {
- this.byteLength -= a.length;
- return a;
- }
- }
- return new Uint8Array(0);
- }
- drain(n) {
- if (this.buffers.length) {
- this.pack();
- const v = this.buffers.pop();
- if (v) {
- const max = this.byteLength;
- if (n === void 0 || n > max) {
- n = max;
- }
- const d = v.subarray(0, n);
- if (max > n) {
- this.buffers.push(v.subarray(n));
- }
- this.byteLength = max - n;
- return d;
- }
- }
- return new Uint8Array(0);
- }
- fill(a, ...bufs) {
- if (a) {
- this.buffers.push(a);
- this.byteLength += a.length;
- }
- for (let i = 0; i < bufs.length; i++) {
- if (bufs[i] && bufs[i].length) {
- this.buffers.push(bufs[i]);
- this.byteLength += bufs[i].length;
- }
- }
- }
- peek() {
- if (this.buffers.length) {
- this.pack();
- return this.buffers[0];
- }
- return new Uint8Array(0);
- }
- size() {
- return this.byteLength;
- }
- length() {
- return this.buffers.length;
- }
- };
- var transportConfig;
- function setTransportFactory(config) {
- transportConfig = config;
- }
- function defaultPort() {
- return transportConfig !== void 0 && transportConfig.defaultPort !== void 0 ? transportConfig.defaultPort : 4222;
- }
- function getUrlParseFn() {
- return transportConfig !== void 0 && transportConfig.urlParseFn ? transportConfig.urlParseFn : void 0;
- }
- function newTransport() {
- if (!transportConfig || typeof transportConfig.factory !== "function") {
- throw new Error("transport fn is not set");
- }
- return transportConfig.factory();
- }
- function getResolveFn() {
- return transportConfig !== void 0 && transportConfig.dnsResolveFn ? transportConfig.dnsResolveFn : void 0;
- }
- var CR_LF = "\r\n";
- CR_LF.length;
- var CRLF = DataBuffer.fromAscii(CR_LF);
- var CR = new Uint8Array(CRLF)[0];
- var LF = new Uint8Array(CRLF)[1];
- function protoLen(ba) {
- for (let i = 0; i < ba.length; i++) {
- const n = i + 1;
- if (ba.byteLength > n && ba[i] === CR && ba[n] === LF) {
- return n + 1;
- }
- }
- return 0;
- }
- function extractProtocolMessage(a) {
- const len = protoLen(a);
- if (len > 0) {
- const ba = new Uint8Array(a);
- const out = ba.slice(0, len);
- return TD.decode(out);
- }
- return "";
- }
- var IPv4LEN = 4;
- var ASCII0 = 48;
- var ASCIIA = 65;
- var ASCIIa = 97;
- function ipV4(a, b, c, d) {
- const ip = new Uint8Array(16);
- const prefix = [
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 255,
- 255
- ];
- prefix.forEach((v, idx) => {
- ip[idx] = v;
- });
- ip[12] = a;
- ip[13] = b;
- ip[14] = c;
- ip[15] = d;
- return ip;
- }
- function isIP(h) {
- return parseIP(h) !== void 0;
- }
- function parseIP(h) {
- for (let i = 0; i < h.length; i++) {
- switch (h[i]) {
- case ".":
- return parseIPv4(h);
- case ":":
- return parseIPv6(h);
- }
- }
- return;
- }
- function parseIPv4(s) {
- const ip = new Uint8Array(4);
- for (let i = 0; i < 4; i++) {
- if (s.length === 0) {
- return void 0;
- }
- if (i > 0) {
- if (s[0] !== ".") {
- return void 0;
- }
- s = s.substring(1);
- }
- const { n, c, ok } = dtoi(s);
- if (!ok || n > 255) {
- return void 0;
- }
- s = s.substring(c);
- ip[i] = n;
- }
- return ipV4(ip[0], ip[1], ip[2], ip[3]);
- }
- function parseIPv6(s) {
- const ip = new Uint8Array(16);
- let ellipsis = -1;
- if (s.length >= 2 && s[0] === ":" && s[1] === ":") {
- ellipsis = 0;
- s = s.substring(2);
- if (s.length === 0) {
- return ip;
- }
- }
- let i = 0;
- while (i < 16) {
- const { n, c, ok } = xtoi(s);
- if (!ok || n > 65535) {
- return void 0;
- }
- if (c < s.length && s[c] === ".") {
- if (ellipsis < 0 && i != 16 - 4) {
- return void 0;
- }
- if (i + 4 > 16) {
- return void 0;
- }
- const ip4 = parseIPv4(s);
- if (ip4 === void 0) {
- return void 0;
- }
- ip[i] = ip4[12];
- ip[i + 1] = ip4[13];
- ip[i + 2] = ip4[14];
- ip[i + 3] = ip4[15];
- s = "";
- i += IPv4LEN;
- break;
- }
- ip[i] = n >> 8;
- ip[i + 1] = n;
- i += 2;
- s = s.substring(c);
- if (s.length === 0) {
- break;
- }
- if (s[0] !== ":" || s.length == 1) {
- return void 0;
- }
- s = s.substring(1);
- if (s[0] === ":") {
- if (ellipsis >= 0) {
- return void 0;
- }
- ellipsis = i;
- s = s.substring(1);
- if (s.length === 0) {
- break;
- }
- }
- }
- if (s.length !== 0) {
- return void 0;
- }
- if (i < 16) {
- if (ellipsis < 0) {
- return void 0;
- }
- const n = 16 - i;
- for (let j = i - 1; j >= ellipsis; j--) {
- ip[j + n] = ip[j];
- }
- for (let j = ellipsis + n - 1; j >= ellipsis; j--) {
- ip[j] = 0;
- }
- } else if (ellipsis >= 0) {
- return void 0;
- }
- return ip;
- }
- function dtoi(s) {
- let i = 0;
- let n = 0;
- for (i = 0; i < s.length && 48 <= s.charCodeAt(i) && s.charCodeAt(i) <= 57; i++) {
- n = n * 10 + (s.charCodeAt(i) - ASCII0);
- if (n >= 16777215) {
- return {
- n: 16777215,
- c: i,
- ok: false
- };
- }
- }
- if (i === 0) {
- return {
- n: 0,
- c: 0,
- ok: false
- };
- }
- return {
- n,
- c: i,
- ok: true
- };
- }
- function xtoi(s) {
- let n = 0;
- let i = 0;
- for (i = 0; i < s.length; i++) {
- if (48 <= s.charCodeAt(i) && s.charCodeAt(i) <= 57) {
- n *= 16;
- n += s.charCodeAt(i) - ASCII0;
- } else if (97 <= s.charCodeAt(i) && s.charCodeAt(i) <= 102) {
- n *= 16;
- n += s.charCodeAt(i) - ASCIIa + 10;
- } else if (65 <= s.charCodeAt(i) && s.charCodeAt(i) <= 70) {
- n *= 16;
- n += s.charCodeAt(i) - ASCIIA + 10;
- } else {
- break;
- }
- if (n >= 16777215) {
- return {
- n: 0,
- c: i,
- ok: false
- };
- }
- }
- if (i === 0) {
- return {
- n: 0,
- c: i,
- ok: false
- };
- }
- return {
- n,
- c: i,
- ok: true
- };
- }
- function isIPV4OrHostname(hp) {
- if (hp.indexOf(".") !== -1) {
- return true;
- }
- if (hp.indexOf("[") !== -1 || hp.indexOf("::") !== -1) {
- return false;
- }
- if (hp.split(":").length <= 2) {
- return true;
- }
- return false;
- }
- function isIPV6(hp) {
- return !isIPV4OrHostname(hp);
- }
- function filterIpv6MappedToIpv4(hp) {
- const prefix = "::FFFF:";
- const idx = hp.toUpperCase().indexOf(prefix);
- if (idx !== -1 && hp.indexOf(".") !== -1) {
- let ip = hp.substring(idx + prefix.length);
- ip = ip.replace("[", "");
- return ip.replace("]", "");
- }
- return hp;
- }
- function hostPort(u) {
- u = u.trim();
- if (u.match(/^(.*:\/\/)(.*)/m)) {
- u = u.replace(/^(.*:\/\/)(.*)/gm, "$2");
- }
- u = filterIpv6MappedToIpv4(u);
- if (isIPV6(u) && u.indexOf("[") === -1) {
- u = `[${u}]`;
- }
- const op = isIPV6(u) ? u.match(/(]:)(\d+)/) : u.match(/(:)(\d+)/);
- const port = op && op.length === 3 && op[1] && op[2] ? parseInt(op[2]) : 4222;
- const protocol = port === 80 ? "https" : "http";
- const url = new URL(`${protocol}://${u}`);
- url.port = `${port}`;
- let hostname = url.hostname;
- if (hostname.charAt(0) === "[") {
- hostname = hostname.substring(1, hostname.length - 1);
- }
- const listen = url.host;
- return {
- listen,
- hostname,
- port
- };
- }
- var ServerImpl = class {
- constructor(u, gossiped = false) {
- __publicField(this, "src");
- __publicField(this, "listen");
- __publicField(this, "hostname");
- __publicField(this, "port");
- __publicField(this, "didConnect");
- __publicField(this, "reconnects");
- __publicField(this, "lastConnect");
- __publicField(this, "gossiped");
- __publicField(this, "tlsName");
- __publicField(this, "resolves");
- this.src = u;
- this.tlsName = "";
- const v = hostPort(u);
- this.listen = v.listen;
- this.hostname = v.hostname;
- this.port = v.port;
- this.didConnect = false;
- this.reconnects = 0;
- this.lastConnect = 0;
- this.gossiped = gossiped;
- }
- toString() {
- return this.listen;
- }
- async resolve(opts) {
- if (!opts.fn) {
- return [
- this
- ];
- }
- const buf = [];
- if (isIP(this.hostname)) {
- return [
- this
- ];
- } else {
- const ips = await opts.fn(this.hostname);
- if (opts.debug) {
- console.log(`resolve ${this.hostname} = ${ips.join(",")}`);
- }
- for (const ip of ips) {
- const proto = this.port === 80 ? "https" : "http";
- const url = new URL(`${proto}://${isIPV6(ip) ? "[" + ip + "]" : ip}`);
- url.port = `${this.port}`;
- const ss = new ServerImpl(url.host, false);
- ss.tlsName = this.hostname;
- buf.push(ss);
- }
- }
- if (opts.randomize) {
- shuffle(buf);
- }
- this.resolves = buf;
- return buf;
- }
- };
- var Servers = class {
- constructor(listens = [], opts = {}) {
- __publicField(this, "firstSelect");
- __publicField(this, "servers");
- __publicField(this, "currentServer");
- __publicField(this, "tlsName");
- __publicField(this, "randomize");
- this.firstSelect = true;
- this.servers = [];
- this.tlsName = "";
- this.randomize = opts.randomize || false;
- const urlParseFn = getUrlParseFn();
- if (listens) {
- listens.forEach((hp) => {
- hp = urlParseFn ? urlParseFn(hp) : hp;
- this.servers.push(new ServerImpl(hp));
- });
- if (this.randomize) {
- this.servers = shuffle(this.servers);
- }
- }
- if (this.servers.length === 0) {
- this.addServer(`${DEFAULT_HOST}:${defaultPort()}`, false);
- }
- this.currentServer = this.servers[0];
- }
- clear() {
- this.servers.length = 0;
- }
- updateTLSName() {
- const cs = this.getCurrentServer();
- if (!isIP(cs.hostname)) {
- this.tlsName = cs.hostname;
- this.servers.forEach((s) => {
- if (s.gossiped) {
- s.tlsName = this.tlsName;
- }
- });
- }
- }
- getCurrentServer() {
- return this.currentServer;
- }
- addServer(u, implicit = false) {
- const urlParseFn = getUrlParseFn();
- u = urlParseFn ? urlParseFn(u) : u;
- const s = new ServerImpl(u, implicit);
- if (isIP(s.hostname)) {
- s.tlsName = this.tlsName;
- }
- this.servers.push(s);
- }
- selectServer() {
- if (this.firstSelect) {
- this.firstSelect = false;
- return this.currentServer;
- }
- const t = this.servers.shift();
- if (t) {
- this.servers.push(t);
- this.currentServer = t;
- }
- return t;
- }
- removeCurrentServer() {
- this.removeServer(this.currentServer);
- }
- removeServer(server) {
- if (server) {
- const index = this.servers.indexOf(server);
- this.servers.splice(index, 1);
- }
- }
- length() {
- return this.servers.length;
- }
- next() {
- return this.servers.length ? this.servers[0] : void 0;
- }
- getServers() {
- return this.servers;
- }
- update(info) {
- const added = [];
- let deleted = [];
- const urlParseFn = getUrlParseFn();
- const discovered = /* @__PURE__ */ new Map();
- if (info.connect_urls && info.connect_urls.length > 0) {
- info.connect_urls.forEach((hp) => {
- hp = urlParseFn ? urlParseFn(hp) : hp;
- const s = new ServerImpl(hp, true);
- discovered.set(hp, s);
- });
- }
- const toDelete = [];
- this.servers.forEach((s, index) => {
- const u = s.listen;
- if (s.gossiped && this.currentServer.listen !== u && discovered.get(u) === void 0) {
- toDelete.push(index);
- }
- discovered.delete(u);
- });
- toDelete.reverse();
- toDelete.forEach((index) => {
- const removed = this.servers.splice(index, 1);
- deleted = deleted.concat(removed[0].listen);
- });
- discovered.forEach((v, k) => {
- this.servers.push(v);
- added.push(k);
- });
- return {
- added,
- deleted
- };
- }
- };
- var QueuedIteratorImpl = class {
- constructor() {
- __publicField(this, "inflight");
- __publicField(this, "processed");
- __publicField(this, "received");
- __publicField(this, "noIterator");
- __publicField(this, "iterClosed");
- __publicField(this, "done");
- __publicField(this, "signal");
- __publicField(this, "yields");
- __publicField(this, "filtered");
- __publicField(this, "pendingFiltered");
- __publicField(this, "ingestionFilterFn");
- __publicField(this, "protocolFilterFn");
- __publicField(this, "dispatchedFn");
- __publicField(this, "ctx");
- __publicField(this, "_data");
- __publicField(this, "err");
- __publicField(this, "time");
- __publicField(this, "yielding");
- this.inflight = 0;
- this.filtered = 0;
- this.pendingFiltered = 0;
- this.processed = 0;
- this.received = 0;
- this.noIterator = false;
- this.done = false;
- this.signal = deferred();
- this.yields = [];
- this.iterClosed = deferred();
- this.time = 0;
- this.yielding = false;
- }
- [Symbol.asyncIterator]() {
- return this.iterate();
- }
- push(v) {
- if (this.done) {
- return;
- }
- if (typeof v === "function") {
- this.yields.push(v);
- this.signal.resolve();
- return;
- }
- const { ingest, protocol } = this.ingestionFilterFn ? this.ingestionFilterFn(v, this.ctx || this) : {
- ingest: true,
- protocol: false
- };
- if (ingest) {
- if (protocol) {
- this.filtered++;
- this.pendingFiltered++;
- }
- this.yields.push(v);
- this.signal.resolve();
- }
- }
- async *iterate() {
- if (this.noIterator) {
- throw new NatsError("unsupported iterator", ErrorCode.ApiError);
- }
- if (this.yielding) {
- throw new NatsError("already yielding", ErrorCode.ApiError);
- }
- this.yielding = true;
- try {
- while (true) {
- if (this.yields.length === 0) {
- await this.signal;
- }
- if (this.err) {
- throw this.err;
- }
- const yields = this.yields;
- this.inflight = yields.length;
- this.yields = [];
- for (let i = 0; i < yields.length; i++) {
- if (typeof yields[i] === "function") {
- const fn = yields[i];
- try {
- fn();
- } catch (err) {
- throw err;
- }
- if (this.err) {
- throw this.err;
- }
- continue;
- }
- const ok = this.protocolFilterFn ? this.protocolFilterFn(yields[i]) : true;
- if (ok) {
- this.processed++;
- const start = Date.now();
- yield yields[i];
- this.time = Date.now() - start;
- if (this.dispatchedFn && yields[i]) {
- this.dispatchedFn(yields[i]);
- }
- } else {
- this.pendingFiltered--;
- }
- this.inflight--;
- }
- if (this.done) {
- break;
- } else if (this.yields.length === 0) {
- yields.length = 0;
- this.yields = yields;
- this.signal = deferred();
- }
- }
- } finally {
- this.stop();
- }
- }
- stop(err) {
- if (this.done) {
- return;
- }
- this.err = err;
- this.done = true;
- this.signal.resolve();
- this.iterClosed.resolve();
- }
- getProcessed() {
- return this.noIterator ? this.received : this.processed;
- }
- getPending() {
- return this.yields.length + this.inflight - this.pendingFiltered;
- }
- getReceived() {
- return this.received - this.filtered;
- }
- };
- function canonicalMIMEHeaderKey(k) {
- const dash = 45;
- const toLower = 97 - 65;
- let upper = true;
- const buf = new Array(k.length);
- for (let i = 0; i < k.length; i++) {
- let c = k.charCodeAt(i);
- if (c === 58 || c < 33 || c > 126) {
- throw new NatsError(`'${k[i]}' is not a valid character for a header key`, ErrorCode.BadHeader);
- }
- if (upper && 97 <= c && c <= 122) {
- c -= toLower;
- } else if (!upper && 65 <= c && c <= 90) {
- c += toLower;
- }
- buf[i] = c;
- upper = c == dash;
- }
- return String.fromCharCode(...buf);
- }
- function headers(code2 = 0, description = "") {
- if (code2 === 0 && description !== "" || code2 > 0 && description === "") {
- throw new Error("setting status requires both code and description");
- }
- return new MsgHdrsImpl(code2, description);
- }
- var HEADER = "NATS/1.0";
- var MsgHdrsImpl = class {
- constructor(code2 = 0, description = "") {
- __publicField(this, "_code");
- __publicField(this, "headers");
- __publicField(this, "_description");
- this._code = code2;
- this._description = description;
- this.headers = /* @__PURE__ */ new Map();
- }
- [Symbol.iterator]() {
- return this.headers.entries();
- }
- size() {
- return this.headers.size;
- }
- equals(mh) {
- if (mh && this.headers.size === mh.headers.size && this._code === mh._code) {
- for (const [k, v] of this.headers) {
- const a = mh.values(k);
- if (v.length !== a.length) {
- return false;
- }
- const vv = [
- ...v
- ].sort();
- const aa = [
- ...a
- ].sort();
- for (let i = 0; i < vv.length; i++) {
- if (vv[i] !== aa[i]) {
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- static decode(a) {
- const mh = new MsgHdrsImpl();
- const s = TD.decode(a);
- const lines = s.split("\r\n");
- const h = lines[0];
- if (h !== HEADER) {
- let str = h.replace(HEADER, "").trim();
- if (str.length > 0) {
- mh._code = parseInt(str, 10);
- if (isNaN(mh._code)) {
- mh._code = 0;
- }
- const scode = mh._code.toString();
- str = str.replace(scode, "");
- mh._description = str.trim();
- }
- }
- if (lines.length >= 1) {
- lines.slice(1).map((s2) => {
- if (s2) {
- const idx = s2.indexOf(":");
- if (idx > -1) {
- const k = s2.slice(0, idx);
- const v = s2.slice(idx + 1).trim();
- mh.append(k, v);
- }
- }
- });
- }
- return mh;
- }
- toString() {
- if (this.headers.size === 0 && this._code === 0) {
- return "";
- }
- let s = HEADER;
- if (this._code > 0 && this._description !== "") {
- s += ` ${this._code} ${this._description}`;
- }
- for (const [k, v] of this.headers) {
- for (let i = 0; i < v.length; i++) {
- s = `${s}\r
- ${k}: ${v[i]}`;
- }
- }
- return `${s}\r
- \r
- `;
- }
- encode() {
- return TE.encode(this.toString());
- }
- static validHeaderValue(k) {
- const inv = /[\r\n]/;
- if (inv.test(k)) {
- throw new NatsError("invalid header value - \\r and \\n are not allowed.", ErrorCode.BadHeader);
- }
- return k.trim();
- }
- keys() {
- const keys = [];
- for (const sk of this.headers.keys()) {
- keys.push(sk);
- }
- return keys;
- }
- findKeys(k, match = Match.Exact) {
- const keys = this.keys();
- switch (match) {
- case Match.Exact:
- return keys.filter((v) => {
- return v === k;
- });
- case Match.CanonicalMIME:
- k = canonicalMIMEHeaderKey(k);
- return keys.filter((v) => {
- return v === k;
- });
- default: {
- const lci = k.toLowerCase();
- return keys.filter((v) => {
- return lci === v.toLowerCase();
- });
- }
- }
- }
- get(k, match = Match.Exact) {
- const keys = this.findKeys(k, match);
- if (keys.length) {
- const v = this.headers.get(keys[0]);
- if (v) {
- return Array.isArray(v) ? v[0] : v;
- }
- }
- return "";
- }
- last(k, match = Match.Exact) {
- const keys = this.findKeys(k, match);
- if (keys.length) {
- const v = this.headers.get(keys[0]);
- if (v) {
- return Array.isArray(v) ? v[v.length - 1] : v;
- }
- }
- return "";
- }
- has(k, match = Match.Exact) {
- return this.findKeys(k, match).length > 0;
- }
- set(k, v, match = Match.Exact) {
- this.delete(k, match);
- this.append(k, v, match);
- }
- append(k, v, match = Match.Exact) {
- const ck = canonicalMIMEHeaderKey(k);
- if (match === Match.CanonicalMIME) {
- k = ck;
- }
- const keys = this.findKeys(k, match);
- k = keys.length > 0 ? keys[0] : k;
- const value = MsgHdrsImpl.validHeaderValue(v);
- let a = this.headers.get(k);
- if (!a) {
- a = [];
- this.headers.set(k, a);
- }
- a.push(value);
- }
- values(k, match = Match.Exact) {
- const buf = [];
- const keys = this.findKeys(k, match);
- keys.forEach((v) => {
- const values = this.headers.get(v);
- if (values) {
- buf.push(...values);
- }
- });
- return buf;
- }
- delete(k, match = Match.Exact) {
- const keys = this.findKeys(k, match);
- keys.forEach((v) => {
- this.headers.delete(v);
- });
- }
- get hasError() {
- return this._code >= 300;
- }
- get status() {
- return `${this._code} ${this._description}`.trim();
- }
- toRecord() {
- const data = {};
- this.keys().forEach((v) => {
- data[v] = this.values(v);
- });
- return data;
- }
- get code() {
- return this._code;
- }
- get description() {
- return this._description;
- }
- static fromRecord(r) {
- const h = new MsgHdrsImpl();
- for (const k in r) {
- h.headers.set(k, r[k]);
- }
- return h;
- }
- };
- function StringCodec() {
- return {
- encode(d) {
- return TE.encode(d);
- },
- decode(a) {
- return TD.decode(a);
- }
- };
- }
- function JSONCodec(reviver) {
- return {
- encode(d) {
- try {
- if (d === void 0) {
- d = null;
- }
- return TE.encode(JSON.stringify(d));
- } catch (err) {
- throw NatsError.errorForCode(ErrorCode.BadJson, err);
- }
- },
- decode(a) {
- try {
- return JSON.parse(TD.decode(a), reviver);
- } catch (err) {
- throw NatsError.errorForCode(ErrorCode.BadJson, err);
- }
- }
- };
- }
- function isRequestError(msg) {
- var _a;
- if (msg && msg.data.length === 0 && ((_a = msg.headers) == null ? void 0 : _a.code) === 503) {
- return NatsError.errorForCode(ErrorCode.NoResponders);
- }
- return null;
- }
- var MsgImpl = class {
- constructor(msg, data, publisher) {
- __publicField(this, "_headers");
- __publicField(this, "_msg");
- __publicField(this, "_rdata");
- __publicField(this, "_reply");
- __publicField(this, "_subject");
- __publicField(this, "publisher");
- this._msg = msg;
- this._rdata = data;
- this.publisher = publisher;
- }
- get subject() {
- if (this._subject) {
- return this._subject;
- }
- this._subject = TD.decode(this._msg.subject);
- return this._subject;
- }
- get reply() {
- if (this._reply) {
- return this._reply;
- }
- this._reply = TD.decode(this._msg.reply);
- return this._reply;
- }
- get sid() {
- return this._msg.sid;
- }
- get headers() {
- if (this._msg.hdr > -1 && !this._headers) {
- const buf = this._rdata.subarray(0, this._msg.hdr);
- this._headers = MsgHdrsImpl.decode(buf);
- }
- return this._headers;
- }
- get data() {
- if (!this._rdata) {
- return new Uint8Array(0);
- }
- return this._msg.hdr > -1 ? this._rdata.subarray(this._msg.hdr) : this._rdata;
- }
- respond(data = Empty, opts) {
- if (this.reply) {
- this.publisher.publish(this.reply, data, opts);
- return true;
- }
- return false;
- }
- size() {
- var _a;
- const subj = this._msg.subject.length;
- const reply = ((_a = this._msg.reply) == null ? void 0 : _a.length) || 0;
- const payloadAndHeaders = this._msg.size === -1 ? 0 : this._msg.size;
- return subj + reply + payloadAndHeaders;
- }
- json(reviver) {
- return JSONCodec(reviver).decode(this.data);
- }
- string() {
- return TD.decode(this.data);
- }
- };
- __publicField(MsgImpl, "jc");
- var MuxSubscription = class {
- constructor() {
- __publicField(this, "baseInbox");
- __publicField(this, "reqs");
- this.reqs = /* @__PURE__ */ new Map();
- }
- size() {
- return this.reqs.size;
- }
- init(prefix) {
- this.baseInbox = `${createInbox(prefix)}.`;
- return this.baseInbox;
- }
- add(r) {
- if (!isNaN(r.received)) {
- r.received = 0;
- }
- this.reqs.set(r.token, r);
- }
- get(token) {
- return this.reqs.get(token);
- }
- cancel(r) {
- this.reqs.delete(r.token);
- }
- getToken(m) {
- const s = m.subject || "";
- if (s.indexOf(this.baseInbox) === 0) {
- return s.substring(this.baseInbox.length);
- }
- return null;
- }
- all() {
- return Array.from(this.reqs.values());
- }
- handleError(isMuxPermissionError, err) {
- if (err && err.permissionContext) {
- if (isMuxPermissionError) {
- this.all().forEach((r) => {
- r.resolver(err, {});
- });
- return true;
- }
- const ctx = err.permissionContext;
- if (ctx.operation === "publish") {
- const req = this.all().find((s) => {
- return s.requestSubject === ctx.subject;
- });
- if (req) {
- req.resolver(err, {});
- return true;
- }
- }
- }
- return false;
- }
- dispatcher() {
- return (err, m) => {
- const token = this.getToken(m);
- if (token) {
- const r = this.get(token);
- if (r) {
- if (err === null && m.headers) {
- err = isRequestError(m);
- }
- r.resolver(err, m);
- }
- }
- };
- }
- close() {
- const err = NatsError.errorForCode(ErrorCode.Timeout);
- this.reqs.forEach((req) => {
- req.resolver(err, {});
- });
- }
- };
- var Heartbeat = class {
- constructor(ph, interval, maxOut) {
- __publicField(this, "ph");
- __publicField(this, "interval");
- __publicField(this, "maxOut");
- __publicField(this, "timer");
- __publicField(this, "pendings");
- this.ph = ph;
- this.interval = interval;
- this.maxOut = maxOut;
- this.pendings = [];
- }
- start() {
- this.cancel();
- this._schedule();
- }
- cancel(stale) {
- if (this.timer) {
- clearTimeout(this.timer);
- this.timer = void 0;
- }
- this._reset();
- if (stale) {
- this.ph.disconnect();
- }
- }
- _schedule() {
- this.timer = setTimeout(() => {
- this.ph.dispatchStatus({
- type: DebugEvents.PingTimer,
- data: `${this.pendings.length + 1}`
- });
- if (this.pendings.length === this.maxOut) {
- this.cancel(true);
- return;
- }
- const ping = deferred();
- this.ph.flush(ping).then(() => {
- this._reset();
- }).catch(() => {
- this.cancel();
- });
- this.pendings.push(ping);
- this._schedule();
- }, this.interval);
- }
- _reset() {
- this.pendings = this.pendings.filter((p) => {
- const d = p;
- d.resolve();
- return false;
- });
- }
- };
- var AssertionError = class extends Error {
- constructor(msg) {
- super(msg);
- this.name = "AssertionError";
- }
- };
- function assert(cond, msg = "Assertion failed.") {
- if (!cond) {
- throw new AssertionError(msg);
- }
- }
- var MIN_READ = 32 * 1024;
- var MAX_SIZE = 2 ** 32 - 2;
- function copy(src, dst, off = 0) {
- const r = dst.byteLength - off;
- if (src.byteLength > r) {
- src = src.subarray(0, r);
- }
- dst.set(src, off);
- return src.byteLength;
- }
- var DenoBuffer = class {
- constructor(ab) {
- __publicField(this, "_buf");
- __publicField(this, "_off");
- this._off = 0;
- if (ab == null) {
- this._buf = new Uint8Array(0);
- return;
- }
- this._buf = new Uint8Array(ab);
- }
- bytes(options = {
- copy: true
- }) {
- if (options.copy === false)
- return this._buf.subarray(this._off);
- return this._buf.slice(this._off);
- }
- empty() {
- return this._buf.byteLength <= this._off;
- }
- get length() {
- return this._buf.byteLength - this._off;
- }
- get capacity() {
- return this._buf.buffer.byteLength;
- }
- truncate(n) {
- if (n === 0) {
- this.reset();
- return;
- }
- if (n < 0 || n > this.length) {
- throw Error("bytes.Buffer: truncation out of range");
- }
- this._reslice(this._off + n);
- }
- reset() {
- this._reslice(0);
- this._off = 0;
- }
- _tryGrowByReslice(n) {
- const l = this._buf.byteLength;
- if (n <= this.capacity - l) {
- this._reslice(l + n);
- return l;
- }
- return -1;
- }
- _reslice(len) {
- assert(len <= this._buf.buffer.byteLength);
- this._buf = new Uint8Array(this._buf.buffer, 0, len);
- }
- readByte() {
- const a = new Uint8Array(1);
- if (this.read(a)) {
- return a[0];
- }
- return null;
- }
- read(p) {
- if (this.empty()) {
- this.reset();
- if (p.byteLength === 0) {
- return 0;
- }
- return null;
- }
- const nread = copy(this._buf.subarray(this._off), p);
- this._off += nread;
- return nread;
- }
- writeByte(n) {
- return this.write(Uint8Array.of(n));
- }
- writeString(s) {
- return this.write(TE.encode(s));
- }
- write(p) {
- const m = this._grow(p.byteLength);
- return copy(p, this._buf, m);
- }
- _grow(n) {
- const m = this.length;
- if (m === 0 && this._off !== 0) {
- this.reset();
- }
- const i = this._tryGrowByReslice(n);
- if (i >= 0) {
- return i;
- }
- const c = this.capacity;
- if (n <= Math.floor(c / 2) - m) {
- copy(this._buf.subarray(this._off), this._buf);
- } else if (c + n > MAX_SIZE) {
- throw new Error("The buffer cannot be grown beyond the maximum size.");
- } else {
- const buf = new Uint8Array(Math.min(2 * c + n, MAX_SIZE));
- copy(this._buf.subarray(this._off), buf);
- this._buf = buf;
- }
- this._off = 0;
- this._reslice(Math.min(m + n, MAX_SIZE));
- return m;
- }
- grow(n) {
- if (n < 0) {
- throw Error("Buffer._grow: negative count");
- }
- const m = this._grow(n);
- this._reslice(m);
- }
- readFrom(r) {
- let n = 0;
- const tmp = new Uint8Array(MIN_READ);
- while (true) {
- const shouldGrow = this.capacity - this.length < MIN_READ;
- const buf = shouldGrow ? tmp : new Uint8Array(this._buf.buffer, this.length);
- const nread = r.read(buf);
- if (nread === null) {
- return n;
- }
- if (shouldGrow)
- this.write(buf.subarray(0, nread));
- else
- this._reslice(this.length + nread);
- n += nread;
- }
- }
- };
- var Kind;
- (function(Kind2) {
- Kind2[Kind2["OK"] = 0] = "OK";
- Kind2[Kind2["ERR"] = 1] = "ERR";
- Kind2[Kind2["MSG"] = 2] = "MSG";
- Kind2[Kind2["INFO"] = 3] = "INFO";
- Kind2[Kind2["PING"] = 4] = "PING";
- Kind2[Kind2["PONG"] = 5] = "PONG";
- })(Kind || (Kind = {}));
- function newMsgArg() {
- const ma = {};
- ma.sid = -1;
- ma.hdr = -1;
- ma.size = -1;
- return ma;
- }
- var ASCII_0 = 48;
- var Parser = class {
- constructor(dispatcher) {
- __publicField(this, "dispatcher");
- __publicField(this, "state");
- __publicField(this, "as");
- __publicField(this, "drop");
- __publicField(this, "hdr");
- __publicField(this, "ma");
- __publicField(this, "argBuf");
- __publicField(this, "msgBuf");
- this.dispatcher = dispatcher;
- this.state = State.OP_START;
- this.as = 0;
- this.drop = 0;
- this.hdr = 0;
- }
- parse(buf) {
- let i;
- for (i = 0; i < buf.length; i++) {
- const b = buf[i];
- switch (this.state) {
- case State.OP_START:
- switch (b) {
- case cc.M:
- case cc.m:
- this.state = State.OP_M;
- this.hdr = -1;
- this.ma = newMsgArg();
- break;
- case cc.H:
- case cc.h:
- this.state = State.OP_H;
- this.hdr = 0;
- this.ma = newMsgArg();
- break;
- case cc.P:
- case cc.p:
- this.state = State.OP_P;
- break;
- case cc.PLUS:
- this.state = State.OP_PLUS;
- break;
- case cc.MINUS:
- this.state = State.OP_MINUS;
- break;
- case cc.I:
- case cc.i:
- this.state = State.OP_I;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_H:
- switch (b) {
- case cc.M:
- case cc.m:
- this.state = State.OP_M;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_M:
- switch (b) {
- case cc.S:
- case cc.s:
- this.state = State.OP_MS;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MS:
- switch (b) {
- case cc.G:
- case cc.g:
- this.state = State.OP_MSG;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MSG:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- this.state = State.OP_MSG_SPC;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MSG_SPC:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- continue;
- default:
- this.state = State.MSG_ARG;
- this.as = i;
- }
- break;
- case State.MSG_ARG:
- switch (b) {
- case cc.CR:
- this.drop = 1;
- break;
- case cc.NL: {
- const arg = this.argBuf ? this.argBuf.bytes() : buf.subarray(this.as, i - this.drop);
- this.processMsgArgs(arg);
- this.drop = 0;
- this.as = i + 1;
- this.state = State.MSG_PAYLOAD;
- i = this.as + this.ma.size - 1;
- break;
- }
- default:
- if (this.argBuf) {
- this.argBuf.writeByte(b);
- }
- }
- break;
- case State.MSG_PAYLOAD:
- if (this.msgBuf) {
- if (this.msgBuf.length >= this.ma.size) {
- const data = this.msgBuf.bytes({
- copy: false
- });
- this.dispatcher.push({
- kind: Kind.MSG,
- msg: this.ma,
- data
- });
- this.argBuf = void 0;
- this.msgBuf = void 0;
- this.state = State.MSG_END;
- } else {
- let toCopy = this.ma.size - this.msgBuf.length;
- const avail = buf.length - i;
- if (avail < toCopy) {
- toCopy = avail;
- }
- if (toCopy > 0) {
- this.msgBuf.write(buf.subarray(i, i + toCopy));
- i = i + toCopy - 1;
- } else {
- this.msgBuf.writeByte(b);
- }
- }
- } else if (i - this.as >= this.ma.size) {
- this.dispatcher.push({
- kind: Kind.MSG,
- msg: this.ma,
- data: buf.subarray(this.as, i)
- });
- this.argBuf = void 0;
- this.msgBuf = void 0;
- this.state = State.MSG_END;
- }
- break;
- case State.MSG_END:
- switch (b) {
- case cc.NL:
- this.drop = 0;
- this.as = i + 1;
- this.state = State.OP_START;
- break;
- default:
- continue;
- }
- break;
- case State.OP_PLUS:
- switch (b) {
- case cc.O:
- case cc.o:
- this.state = State.OP_PLUS_O;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PLUS_O:
- switch (b) {
- case cc.K:
- case cc.k:
- this.state = State.OP_PLUS_OK;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PLUS_OK:
- switch (b) {
- case cc.NL:
- this.dispatcher.push({
- kind: Kind.OK
- });
- this.drop = 0;
- this.state = State.OP_START;
- break;
- }
- break;
- case State.OP_MINUS:
- switch (b) {
- case cc.E:
- case cc.e:
- this.state = State.OP_MINUS_E;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MINUS_E:
- switch (b) {
- case cc.R:
- case cc.r:
- this.state = State.OP_MINUS_ER;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MINUS_ER:
- switch (b) {
- case cc.R:
- case cc.r:
- this.state = State.OP_MINUS_ERR;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MINUS_ERR:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- this.state = State.OP_MINUS_ERR_SPC;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_MINUS_ERR_SPC:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- continue;
- default:
- this.state = State.MINUS_ERR_ARG;
- this.as = i;
- }
- break;
- case State.MINUS_ERR_ARG:
- switch (b) {
- case cc.CR:
- this.drop = 1;
- break;
- case cc.NL: {
- let arg;
- if (this.argBuf) {
- arg = this.argBuf.bytes();
- this.argBuf = void 0;
- } else {
- arg = buf.subarray(this.as, i - this.drop);
- }
- this.dispatcher.push({
- kind: Kind.ERR,
- data: arg
- });
- this.drop = 0;
- this.as = i + 1;
- this.state = State.OP_START;
- break;
- }
- default:
- if (this.argBuf) {
- this.argBuf.write(Uint8Array.of(b));
- }
- }
- break;
- case State.OP_P:
- switch (b) {
- case cc.I:
- case cc.i:
- this.state = State.OP_PI;
- break;
- case cc.O:
- case cc.o:
- this.state = State.OP_PO;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PO:
- switch (b) {
- case cc.N:
- case cc.n:
- this.state = State.OP_PON;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PON:
- switch (b) {
- case cc.G:
- case cc.g:
- this.state = State.OP_PONG;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PONG:
- switch (b) {
- case cc.NL:
- this.dispatcher.push({
- kind: Kind.PONG
- });
- this.drop = 0;
- this.state = State.OP_START;
- break;
- }
- break;
- case State.OP_PI:
- switch (b) {
- case cc.N:
- case cc.n:
- this.state = State.OP_PIN;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PIN:
- switch (b) {
- case cc.G:
- case cc.g:
- this.state = State.OP_PING;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_PING:
- switch (b) {
- case cc.NL:
- this.dispatcher.push({
- kind: Kind.PING
- });
- this.drop = 0;
- this.state = State.OP_START;
- break;
- }
- break;
- case State.OP_I:
- switch (b) {
- case cc.N:
- case cc.n:
- this.state = State.OP_IN;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_IN:
- switch (b) {
- case cc.F:
- case cc.f:
- this.state = State.OP_INF;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_INF:
- switch (b) {
- case cc.O:
- case cc.o:
- this.state = State.OP_INFO;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_INFO:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- this.state = State.OP_INFO_SPC;
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- break;
- case State.OP_INFO_SPC:
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- continue;
- default:
- this.state = State.INFO_ARG;
- this.as = i;
- }
- break;
- case State.INFO_ARG:
- switch (b) {
- case cc.CR:
- this.drop = 1;
- break;
- case cc.NL: {
- let arg;
- if (this.argBuf) {
- arg = this.argBuf.bytes();
- this.argBuf = void 0;
- } else {
- arg = buf.subarray(this.as, i - this.drop);
- }
- this.dispatcher.push({
- kind: Kind.INFO,
- data: arg
- });
- this.drop = 0;
- this.as = i + 1;
- this.state = State.OP_START;
- break;
- }
- default:
- if (this.argBuf) {
- this.argBuf.writeByte(b);
- }
- }
- break;
- default:
- throw this.fail(buf.subarray(i));
- }
- }
- if ((this.state === State.MSG_ARG || this.state === State.MINUS_ERR_ARG || this.state === State.INFO_ARG) && !this.argBuf) {
- this.argBuf = new DenoBuffer(buf.subarray(this.as, i - this.drop));
- }
- if (this.state === State.MSG_PAYLOAD && !this.msgBuf) {
- if (!this.argBuf) {
- this.cloneMsgArg();
- }
- this.msgBuf = new DenoBuffer(buf.subarray(this.as));
- }
- }
- cloneMsgArg() {
- const s = this.ma.subject.length;
- const r = this.ma.reply ? this.ma.reply.length : 0;
- const buf = new Uint8Array(s + r);
- buf.set(this.ma.subject);
- if (this.ma.reply) {
- buf.set(this.ma.reply, s);
- }
- this.argBuf = new DenoBuffer(buf);
- this.ma.subject = buf.subarray(0, s);
- if (this.ma.reply) {
- this.ma.reply = buf.subarray(s);
- }
- }
- processMsgArgs(arg) {
- if (this.hdr >= 0) {
- return this.processHeaderMsgArgs(arg);
- }
- const args = [];
- let start = -1;
- for (let i = 0; i < arg.length; i++) {
- const b = arg[i];
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- case cc.CR:
- case cc.NL:
- if (start >= 0) {
- args.push(arg.subarray(start, i));
- start = -1;
- }
- break;
- default:
- if (start < 0) {
- start = i;
- }
- }
- }
- if (start >= 0) {
- args.push(arg.subarray(start));
- }
- switch (args.length) {
- case 3:
- this.ma.subject = args[0];
- this.ma.sid = this.protoParseInt(args[1]);
- this.ma.reply = void 0;
- this.ma.size = this.protoParseInt(args[2]);
- break;
- case 4:
- this.ma.subject = args[0];
- this.ma.sid = this.protoParseInt(args[1]);
- this.ma.reply = args[2];
- this.ma.size = this.protoParseInt(args[3]);
- break;
- default:
- throw this.fail(arg, "processMsgArgs Parse Error");
- }
- if (this.ma.sid < 0) {
- throw this.fail(arg, "processMsgArgs Bad or Missing Sid Error");
- }
- if (this.ma.size < 0) {
- throw this.fail(arg, "processMsgArgs Bad or Missing Size Error");
- }
- }
- fail(data, label = "") {
- if (!label) {
- label = `parse error [${this.state}]`;
- } else {
- label = `${label} [${this.state}]`;
- }
- return new Error(`${label}: ${TD.decode(data)}`);
- }
- processHeaderMsgArgs(arg) {
- const args = [];
- let start = -1;
- for (let i = 0; i < arg.length; i++) {
- const b = arg[i];
- switch (b) {
- case cc.SPACE:
- case cc.TAB:
- case cc.CR:
- case cc.NL:
- if (start >= 0) {
- args.push(arg.subarray(start, i));
- start = -1;
- }
- break;
- default:
- if (start < 0) {
- start = i;
- }
- }
- }
- if (start >= 0) {
- args.push(arg.subarray(start));
- }
- switch (args.length) {
- case 4:
- this.ma.subject = args[0];
- this.ma.sid = this.protoParseInt(args[1]);
- this.ma.reply = void 0;
- this.ma.hdr = this.protoParseInt(args[2]);
- this.ma.size = this.protoParseInt(args[3]);
- break;
- case 5:
- this.ma.subject = args[0];
- this.ma.sid = this.protoParseInt(args[1]);
- this.ma.reply = args[2];
- this.ma.hdr = this.protoParseInt(args[3]);
- this.ma.size = this.protoParseInt(args[4]);
- break;
- default:
- throw this.fail(arg, "processHeaderMsgArgs Parse Error");
- }
- if (this.ma.sid < 0) {
- throw this.fail(arg, "processHeaderMsgArgs Bad or Missing Sid Error");
- }
- if (this.ma.hdr < 0 || this.ma.hdr > this.ma.size) {
- throw this.fail(arg, "processHeaderMsgArgs Bad or Missing Header Size Error");
- }
- if (this.ma.size < 0) {
- throw this.fail(arg, "processHeaderMsgArgs Bad or Missing Size Error");
- }
- }
- protoParseInt(a) {
- if (a.length === 0) {
- return -1;
- }
- let n = 0;
- for (let i = 0; i < a.length; i++) {
- if (a[i] < 48 || a[i] > 57) {
- return -1;
- }
- n = n * 10 + (a[i] - ASCII_0);
- }
- return n;
- }
- };
- var State;
- (function(State2) {
- State2[State2["OP_START"] = 0] = "OP_START";
- State2[State2["OP_PLUS"] = 1] = "OP_PLUS";
- State2[State2["OP_PLUS_O"] = 2] = "OP_PLUS_O";
- State2[State2["OP_PLUS_OK"] = 3] = "OP_PLUS_OK";
- State2[State2["OP_MINUS"] = 4] = "OP_MINUS";
- State2[State2["OP_MINUS_E"] = 5] = "OP_MINUS_E";
- State2[State2["OP_MINUS_ER"] = 6] = "OP_MINUS_ER";
- State2[State2["OP_MINUS_ERR"] = 7] = "OP_MINUS_ERR";
- State2[State2["OP_MINUS_ERR_SPC"] = 8] = "OP_MINUS_ERR_SPC";
- State2[State2["MINUS_ERR_ARG"] = 9] = "MINUS_ERR_ARG";
- State2[State2["OP_M"] = 10] = "OP_M";
- State2[State2["OP_MS"] = 11] = "OP_MS";
- State2[State2["OP_MSG"] = 12] = "OP_MSG";
- State2[State2["OP_MSG_SPC"] = 13] = "OP_MSG_SPC";
- State2[State2["MSG_ARG"] = 14] = "MSG_ARG";
- State2[State2["MSG_PAYLOAD"] = 15] = "MSG_PAYLOAD";
- State2[State2["MSG_END"] = 16] = "MSG_END";
- State2[State2["OP_H"] = 17] = "OP_H";
- State2[State2["OP_P"] = 18] = "OP_P";
- State2[State2["OP_PI"] = 19] = "OP_PI";
- State2[State2["OP_PIN"] = 20] = "OP_PIN";
- State2[State2["OP_PING"] = 21] = "OP_PING";
- State2[State2["OP_PO"] = 22] = "OP_PO";
- State2[State2["OP_PON"] = 23] = "OP_PON";
- State2[State2["OP_PONG"] = 24] = "OP_PONG";
- State2[State2["OP_I"] = 25] = "OP_I";
- State2[State2["OP_IN"] = 26] = "OP_IN";
- State2[State2["OP_INF"] = 27] = "OP_INF";
- State2[State2["OP_INFO"] = 28] = "OP_INFO";
- State2[State2["OP_INFO_SPC"] = 29] = "OP_INFO_SPC";
- State2[State2["INFO_ARG"] = 30] = "INFO_ARG";
- })(State || (State = {}));
- var cc;
- (function(cc2) {
- cc2[cc2["CR"] = "\r".charCodeAt(0)] = "CR";
- cc2[cc2["E"] = "E".charCodeAt(0)] = "E";
- cc2[cc2["e"] = "e".charCodeAt(0)] = "e";
- cc2[cc2["F"] = "F".charCodeAt(0)] = "F";
- cc2[cc2["f"] = "f".charCodeAt(0)] = "f";
- cc2[cc2["G"] = "G".charCodeAt(0)] = "G";
- cc2[cc2["g"] = "g".charCodeAt(0)] = "g";
- cc2[cc2["H"] = "H".charCodeAt(0)] = "H";
- cc2[cc2["h"] = "h".charCodeAt(0)] = "h";
- cc2[cc2["I"] = "I".charCodeAt(0)] = "I";
- cc2[cc2["i"] = "i".charCodeAt(0)] = "i";
- cc2[cc2["K"] = "K".charCodeAt(0)] = "K";
- cc2[cc2["k"] = "k".charCodeAt(0)] = "k";
- cc2[cc2["M"] = "M".charCodeAt(0)] = "M";
- cc2[cc2["m"] = "m".charCodeAt(0)] = "m";
- cc2[cc2["MINUS"] = "-".charCodeAt(0)] = "MINUS";
- cc2[cc2["N"] = "N".charCodeAt(0)] = "N";
- cc2[cc2["n"] = "n".charCodeAt(0)] = "n";
- cc2[cc2["NL"] = "\n".charCodeAt(0)] = "NL";
- cc2[cc2["O"] = "O".charCodeAt(0)] = "O";
- cc2[cc2["o"] = "o".charCodeAt(0)] = "o";
- cc2[cc2["P"] = "P".charCodeAt(0)] = "P";
- cc2[cc2["p"] = "p".charCodeAt(0)] = "p";
- cc2[cc2["PLUS"] = "+".charCodeAt(0)] = "PLUS";
- cc2[cc2["R"] = "R".charCodeAt(0)] = "R";
- cc2[cc2["r"] = "r".charCodeAt(0)] = "r";
- cc2[cc2["S"] = "S".charCodeAt(0)] = "S";
- cc2[cc2["s"] = "s".charCodeAt(0)] = "s";
- cc2[cc2["SPACE"] = " ".charCodeAt(0)] = "SPACE";
- cc2[cc2["TAB"] = " ".charCodeAt(0)] = "TAB";
- })(cc || (cc = {}));
- function parseSemVer(s = "") {
- const m = s.match(/(\d+).(\d+).(\d+)/);
- if (m) {
- return {
- major: parseInt(m[1]),
- minor: parseInt(m[2]),
- micro: parseInt(m[3])
- };
- }
- throw new Error(`'${s}' is not a semver value`);
- }
- function compare(a, b) {
- if (a.major < b.major)
- return -1;
- if (a.major > b.major)
- return 1;
- if (a.minor < b.minor)
- return -1;
- if (a.minor > b.minor)
- return 1;
- if (a.micro < b.micro)
- return -1;
- if (a.micro > b.micro)
- return 1;
- return 0;
- }
- var Feature;
- (function(Feature2) {
- Feature2["JS_KV"] = "js_kv";
- Feature2["JS_OBJECTSTORE"] = "js_objectstore";
- Feature2["JS_PULL_MAX_BYTES"] = "js_pull_max_bytes";
- Feature2["JS_NEW_CONSUMER_CREATE_API"] = "js_new_consumer_create";
- Feature2["JS_ALLOW_DIRECT"] = "js_allow_direct";
- Feature2["JS_MULTIPLE_CONSUMER_FILTER"] = "js_multiple_consumer_filter";
- Feature2["JS_SIMPLIFICATION"] = "js_simplification";
- Feature2["JS_STREAM_CONSUMER_METADATA"] = "js_stream_consumer_metadata";
- Feature2["JS_CONSUMER_FILTER_SUBJECTS"] = "js_consumer_filter_subjects";
- Feature2["JS_STREAM_FIRST_SEQ"] = "js_stream_first_seq";
- Feature2["JS_STREAM_SUBJECT_TRANSFORM"] = "js_stream_subject_transform";
- Feature2["JS_STREAM_SOURCE_SUBJECT_TRANSFORM"] = "js_stream_source_subject_transform";
- Feature2["JS_STREAM_COMPRESSION"] = "js_stream_compression";
- Feature2["JS_DEFAULT_CONSUMER_LIMITS"] = "js_default_consumer_limits";
- })(Feature || (Feature = {}));
- var Features = class {
- constructor(v) {
- __publicField(this, "server");
- __publicField(this, "features");
- __publicField(this, "disabled");
- this.features = /* @__PURE__ */ new Map();
- this.disabled = [];
- this.update(v);
- }
- resetDisabled() {
- this.disabled.length = 0;
- this.update(this.server);
- }
- disable(f) {
- this.disabled.push(f);
- this.update(this.server);
- }
- isDisabled(f) {
- return this.disabled.indexOf(f) !== -1;
- }
- update(v) {
- if (typeof v === "string") {
- v = parseSemVer(v);
- }
- this.server = v;
- this.set(Feature.JS_KV, "2.6.2");
- this.set(Feature.JS_OBJECTSTORE, "2.6.3");
- this.set(Feature.JS_PULL_MAX_BYTES, "2.8.3");
- this.set(Feature.JS_NEW_CONSUMER_CREATE_API, "2.9.0");
- this.set(Feature.JS_ALLOW_DIRECT, "2.9.0");
- this.set(Feature.JS_MULTIPLE_CONSUMER_FILTER, "2.10.0");
- this.set(Feature.JS_SIMPLIFICATION, "2.9.4");
- this.set(Feature.JS_STREAM_CONSUMER_METADATA, "2.10.0");
- this.set(Feature.JS_CONSUMER_FILTER_SUBJECTS, "2.10.0");
- this.set(Feature.JS_STREAM_FIRST_SEQ, "2.10.0");
- this.set(Feature.JS_STREAM_SUBJECT_TRANSFORM, "2.10.0");
- this.set(Feature.JS_STREAM_SOURCE_SUBJECT_TRANSFORM, "2.10.0");
- this.set(Feature.JS_STREAM_COMPRESSION, "2.10.0");
- this.set(Feature.JS_DEFAULT_CONSUMER_LIMITS, "2.10.0");
- this.disabled.forEach((f) => {
- this.features.delete(f);
- });
- }
- set(f, requires) {
- this.features.set(f, {
- min: requires,
- ok: compare(this.server, parseSemVer(requires)) >= 0
- });
- }
- get(f) {
- return this.features.get(f) || {
- min: "unknown",
- ok: false
- };
- }
- supports(f) {
- var _a;
- return ((_a = this.get(f)) == null ? void 0 : _a.ok) || false;
- }
- require(v) {
- if (typeof v === "string") {
- v = parseSemVer(v);
- }
- return compare(this.server, v) >= 0;
- }
- };
- (function(nacl2) {
- "use strict";
- var u64 = function(h, l) {
- this.hi = h | 0 >>> 0;
- this.lo = l | 0 >>> 0;
- };
- var gf = function(init2) {
- var i, r = new Float64Array(16);
- if (init2)
- for (i = 0; i < init2.length; i++)
- r[i] = init2[i];
- return r;
- };
- var randombytes = function() {
- throw new Error("no PRNG");
- };
- var _0 = new Uint8Array(16);
- var _9 = new Uint8Array(32);
- _9[0] = 9;
- var gf0 = gf(), gf1 = gf([
- 1
- ]), _121665 = gf([
- 56129,
- 1
- ]), D = gf([
- 30883,
- 4953,
- 19914,
- 30187,
- 55467,
- 16705,
- 2637,
- 112,
- 59544,
- 30585,
- 16505,
- 36039,
- 65139,
- 11119,
- 27886,
- 20995
- ]), D2 = gf([
- 61785,
- 9906,
- 39828,
- 60374,
- 45398,
- 33411,
- 5274,
- 224,
- 53552,
- 61171,
- 33010,
- 6542,
- 64743,
- 22239,
- 55772,
- 9222
- ]), X = gf([
- 54554,
- 36645,
- 11616,
- 51542,
- 42930,
- 38181,
- 51040,
- 26924,
- 56412,
- 64982,
- 57905,
- 49316,
- 21502,
- 52590,
- 14035,
- 8553
- ]), Y = gf([
- 26200,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214,
- 26214
- ]), I = gf([
- 41136,
- 18958,
- 6951,
- 50414,
- 58488,
- 44335,
- 6150,
- 12099,
- 55207,
- 15867,
- 153,
- 11085,
- 57099,
- 20417,
- 9344,
- 11139
- ]);
- function L32(x, c) {
- return x << c | x >>> 32 - c;
- }
- function ld32(x, i) {
- var u = x[i + 3] & 255;
- u = u << 8 | x[i + 2] & 255;
- u = u << 8 | x[i + 1] & 255;
- return u << 8 | x[i + 0] & 255;
- }
- function dl64(x, i) {
- var h = x[i] << 24 | x[i + 1] << 16 | x[i + 2] << 8 | x[i + 3];
- var l = x[i + 4] << 24 | x[i + 5] << 16 | x[i + 6] << 8 | x[i + 7];
- return new u64(h, l);
- }
- function st32(x, j, u) {
- var i;
- for (i = 0; i < 4; i++) {
- x[j + i] = u & 255;
- u >>>= 8;
- }
- }
- function ts64(x, i, u) {
- x[i] = u.hi >> 24 & 255;
- x[i + 1] = u.hi >> 16 & 255;
- x[i + 2] = u.hi >> 8 & 255;
- x[i + 3] = u.hi & 255;
- x[i + 4] = u.lo >> 24 & 255;
- x[i + 5] = u.lo >> 16 & 255;
- x[i + 6] = u.lo >> 8 & 255;
- x[i + 7] = u.lo & 255;
- }
- function vn(x, xi, y, yi, n) {
- var i, d = 0;
- for (i = 0; i < n; i++)
- d |= x[xi + i] ^ y[yi + i];
- return (1 & d - 1 >>> 8) - 1;
- }
- function crypto_verify_16(x, xi, y, yi) {
- return vn(x, xi, y, yi, 16);
- }
- function crypto_verify_32(x, xi, y, yi) {
- return vn(x, xi, y, yi, 32);
- }
- function core(out, inp, k, c, h) {
- var w = new Uint32Array(16), x = new Uint32Array(16), y = new Uint32Array(16), t = new Uint32Array(4);
- var i, j, m;
- for (i = 0; i < 4; i++) {
- x[5 * i] = ld32(c, 4 * i);
- x[1 + i] = ld32(k, 4 * i);
- x[6 + i] = ld32(inp, 4 * i);
- x[11 + i] = ld32(k, 16 + 4 * i);
- }
- for (i = 0; i < 16; i++)
- y[i] = x[i];
- for (i = 0; i < 20; i++) {
- for (j = 0; j < 4; j++) {
- for (m = 0; m < 4; m++)
- t[m] = x[(5 * j + 4 * m) % 16];
- t[1] ^= L32(t[0] + t[3] | 0, 7);
- t[2] ^= L32(t[1] + t[0] | 0, 9);
- t[3] ^= L32(t[2] + t[1] | 0, 13);
- t[0] ^= L32(t[3] + t[2] | 0, 18);
- for (m = 0; m < 4; m++)
- w[4 * j + (j + m) % 4] = t[m];
- }
- for (m = 0; m < 16; m++)
- x[m] = w[m];
- }
- if (h) {
- for (i = 0; i < 16; i++)
- x[i] = x[i] + y[i] | 0;
- for (i = 0; i < 4; i++) {
- x[5 * i] = x[5 * i] - ld32(c, 4 * i) | 0;
- x[6 + i] = x[6 + i] - ld32(inp, 4 * i) | 0;
- }
- for (i = 0; i < 4; i++) {
- st32(out, 4 * i, x[5 * i]);
- st32(out, 16 + 4 * i, x[6 + i]);
- }
- } else {
- for (i = 0; i < 16; i++)
- st32(out, 4 * i, x[i] + y[i] | 0);
- }
- }
- function crypto_core_salsa20(out, inp, k, c) {
- core(out, inp, k, c, false);
- return 0;
- }
- function crypto_core_hsalsa20(out, inp, k, c) {
- core(out, inp, k, c, true);
- return 0;
- }
- var sigma = new Uint8Array([
- 101,
- 120,
- 112,
- 97,
- 110,
- 100,
- 32,
- 51,
- 50,
- 45,
- 98,
- 121,
- 116,
- 101,
- 32,
- 107
- ]);
- function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
- var z = new Uint8Array(16), x = new Uint8Array(64);
- var u, i;
- if (!b)
- return 0;
- for (i = 0; i < 16; i++)
- z[i] = 0;
- for (i = 0; i < 8; i++)
- z[i] = n[i];
- while (b >= 64) {
- crypto_core_salsa20(x, z, k, sigma);
- for (i = 0; i < 64; i++)
- c[cpos + i] = (m ? m[mpos + i] : 0) ^ x[i];
- u = 1;
- for (i = 8; i < 16; i++) {
- u = u + (z[i] & 255) | 0;
- z[i] = u & 255;
- u >>>= 8;
- }
- b -= 64;
- cpos += 64;
- if (m)
- mpos += 64;
- }
- if (b > 0) {
- crypto_core_salsa20(x, z, k, sigma);
- for (i = 0; i < b; i++)
- c[cpos + i] = (m ? m[mpos + i] : 0) ^ x[i];
- }
- return 0;
- }
- function crypto_stream_salsa20(c, cpos, d, n, k) {
- return crypto_stream_salsa20_xor(c, cpos, null, 0, d, n, k);
- }
- function crypto_stream(c, cpos, d, n, k) {
- var s = new Uint8Array(32);
- crypto_core_hsalsa20(s, n, k, sigma);
- return crypto_stream_salsa20(c, cpos, d, n.subarray(16), s);
- }
- function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
- var s = new Uint8Array(32);
- crypto_core_hsalsa20(s, n, k, sigma);
- return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, n.subarray(16), s);
- }
- function add1305(h, c) {
- var j, u = 0;
- for (j = 0; j < 17; j++) {
- u = u + (h[j] + c[j] | 0) | 0;
- h[j] = u & 255;
- u >>>= 8;
- }
- }
- var minusp = new Uint32Array([
- 5,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 252
- ]);
- function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
- var s, i, j, u;
- var x = new Uint32Array(17), r = new Uint32Array(17), h = new Uint32Array(17), c = new Uint32Array(17), g = new Uint32Array(17);
- for (j = 0; j < 17; j++)
- r[j] = h[j] = 0;
- for (j = 0; j < 16; j++)
- r[j] = k[j];
- r[3] &= 15;
- r[4] &= 252;
- r[7] &= 15;
- r[8] &= 252;
- r[11] &= 15;
- r[12] &= 252;
- r[15] &= 15;
- while (n > 0) {
- for (j = 0; j < 17; j++)
- c[j] = 0;
- for (j = 0; j < 16 && j < n; ++j)
- c[j] = m[mpos + j];
- c[j] = 1;
- mpos += j;
- n -= j;
- add1305(h, c);
- for (i = 0; i < 17; i++) {
- x[i] = 0;
- for (j = 0; j < 17; j++)
- x[i] = x[i] + h[j] * (j <= i ? r[i - j] : 320 * r[i + 17 - j] | 0) | 0 | 0;
- }
- for (i = 0; i < 17; i++)
- h[i] = x[i];
- u = 0;
- for (j = 0; j < 16; j++) {
- u = u + h[j] | 0;
- h[j] = u & 255;
- u >>>= 8;
- }
- u = u + h[16] | 0;
- h[16] = u & 3;
- u = 5 * (u >>> 2) | 0;
- for (j = 0; j < 16; j++) {
- u = u + h[j] | 0;
- h[j] = u & 255;
- u >>>= 8;
- }
- u = u + h[16] | 0;
- h[16] = u;
- }
- for (j = 0; j < 17; j++)
- g[j] = h[j];
- add1305(h, minusp);
- s = -(h[16] >>> 7) | 0;
- for (j = 0; j < 17; j++)
- h[j] ^= s & (g[j] ^ h[j]);
- for (j = 0; j < 16; j++)
- c[j] = k[j + 16];
- c[16] = 0;
- add1305(h, c);
- for (j = 0; j < 16; j++)
- out[outpos + j] = h[j];
- return 0;
- }
- function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
- var x = new Uint8Array(16);
- crypto_onetimeauth(x, 0, m, mpos, n, k);
- return crypto_verify_16(h, hpos, x, 0);
- }
- function crypto_secretbox(c, m, d, n, k) {
- var i;
- if (d < 32)
- return -1;
- crypto_stream_xor(c, 0, m, 0, d, n, k);
- crypto_onetimeauth(c, 16, c, 32, d - 32, c);
- for (i = 0; i < 16; i++)
- c[i] = 0;
- return 0;
- }
- function crypto_secretbox_open(m, c, d, n, k) {
- var i;
- var x = new Uint8Array(32);
- if (d < 32)
- return -1;
- crypto_stream(x, 0, 32, n, k);
- if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0)
- return -1;
- crypto_stream_xor(m, 0, c, 0, d, n, k);
- for (i = 0; i < 32; i++)
- m[i] = 0;
- return 0;
- }
- function set25519(r, a) {
- var i;
- for (i = 0; i < 16; i++)
- r[i] = a[i] | 0;
- }
- function car25519(o) {
- var c;
- var i;
- for (i = 0; i < 16; i++) {
- o[i] += 65536;
- c = Math.floor(o[i] / 65536);
- o[(i + 1) * (i < 15 ? 1 : 0)] += c - 1 + 37 * (c - 1) * (i === 15 ? 1 : 0);
- o[i] -= c * 65536;
- }
- }
- function sel25519(p, q, b) {
- var t, c = ~(b - 1);
- for (var i = 0; i < 16; i++) {
- t = c & (p[i] ^ q[i]);
- p[i] ^= t;
- q[i] ^= t;
- }
- }
- function pack25519(o, n) {
- var i, j, b;
- var m = gf(), t = gf();
- for (i = 0; i < 16; i++)
- t[i] = n[i];
- car25519(t);
- car25519(t);
- car25519(t);
- for (j = 0; j < 2; j++) {
- m[0] = t[0] - 65517;
- for (i = 1; i < 15; i++) {
- m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
- m[i - 1] &= 65535;
- }
- m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
- b = m[15] >> 16 & 1;
- m[14] &= 65535;
- sel25519(t, m, 1 - b);
- }
- for (i = 0; i < 16; i++) {
- o[2 * i] = t[i] & 255;
- o[2 * i + 1] = t[i] >> 8;
- }
- }
- function neq25519(a, b) {
- var c = new Uint8Array(32), d = new Uint8Array(32);
- pack25519(c, a);
- pack25519(d, b);
- return crypto_verify_32(c, 0, d, 0);
- }
- function par25519(a) {
- var d = new Uint8Array(32);
- pack25519(d, a);
- return d[0] & 1;
- }
- function unpack25519(o, n) {
- var i;
- for (i = 0; i < 16; i++)
- o[i] = n[2 * i] + (n[2 * i + 1] << 8);
- o[15] &= 32767;
- }
- function A(o, a, b) {
- var i;
- for (i = 0; i < 16; i++)
- o[i] = a[i] + b[i] | 0;
- }
- function Z(o, a, b) {
- var i;
- for (i = 0; i < 16; i++)
- o[i] = a[i] - b[i] | 0;
- }
- function M(o, a, b) {
- var i, j, t = new Float64Array(31);
- for (i = 0; i < 31; i++)
- t[i] = 0;
- for (i = 0; i < 16; i++) {
- for (j = 0; j < 16; j++) {
- t[i + j] += a[i] * b[j];
- }
- }
- for (i = 0; i < 15; i++) {
- t[i] += 38 * t[i + 16];
- }
- for (i = 0; i < 16; i++)
- o[i] = t[i];
- car25519(o);
- car25519(o);
- }
- function S(o, a) {
- M(o, a, a);
- }
- function inv25519(o, i) {
- var c = gf();
- var a;
- for (a = 0; a < 16; a++)
- c[a] = i[a];
- for (a = 253; a >= 0; a--) {
- S(c, c);
- if (a !== 2 && a !== 4)
- M(c, c, i);
- }
- for (a = 0; a < 16; a++)
- o[a] = c[a];
- }
- function pow2523(o, i) {
- var c = gf();
- var a;
- for (a = 0; a < 16; a++)
- c[a] = i[a];
- for (a = 250; a >= 0; a--) {
- S(c, c);
- if (a !== 1)
- M(c, c, i);
- }
- for (a = 0; a < 16; a++)
- o[a] = c[a];
- }
- function crypto_scalarmult(q, n, p) {
- var z = new Uint8Array(32);
- var x = new Float64Array(80), r, i;
- var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
- for (i = 0; i < 31; i++)
- z[i] = n[i];
- z[31] = n[31] & 127 | 64;
- z[0] &= 248;
- unpack25519(x, p);
- for (i = 0; i < 16; i++) {
- b[i] = x[i];
- d[i] = a[i] = c[i] = 0;
- }
- a[0] = d[0] = 1;
- for (i = 254; i >= 0; --i) {
- r = z[i >>> 3] >>> (i & 7) & 1;
- sel25519(a, b, r);
- sel25519(c, d, r);
- A(e, a, c);
- Z(a, a, c);
- A(c, b, d);
- Z(b, b, d);
- S(d, e);
- S(f, a);
- M(a, c, a);
- M(c, b, e);
- A(e, a, c);
- Z(a, a, c);
- S(b, a);
- Z(c, d, f);
- M(a, c, _121665);
- A(a, a, d);
- M(c, c, a);
- M(a, d, f);
- M(d, b, x);
- S(b, e);
- sel25519(a, b, r);
- sel25519(c, d, r);
- }
- for (i = 0; i < 16; i++) {
- x[i + 16] = a[i];
- x[i + 32] = c[i];
- x[i + 48] = b[i];
- x[i + 64] = d[i];
- }
- var x32 = x.subarray(32);
- var x16 = x.subarray(16);
- inv25519(x32, x32);
- M(x16, x16, x32);
- pack25519(q, x16);
- return 0;
- }
- function crypto_scalarmult_base(q, n) {
- return crypto_scalarmult(q, n, _9);
- }
- function crypto_box_keypair(y, x) {
- randombytes(x, 32);
- return crypto_scalarmult_base(y, x);
- }
- function crypto_box_beforenm(k, y, x) {
- var s = new Uint8Array(32);
- crypto_scalarmult(s, x, y);
- return crypto_core_hsalsa20(k, _0, s, sigma);
- }
- var crypto_box_afternm = crypto_secretbox;
- var crypto_box_open_afternm = crypto_secretbox_open;
- function crypto_box(c, m, d, n, y, x) {
- var k = new Uint8Array(32);
- crypto_box_beforenm(k, y, x);
- return crypto_box_afternm(c, m, d, n, k);
- }
- function crypto_box_open(m, c, d, n, y, x) {
- var k = new Uint8Array(32);
- crypto_box_beforenm(k, y, x);
- return crypto_box_open_afternm(m, c, d, n, k);
- }
- function add64() {
- var a = 0, b = 0, c = 0, d = 0, m16 = 65535, l, h, i;
- for (i = 0; i < arguments.length; i++) {
- l = arguments[i].lo;
- h = arguments[i].hi;
- a += l & m16;
- b += l >>> 16;
- c += h & m16;
- d += h >>> 16;
- }
- b += a >>> 16;
- c += b >>> 16;
- d += c >>> 16;
- return new u64(c & m16 | d << 16, a & m16 | b << 16);
- }
- function shr64(x, c) {
- return new u64(x.hi >>> c, x.lo >>> c | x.hi << 32 - c);
- }
- function xor64() {
- var l = 0, h = 0, i;
- for (i = 0; i < arguments.length; i++) {
- l ^= arguments[i].lo;
- h ^= arguments[i].hi;
- }
- return new u64(h, l);
- }
- function R(x, c) {
- var h, l, c1 = 32 - c;
- if (c < 32) {
- h = x.hi >>> c | x.lo << c1;
- l = x.lo >>> c | x.hi << c1;
- } else if (c < 64) {
- h = x.lo >>> c | x.hi << c1;
- l = x.hi >>> c | x.lo << c1;
- }
- return new u64(h, l);
- }
- function Ch(x, y, z) {
- var h = x.hi & y.hi ^ ~x.hi & z.hi, l = x.lo & y.lo ^ ~x.lo & z.lo;
- return new u64(h, l);
- }
- function Maj(x, y, z) {
- var h = x.hi & y.hi ^ x.hi & z.hi ^ y.hi & z.hi, l = x.lo & y.lo ^ x.lo & z.lo ^ y.lo & z.lo;
- return new u64(h, l);
- }
- function Sigma0(x) {
- return xor64(R(x, 28), R(x, 34), R(x, 39));
- }
- function Sigma1(x) {
- return xor64(R(x, 14), R(x, 18), R(x, 41));
- }
- function sigma0(x) {
- return xor64(R(x, 1), R(x, 8), shr64(x, 7));
- }
- function sigma1(x) {
- return xor64(R(x, 19), R(x, 61), shr64(x, 6));
- }
- var K = [
- new u64(1116352408, 3609767458),
- new u64(1899447441, 602891725),
- new u64(3049323471, 3964484399),
- new u64(3921009573, 2173295548),
- new u64(961987163, 4081628472),
- new u64(1508970993, 3053834265),
- new u64(2453635748, 2937671579),
- new u64(2870763221, 3664609560),
- new u64(3624381080, 2734883394),
- new u64(310598401, 1164996542),
- new u64(607225278, 1323610764),
- new u64(1426881987, 3590304994),
- new u64(1925078388, 4068182383),
- new u64(2162078206, 991336113),
- new u64(2614888103, 633803317),
- new u64(3248222580, 3479774868),
- new u64(3835390401, 2666613458),
- new u64(4022224774, 944711139),
- new u64(264347078, 2341262773),
- new u64(604807628, 2007800933),
- new u64(770255983, 1495990901),
- new u64(1249150122, 1856431235),
- new u64(1555081692, 3175218132),
- new u64(1996064986, 2198950837),
- new u64(2554220882, 3999719339),
- new u64(2821834349, 766784016),
- new u64(2952996808, 2566594879),
- new u64(3210313671, 3203337956),
- new u64(3336571891, 1034457026),
- new u64(3584528711, 2466948901),
- new u64(113926993, 3758326383),
- new u64(338241895, 168717936),
- new u64(666307205, 1188179964),
- new u64(773529912, 1546045734),
- new u64(1294757372, 1522805485),
- new u64(1396182291, 2643833823),
- new u64(1695183700, 2343527390),
- new u64(1986661051, 1014477480),
- new u64(2177026350, 1206759142),
- new u64(2456956037, 344077627),
- new u64(2730485921, 1290863460),
- new u64(2820302411, 3158454273),
- new u64(3259730800, 3505952657),
- new u64(3345764771, 106217008),
- new u64(3516065817, 3606008344),
- new u64(3600352804, 1432725776),
- new u64(4094571909, 1467031594),
- new u64(275423344, 851169720),
- new u64(430227734, 3100823752),
- new u64(506948616, 1363258195),
- new u64(659060556, 3750685593),
- new u64(883997877, 3785050280),
- new u64(958139571, 3318307427),
- new u64(1322822218, 3812723403),
- new u64(1537002063, 2003034995),
- new u64(1747873779, 3602036899),
- new u64(1955562222, 1575990012),
- new u64(2024104815, 1125592928),
- new u64(2227730452, 2716904306),
- new u64(2361852424, 442776044),
- new u64(2428436474, 593698344),
- new u64(2756734187, 3733110249),
- new u64(3204031479, 2999351573),
- new u64(3329325298, 3815920427),
- new u64(3391569614, 3928383900),
- new u64(3515267271, 566280711),
- new u64(3940187606, 3454069534),
- new u64(4118630271, 4000239992),
- new u64(116418474, 1914138554),
- new u64(174292421, 2731055270),
- new u64(289380356, 3203993006),
- new u64(460393269, 320620315),
- new u64(685471733, 587496836),
- new u64(852142971, 1086792851),
- new u64(1017036298, 365543100),
- new u64(1126000580, 2618297676),
- new u64(1288033470, 3409855158),
- new u64(1501505948, 4234509866),
- new u64(1607167915, 987167468),
- new u64(1816402316, 1246189591)
- ];
- function crypto_hashblocks(x, m, n) {
- var z = [], b = [], a = [], w = [], t, i, j;
- for (i = 0; i < 8; i++)
- z[i] = a[i] = dl64(x, 8 * i);
- var pos = 0;
- while (n >= 128) {
- for (i = 0; i < 16; i++)
- w[i] = dl64(m, 8 * i + pos);
- for (i = 0; i < 80; i++) {
- for (j = 0; j < 8; j++)
- b[j] = a[j];
- t = add64(a[7], Sigma1(a[4]), Ch(a[4], a[5], a[6]), K[i], w[i % 16]);
- b[7] = add64(t, Sigma0(a[0]), Maj(a[0], a[1], a[2]));
- b[3] = add64(b[3], t);
- for (j = 0; j < 8; j++)
- a[(j + 1) % 8] = b[j];
- if (i % 16 === 15) {
- for (j = 0; j < 16; j++) {
- w[j] = add64(w[j], w[(j + 9) % 16], sigma0(w[(j + 1) % 16]), sigma1(w[(j + 14) % 16]));
- }
- }
- }
- for (i = 0; i < 8; i++) {
- a[i] = add64(a[i], z[i]);
- z[i] = a[i];
- }
- pos += 128;
- n -= 128;
- }
- for (i = 0; i < 8; i++)
- ts64(x, 8 * i, z[i]);
- return n;
- }
- var iv = new Uint8Array([
- 106,
- 9,
- 230,
- 103,
- 243,
- 188,
- 201,
- 8,
- 187,
- 103,
- 174,
- 133,
- 132,
- 202,
- 167,
- 59,
- 60,
- 110,
- 243,
- 114,
- 254,
- 148,
- 248,
- 43,
- 165,
- 79,
- 245,
- 58,
- 95,
- 29,
- 54,
- 241,
- 81,
- 14,
- 82,
- 127,
- 173,
- 230,
- 130,
- 209,
- 155,
- 5,
- 104,
- 140,
- 43,
- 62,
- 108,
- 31,
- 31,
- 131,
- 217,
- 171,
- 251,
- 65,
- 189,
- 107,
- 91,
- 224,
- 205,
- 25,
- 19,
- 126,
- 33,
- 121
- ]);
- function crypto_hash(out, m, n) {
- var h = new Uint8Array(64), x = new Uint8Array(256);
- var i, b = n;
- for (i = 0; i < 64; i++)
- h[i] = iv[i];
- crypto_hashblocks(h, m, n);
- n %= 128;
- for (i = 0; i < 256; i++)
- x[i] = 0;
- for (i = 0; i < n; i++)
- x[i] = m[b - n + i];
- x[n] = 128;
- n = 256 - 128 * (n < 112 ? 1 : 0);
- x[n - 9] = 0;
- ts64(x, n - 8, new u64(b / 536870912 | 0, b << 3));
- crypto_hashblocks(h, x, n);
- for (i = 0; i < 64; i++)
- out[i] = h[i];
- return 0;
- }
- function add(p, q) {
- var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
- Z(a, p[1], p[0]);
- Z(t, q[1], q[0]);
- M(a, a, t);
- A(b, p[0], p[1]);
- A(t, q[0], q[1]);
- M(b, b, t);
- M(c, p[3], q[3]);
- M(c, c, D2);
- M(d, p[2], q[2]);
- A(d, d, d);
- Z(e, b, a);
- Z(f, d, c);
- A(g, d, c);
- A(h, b, a);
- M(p[0], e, f);
- M(p[1], h, g);
- M(p[2], g, f);
- M(p[3], e, h);
- }
- function cswap(p, q, b) {
- var i;
- for (i = 0; i < 4; i++) {
- sel25519(p[i], q[i], b);
- }
- }
- function pack(r, p) {
- var tx = gf(), ty = gf(), zi = gf();
- inv25519(zi, p[2]);
- M(tx, p[0], zi);
- M(ty, p[1], zi);
- pack25519(r, ty);
- r[31] ^= par25519(tx) << 7;
- }
- function scalarmult(p, q, s) {
- var b, i;
- set25519(p[0], gf0);
- set25519(p[1], gf1);
- set25519(p[2], gf1);
- set25519(p[3], gf0);
- for (i = 255; i >= 0; --i) {
- b = s[i / 8 | 0] >> (i & 7) & 1;
- cswap(p, q, b);
- add(q, p);
- add(p, p);
- cswap(p, q, b);
- }
- }
- function scalarbase(p, s) {
- var q = [
- gf(),
- gf(),
- gf(),
- gf()
- ];
- set25519(q[0], X);
- set25519(q[1], Y);
- set25519(q[2], gf1);
- M(q[3], X, Y);
- scalarmult(p, q, s);
- }
- function crypto_sign_keypair(pk, sk, seeded) {
- var d = new Uint8Array(64);
- var p = [
- gf(),
- gf(),
- gf(),
- gf()
- ];
- var i;
- if (!seeded)
- randombytes(sk, 32);
- crypto_hash(d, sk, 32);
- d[0] &= 248;
- d[31] &= 127;
- d[31] |= 64;
- scalarbase(p, d);
- pack(pk, p);
- for (i = 0; i < 32; i++)
- sk[i + 32] = pk[i];
- return 0;
- }
- var L = new Float64Array([
- 237,
- 211,
- 245,
- 92,
- 26,
- 99,
- 18,
- 88,
- 214,
- 156,
- 247,
- 162,
- 222,
- 249,
- 222,
- 20,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 16
- ]);
- function modL(r, x) {
- var carry, i, j, k;
- for (i = 63; i >= 32; --i) {
- carry = 0;
- for (j = i - 32, k = i - 12; j < k; ++j) {
- x[j] += carry - 16 * x[i] * L[j - (i - 32)];
- carry = Math.floor((x[j] + 128) / 256);
- x[j] -= carry * 256;
- }
- x[j] += carry;
- x[i] = 0;
- }
- carry = 0;
- for (j = 0; j < 32; j++) {
- x[j] += carry - (x[31] >> 4) * L[j];
- carry = x[j] >> 8;
- x[j] &= 255;
- }
- for (j = 0; j < 32; j++)
- x[j] -= carry * L[j];
- for (i = 0; i < 32; i++) {
- x[i + 1] += x[i] >> 8;
- r[i] = x[i] & 255;
- }
- }
- function reduce(r) {
- var x = new Float64Array(64), i;
- for (i = 0; i < 64; i++)
- x[i] = r[i];
- for (i = 0; i < 64; i++)
- r[i] = 0;
- modL(r, x);
- }
- function crypto_sign(sm, m, n, sk) {
- var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
- var i, j, x = new Float64Array(64);
- var p = [
- gf(),
- gf(),
- gf(),
- gf()
- ];
- crypto_hash(d, sk, 32);
- d[0] &= 248;
- d[31] &= 127;
- d[31] |= 64;
- var smlen = n + 64;
- for (i = 0; i < n; i++)
- sm[64 + i] = m[i];
- for (i = 0; i < 32; i++)
- sm[32 + i] = d[32 + i];
- crypto_hash(r, sm.subarray(32), n + 32);
- reduce(r);
- scalarbase(p, r);
- pack(sm, p);
- for (i = 32; i < 64; i++)
- sm[i] = sk[i];
- crypto_hash(h, sm, n + 64);
- reduce(h);
- for (i = 0; i < 64; i++)
- x[i] = 0;
- for (i = 0; i < 32; i++)
- x[i] = r[i];
- for (i = 0; i < 32; i++) {
- for (j = 0; j < 32; j++) {
- x[i + j] += h[i] * d[j];
- }
- }
- modL(sm.subarray(32), x);
- return smlen;
- }
- function unpackneg(r, p) {
- var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
- set25519(r[2], gf1);
- unpack25519(r[1], p);
- S(num, r[1]);
- M(den, num, D);
- Z(num, num, r[2]);
- A(den, r[2], den);
- S(den2, den);
- S(den4, den2);
- M(den6, den4, den2);
- M(t, den6, num);
- M(t, t, den);
- pow2523(t, t);
- M(t, t, num);
- M(t, t, den);
- M(t, t, den);
- M(r[0], t, den);
- S(chk, r[0]);
- M(chk, chk, den);
- if (neq25519(chk, num))
- M(r[0], r[0], I);
- S(chk, r[0]);
- M(chk, chk, den);
- if (neq25519(chk, num))
- return -1;
- if (par25519(r[0]) === p[31] >> 7)
- Z(r[0], gf0, r[0]);
- M(r[3], r[0], r[1]);
- return 0;
- }
- function crypto_sign_open(m, sm, n, pk) {
- var i;
- var t = new Uint8Array(32), h = new Uint8Array(64);
- var p = [
- gf(),
- gf(),
- gf(),
- gf()
- ], q = [
- gf(),
- gf(),
- gf(),
- gf()
- ];
- if (n < 64)
- return -1;
- if (unpackneg(q, pk))
- return -1;
- for (i = 0; i < n; i++)
- m[i] = sm[i];
- for (i = 0; i < 32; i++)
- m[i + 32] = pk[i];
- crypto_hash(h, m, n);
- reduce(h);
- scalarmult(p, q, h);
- scalarbase(q, sm.subarray(32));
- add(p, q);
- pack(t, p);
- n -= 64;
- if (crypto_verify_32(sm, 0, t, 0)) {
- for (i = 0; i < n; i++)
- m[i] = 0;
- return -1;
- }
- for (i = 0; i < n; i++)
- m[i] = sm[i + 64];
- return n;
- }
- var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, crypto_secretbox_BOXZEROBYTES = 16, crypto_scalarmult_BYTES = 32, crypto_scalarmult_SCALARBYTES = 32, crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, crypto_box_BEFORENMBYTES = 32, crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES, crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES, crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES, crypto_sign_BYTES = 64, crypto_sign_PUBLICKEYBYTES = 32, crypto_sign_SECRETKEYBYTES = 64, crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64;
- nacl2.lowlevel = {
- crypto_core_hsalsa20,
- crypto_stream_xor,
- crypto_stream,
- crypto_stream_salsa20_xor,
- crypto_stream_salsa20,
- crypto_onetimeauth,
- crypto_onetimeauth_verify,
- crypto_verify_16,
- crypto_verify_32,
- crypto_secretbox,
- crypto_secretbox_open,
- crypto_scalarmult,
- crypto_scalarmult_base,
- crypto_box_beforenm,
- crypto_box_afternm,
- crypto_box,
- crypto_box_open,
- crypto_box_keypair,
- crypto_hash,
- crypto_sign,
- crypto_sign_keypair,
- crypto_sign_open,
- crypto_secretbox_KEYBYTES,
- crypto_secretbox_NONCEBYTES,
- crypto_secretbox_ZEROBYTES,
- crypto_secretbox_BOXZEROBYTES,
- crypto_scalarmult_BYTES,
- crypto_scalarmult_SCALARBYTES,
- crypto_box_PUBLICKEYBYTES,
- crypto_box_SECRETKEYBYTES,
- crypto_box_BEFORENMBYTES,
- crypto_box_NONCEBYTES,
- crypto_box_ZEROBYTES,
- crypto_box_BOXZEROBYTES,
- crypto_sign_BYTES,
- crypto_sign_PUBLICKEYBYTES,
- crypto_sign_SECRETKEYBYTES,
- crypto_sign_SEEDBYTES,
- crypto_hash_BYTES,
- gf,
- D,
- L,
- pack25519,
- unpack25519,
- M,
- A,
- S,
- Z,
- pow2523,
- add,
- set25519,
- modL,
- scalarmult,
- scalarbase
- };
- function checkLengths(k, n) {
- if (k.length !== crypto_secretbox_KEYBYTES)
- throw new Error("bad key size");
- if (n.length !== crypto_secretbox_NONCEBYTES)
- throw new Error("bad nonce size");
- }
- function checkBoxLengths(pk, sk) {
- if (pk.length !== crypto_box_PUBLICKEYBYTES)
- throw new Error("bad public key size");
- if (sk.length !== crypto_box_SECRETKEYBYTES)
- throw new Error("bad secret key size");
- }
- function checkArrayTypes() {
- for (var i = 0; i < arguments.length; i++) {
- if (!(arguments[i] instanceof Uint8Array))
- throw new TypeError("unexpected type, use Uint8Array");
- }
- }
- function cleanup(arr) {
- for (var i = 0; i < arr.length; i++)
- arr[i] = 0;
- }
- nacl2.randomBytes = function(n) {
- var b = new Uint8Array(n);
- randombytes(b, n);
- return b;
- };
- nacl2.secretbox = function(msg, nonce, key) {
- checkArrayTypes(msg, nonce, key);
- checkLengths(key, nonce);
- var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
- var c = new Uint8Array(m.length);
- for (var i = 0; i < msg.length; i++)
- m[i + crypto_secretbox_ZEROBYTES] = msg[i];
- crypto_secretbox(c, m, m.length, nonce, key);
- return c.subarray(crypto_secretbox_BOXZEROBYTES);
- };
- nacl2.secretbox.open = function(box, nonce, key) {
- checkArrayTypes(box, nonce, key);
- checkLengths(key, nonce);
- var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
- var m = new Uint8Array(c.length);
- for (var i = 0; i < box.length; i++)
- c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
- if (c.length < 32)
- return null;
- if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0)
- return null;
- return m.subarray(crypto_secretbox_ZEROBYTES);
- };
- nacl2.secretbox.keyLength = crypto_secretbox_KEYBYTES;
- nacl2.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
- nacl2.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
- nacl2.scalarMult = function(n, p) {
- checkArrayTypes(n, p);
- if (n.length !== crypto_scalarmult_SCALARBYTES)
- throw new Error("bad n size");
- if (p.length !== crypto_scalarmult_BYTES)
- throw new Error("bad p size");
- var q = new Uint8Array(crypto_scalarmult_BYTES);
- crypto_scalarmult(q, n, p);
- return q;
- };
- nacl2.scalarMult.base = function(n) {
- checkArrayTypes(n);
- if (n.length !== crypto_scalarmult_SCALARBYTES)
- throw new Error("bad n size");
- var q = new Uint8Array(crypto_scalarmult_BYTES);
- crypto_scalarmult_base(q, n);
- return q;
- };
- nacl2.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
- nacl2.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
- nacl2.box = function(msg, nonce, publicKey, secretKey) {
- var k = nacl2.box.before(publicKey, secretKey);
- return nacl2.secretbox(msg, nonce, k);
- };
- nacl2.box.before = function(publicKey, secretKey) {
- checkArrayTypes(publicKey, secretKey);
- checkBoxLengths(publicKey, secretKey);
- var k = new Uint8Array(crypto_box_BEFORENMBYTES);
- crypto_box_beforenm(k, publicKey, secretKey);
- return k;
- };
- nacl2.box.after = nacl2.secretbox;
- nacl2.box.open = function(msg, nonce, publicKey, secretKey) {
- var k = nacl2.box.before(publicKey, secretKey);
- return nacl2.secretbox.open(msg, nonce, k);
- };
- nacl2.box.open.after = nacl2.secretbox.open;
- nacl2.box.keyPair = function() {
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
- var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
- crypto_box_keypair(pk, sk);
- return {
- publicKey: pk,
- secretKey: sk
- };
- };
- nacl2.box.keyPair.fromSecretKey = function(secretKey) {
- checkArrayTypes(secretKey);
- if (secretKey.length !== crypto_box_SECRETKEYBYTES)
- throw new Error("bad secret key size");
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
- crypto_scalarmult_base(pk, secretKey);
- return {
- publicKey: pk,
- secretKey: new Uint8Array(secretKey)
- };
- };
- nacl2.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
- nacl2.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
- nacl2.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
- nacl2.box.nonceLength = crypto_box_NONCEBYTES;
- nacl2.box.overheadLength = nacl2.secretbox.overheadLength;
- nacl2.sign = function(msg, secretKey) {
- checkArrayTypes(msg, secretKey);
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
- throw new Error("bad secret key size");
- var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
- crypto_sign(signedMsg, msg, msg.length, secretKey);
- return signedMsg;
- };
- nacl2.sign.open = function(signedMsg, publicKey) {
- checkArrayTypes(signedMsg, publicKey);
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
- throw new Error("bad public key size");
- var tmp = new Uint8Array(signedMsg.length);
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
- if (mlen < 0)
- return null;
- var m = new Uint8Array(mlen);
- for (var i = 0; i < m.length; i++)
- m[i] = tmp[i];
- return m;
- };
- nacl2.sign.detached = function(msg, secretKey) {
- var signedMsg = nacl2.sign(msg, secretKey);
- var sig = new Uint8Array(crypto_sign_BYTES);
- for (var i = 0; i < sig.length; i++)
- sig[i] = signedMsg[i];
- return sig;
- };
- nacl2.sign.detached.verify = function(msg, sig, publicKey) {
- checkArrayTypes(msg, sig, publicKey);
- if (sig.length !== crypto_sign_BYTES)
- throw new Error("bad signature size");
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
- throw new Error("bad public key size");
- var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
- var m = new Uint8Array(crypto_sign_BYTES + msg.length);
- var i;
- for (i = 0; i < crypto_sign_BYTES; i++)
- sm[i] = sig[i];
- for (i = 0; i < msg.length; i++)
- sm[i + crypto_sign_BYTES] = msg[i];
- return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
- };
- nacl2.sign.keyPair = function() {
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
- crypto_sign_keypair(pk, sk);
- return {
- publicKey: pk,
- secretKey: sk
- };
- };
- nacl2.sign.keyPair.fromSecretKey = function(secretKey) {
- checkArrayTypes(secretKey);
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
- throw new Error("bad secret key size");
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
- for (var i = 0; i < pk.length; i++)
- pk[i] = secretKey[32 + i];
- return {
- publicKey: pk,
- secretKey: new Uint8Array(secretKey)
- };
- };
- nacl2.sign.keyPair.fromSeed = function(seed) {
- checkArrayTypes(seed);
- if (seed.length !== crypto_sign_SEEDBYTES)
- throw new Error("bad seed size");
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
- for (var i = 0; i < 32; i++)
- sk[i] = seed[i];
- crypto_sign_keypair(pk, sk, true);
- return {
- publicKey: pk,
- secretKey: sk
- };
- };
- nacl2.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
- nacl2.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
- nacl2.sign.seedLength = crypto_sign_SEEDBYTES;
- nacl2.sign.signatureLength = crypto_sign_BYTES;
- nacl2.hash = function(msg) {
- checkArrayTypes(msg);
- var h = new Uint8Array(crypto_hash_BYTES);
- crypto_hash(h, msg, msg.length);
- return h;
- };
- nacl2.hash.hashLength = crypto_hash_BYTES;
- nacl2.verify = function(x, y) {
- checkArrayTypes(x, y);
- if (x.length === 0 || y.length === 0)
- return false;
- if (x.length !== y.length)
- return false;
- return vn(x, 0, y, 0, x.length) === 0 ? true : false;
- };
- nacl2.setPRNG = function(fn) {
- randombytes = fn;
- };
- (function() {
- var crypto1 = typeof globalThis !== "undefined" ? globalThis.crypto || globalThis.msCrypto : null;
- if (crypto1 && crypto1.getRandomValues) {
- var QUOTA = 65536;
- nacl2.setPRNG(function(x, n) {
- var i, v = new Uint8Array(n);
- for (i = 0; i < n; i += QUOTA) {
- crypto1.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
- }
- for (i = 0; i < n; i++)
- x[i] = v[i];
- cleanup(v);
- });
- } else if (typeof __require !== "undefined") {
- crypto1 = require_crypto();
- if (crypto1 && crypto1.randomBytes) {
- nacl2.setPRNG(function(x, n) {
- var i, v = crypto1.randomBytes(n);
- for (i = 0; i < n; i++)
- x[i] = v[i];
- cleanup(v);
- });
- }
- }
- })();
- })(typeof module !== "undefined" && module.exports ? module.exports : globalThis.nacl = globalThis.nacl || {});
- var nacl = typeof module !== "undefined" && module.exports ? module.exports : globalThis.nacl;
- var denoHelper = {
- fromSeed: nacl.sign.keyPair.fromSeed,
- sign: nacl.sign.detached,
- verify: nacl.sign.detached.verify,
- randomBytes: nacl.randomBytes
- };
- var helper;
- function setEd25519Helper(lib) {
- helper = lib;
- }
- function getEd25519Helper() {
- return helper;
- }
- var crc16tab = new Uint16Array([
- 0,
- 4129,
- 8258,
- 12387,
- 16516,
- 20645,
- 24774,
- 28903,
- 33032,
- 37161,
- 41290,
- 45419,
- 49548,
- 53677,
- 57806,
- 61935,
- 4657,
- 528,
- 12915,
- 8786,
- 21173,
- 17044,
- 29431,
- 25302,
- 37689,
- 33560,
- 45947,
- 41818,
- 54205,
- 50076,
- 62463,
- 58334,
- 9314,
- 13379,
- 1056,
- 5121,
- 25830,
- 29895,
- 17572,
- 21637,
- 42346,
- 46411,
- 34088,
- 38153,
- 58862,
- 62927,
- 50604,
- 54669,
- 13907,
- 9842,
- 5649,
- 1584,
- 30423,
- 26358,
- 22165,
- 18100,
- 46939,
- 42874,
- 38681,
- 34616,
- 63455,
- 59390,
- 55197,
- 51132,
- 18628,
- 22757,
- 26758,
- 30887,
- 2112,
- 6241,
- 10242,
- 14371,
- 51660,
- 55789,
- 59790,
- 63919,
- 35144,
- 39273,
- 43274,
- 47403,
- 23285,
- 19156,
- 31415,
- 27286,
- 6769,
- 2640,
- 14899,
- 10770,
- 56317,
- 52188,
- 64447,
- 60318,
- 39801,
- 35672,
- 47931,
- 43802,
- 27814,
- 31879,
- 19684,
- 23749,
- 11298,
- 15363,
- 3168,
- 7233,
- 60846,
- 64911,
- 52716,
- 56781,
- 44330,
- 48395,
- 36200,
- 40265,
- 32407,
- 28342,
- 24277,
- 20212,
- 15891,
- 11826,
- 7761,
- 3696,
- 65439,
- 61374,
- 57309,
- 53244,
- 48923,
- 44858,
- 40793,
- 36728,
- 37256,
- 33193,
- 45514,
- 41451,
- 53516,
- 49453,
- 61774,
- 57711,
- 4224,
- 161,
- 12482,
- 8419,
- 20484,
- 16421,
- 28742,
- 24679,
- 33721,
- 37784,
- 41979,
- 46042,
- 49981,
- 54044,
- 58239,
- 62302,
- 689,
- 4752,
- 8947,
- 13010,
- 16949,
- 21012,
- 25207,
- 29270,
- 46570,
- 42443,
- 38312,
- 34185,
- 62830,
- 58703,
- 54572,
- 50445,
- 13538,
- 9411,
- 5280,
- 1153,
- 29798,
- 25671,
- 21540,
- 17413,
- 42971,
- 47098,
- 34713,
- 38840,
- 59231,
- 63358,
- 50973,
- 55100,
- 9939,
- 14066,
- 1681,
- 5808,
- 26199,
- 30326,
- 17941,
- 22068,
- 55628,
- 51565,
- 63758,
- 59695,
- 39368,
- 35305,
- 47498,
- 43435,
- 22596,
- 18533,
- 30726,
- 26663,
- 6336,
- 2273,
- 14466,
- 10403,
- 52093,
- 56156,
- 60223,
- 64286,
- 35833,
- 39896,
- 43963,
- 48026,
- 19061,
- 23124,
- 27191,
- 31254,
- 2801,
- 6864,
- 10931,
- 14994,
- 64814,
- 60687,
- 56684,
- 52557,
- 48554,
- 44427,
- 40424,
- 36297,
- 31782,
- 27655,
- 23652,
- 19525,
- 15522,
- 11395,
- 7392,
- 3265,
- 61215,
- 65342,
- 53085,
- 57212,
- 44955,
- 49082,
- 36825,
- 40952,
- 28183,
- 32310,
- 20053,
- 24180,
- 11923,
- 16050,
- 3793,
- 7920
- ]);
- var crc16 = class {
- static checksum(data) {
- let crc = 0;
- for (let i = 0; i < data.byteLength; i++) {
- let b = data[i];
- crc = crc << 8 & 65535 ^ crc16tab[(crc >> 8 ^ b) & 255];
- }
- return crc;
- }
- static validate(data, expected) {
- let ba = crc16.checksum(data);
- return ba == expected;
- }
- };
- var b32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
- var base32 = class {
- static encode(src) {
- let bits = 0;
- let value = 0;
- let a = new Uint8Array(src);
- let buf = new Uint8Array(src.byteLength * 2);
- let j = 0;
- for (let i = 0; i < a.byteLength; i++) {
- value = value << 8 | a[i];
- bits += 8;
- while (bits >= 5) {
- let index = value >>> bits - 5 & 31;
- buf[j++] = b32Alphabet.charAt(index).charCodeAt(0);
- bits -= 5;
- }
- }
- if (bits > 0) {
- let index = value << 5 - bits & 31;
- buf[j++] = b32Alphabet.charAt(index).charCodeAt(0);
- }
- return buf.slice(0, j);
- }
- static decode(src) {
- let bits = 0;
- let __byte = 0;
- let j = 0;
- let a = new Uint8Array(src);
- let out = new Uint8Array(a.byteLength * 5 / 8 | 0);
- for (let i = 0; i < a.byteLength; i++) {
- let v = String.fromCharCode(a[i]);
- let vv = b32Alphabet.indexOf(v);
- if (vv === -1) {
- throw new Error("Illegal Base32 character: " + a[i]);
- }
- __byte = __byte << 5 | vv;
- bits += 5;
- if (bits >= 8) {
- out[j++] = __byte >>> bits - 8 & 255;
- bits -= 8;
- }
- }
- return out.slice(0, j);
- }
- };
- var NKeysError = class extends Error {
- constructor(code2, chainedError) {
- super(code2);
- __publicField(this, "name");
- __publicField(this, "code");
- __publicField(this, "chainedError");
- this.name = "NKeysError";
- this.code = code2;
- this.chainedError = chainedError;
- }
- };
- function createOperator() {
- return createPair(Prefix.Operator);
- }
- function createAccount() {
- return createPair(Prefix.Account);
- }
- function createUser() {
- return createPair(Prefix.User);
- }
- var NKeysErrorCode;
- (function(NKeysErrorCode2) {
- NKeysErrorCode2["InvalidPrefixByte"] = "nkeys: invalid prefix byte";
- NKeysErrorCode2["InvalidKey"] = "nkeys: invalid key";
- NKeysErrorCode2["InvalidPublicKey"] = "nkeys: invalid public key";
- NKeysErrorCode2["InvalidSeedLen"] = "nkeys: invalid seed length";
- NKeysErrorCode2["InvalidSeed"] = "nkeys: invalid seed";
- NKeysErrorCode2["InvalidEncoding"] = "nkeys: invalid encoded key";
- NKeysErrorCode2["InvalidSignature"] = "nkeys: signature verification failed";
- NKeysErrorCode2["CannotSign"] = "nkeys: cannot sign, no private key available";
- NKeysErrorCode2["PublicKeyOnly"] = "nkeys: no seed or private key available";
- NKeysErrorCode2["InvalidChecksum"] = "nkeys: invalid checksum";
- NKeysErrorCode2["SerializationError"] = "nkeys: serialization error";
- NKeysErrorCode2["ApiError"] = "nkeys: api error";
- NKeysErrorCode2["ClearedPair"] = "nkeys: pair is cleared";
- })(NKeysErrorCode || (NKeysErrorCode = {}));
- var Prefix;
- (function(Prefix2) {
- Prefix2[Prefix2["Seed"] = 144] = "Seed";
- Prefix2[Prefix2["Private"] = 120] = "Private";
- Prefix2[Prefix2["Operator"] = 112] = "Operator";
- Prefix2[Prefix2["Server"] = 104] = "Server";
- Prefix2[Prefix2["Cluster"] = 16] = "Cluster";
- Prefix2[Prefix2["Account"] = 0] = "Account";
- Prefix2[Prefix2["User"] = 160] = "User";
- })(Prefix || (Prefix = {}));
- var Prefixes = class {
- static isValidPublicPrefix(prefix) {
- return prefix == Prefix.Server || prefix == Prefix.Operator || prefix == Prefix.Cluster || prefix == Prefix.Account || prefix == Prefix.User;
- }
- static startsWithValidPrefix(s) {
- let c = s[0];
- return c == "S" || c == "P" || c == "O" || c == "N" || c == "C" || c == "A" || c == "U";
- }
- static isValidPrefix(prefix) {
- let v = this.parsePrefix(prefix);
- return v != -1;
- }
- static parsePrefix(v) {
- switch (v) {
- case Prefix.Seed:
- return Prefix.Seed;
- case Prefix.Private:
- return Prefix.Private;
- case Prefix.Operator:
- return Prefix.Operator;
- case Prefix.Server:
- return Prefix.Server;
- case Prefix.Cluster:
- return Prefix.Cluster;
- case Prefix.Account:
- return Prefix.Account;
- case Prefix.User:
- return Prefix.User;
- default:
- return -1;
- }
- }
- };
- var Codec = class {
- static encode(prefix, src) {
- if (!src || !(src instanceof Uint8Array)) {
- throw new NKeysError(NKeysErrorCode.SerializationError);
- }
- if (!Prefixes.isValidPrefix(prefix)) {
- throw new NKeysError(NKeysErrorCode.InvalidPrefixByte);
- }
- return Codec._encode(false, prefix, src);
- }
- static encodeSeed(role, src) {
- if (!src) {
- throw new NKeysError(NKeysErrorCode.ApiError);
- }
- if (!Prefixes.isValidPublicPrefix(role)) {
- throw new NKeysError(NKeysErrorCode.InvalidPrefixByte);
- }
- if (src.byteLength !== 32) {
- throw new NKeysError(NKeysErrorCode.InvalidSeedLen);
- }
- return Codec._encode(true, role, src);
- }
- static decode(expected, src) {
- if (!Prefixes.isValidPrefix(expected)) {
- throw new NKeysError(NKeysErrorCode.InvalidPrefixByte);
- }
- const raw = Codec._decode(src);
- if (raw[0] !== expected) {
- throw new NKeysError(NKeysErrorCode.InvalidPrefixByte);
- }
- return raw.slice(1);
- }
- static decodeSeed(src) {
- const raw = Codec._decode(src);
- const prefix = Codec._decodePrefix(raw);
- if (prefix[0] != Prefix.Seed) {
- throw new NKeysError(NKeysErrorCode.InvalidSeed);
- }
- if (!Prefixes.isValidPublicPrefix(prefix[1])) {
- throw new NKeysError(NKeysErrorCode.InvalidPrefixByte);
- }
- return {
- buf: raw.slice(2),
- prefix: prefix[1]
- };
- }
- static _encode(seed, role, payload) {
- const payloadOffset = seed ? 2 : 1;
- const payloadLen = payload.byteLength;
- const cap = payloadOffset + payloadLen + 2;
- const checkOffset = payloadOffset + payloadLen;
- const raw = new Uint8Array(cap);
- if (seed) {
- const encodedPrefix = Codec._encodePrefix(Prefix.Seed, role);
- raw.set(encodedPrefix);
- } else {
- raw[0] = role;
- }
- raw.set(payload, payloadOffset);
- const checksum = crc16.checksum(raw.slice(0, checkOffset));
- const dv = new DataView(raw.buffer);
- dv.setUint16(checkOffset, checksum, true);
- return base32.encode(raw);
- }
- static _decode(src) {
- if (src.byteLength < 4) {
- throw new NKeysError(NKeysErrorCode.InvalidEncoding);
- }
- let raw;
- try {
- raw = base32.decode(src);
- } catch (ex) {
- throw new NKeysError(NKeysErrorCode.InvalidEncoding, ex);
- }
- const checkOffset = raw.byteLength - 2;
- const dv = new DataView(raw.buffer);
- const checksum = dv.getUint16(checkOffset, true);
- const payload = raw.slice(0, checkOffset);
- if (!crc16.validate(payload, checksum)) {
- throw new NKeysError(NKeysErrorCode.InvalidChecksum);
- }
- return payload;
- }
- static _encodePrefix(kind, role) {
- const b1 = kind | role >> 5;
- const b2 = (role & 31) << 3;
- return new Uint8Array([
- b1,
- b2
- ]);
- }
- static _decodePrefix(raw) {
- const b1 = raw[0] & 248;
- const b2 = (raw[0] & 7) << 5 | (raw[1] & 248) >> 3;
- return new Uint8Array([
- b1,
- b2
- ]);
- }
- };
- var KP = class {
- constructor(seed) {
- __publicField(this, "seed");
- this.seed = seed;
- }
- getRawSeed() {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- let sd = Codec.decodeSeed(this.seed);
- return sd.buf;
- }
- getSeed() {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- return this.seed;
- }
- getPublicKey() {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- const sd = Codec.decodeSeed(this.seed);
- const kp = getEd25519Helper().fromSeed(this.getRawSeed());
- const buf = Codec.encode(sd.prefix, kp.publicKey);
- return new TextDecoder().decode(buf);
- }
- getPrivateKey() {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- const kp = getEd25519Helper().fromSeed(this.getRawSeed());
- return Codec.encode(Prefix.Private, kp.secretKey);
- }
- sign(input) {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- const kp = getEd25519Helper().fromSeed(this.getRawSeed());
- return getEd25519Helper().sign(input, kp.secretKey);
- }
- verify(input, sig) {
- if (!this.seed) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- const kp = getEd25519Helper().fromSeed(this.getRawSeed());
- return getEd25519Helper().verify(input, sig, kp.publicKey);
- }
- clear() {
- if (!this.seed) {
- return;
- }
- this.seed.fill(0);
- this.seed = void 0;
- }
- };
- function createPair(prefix) {
- const rawSeed = getEd25519Helper().randomBytes(32);
- let str = Codec.encodeSeed(prefix, new Uint8Array(rawSeed));
- return new KP(str);
- }
- var PublicKey = class {
- constructor(publicKey) {
- __publicField(this, "publicKey");
- this.publicKey = publicKey;
- }
- getPublicKey() {
- if (!this.publicKey) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- return new TextDecoder().decode(this.publicKey);
- }
- getPrivateKey() {
- if (!this.publicKey) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- throw new NKeysError(NKeysErrorCode.PublicKeyOnly);
- }
- getSeed() {
- if (!this.publicKey) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- throw new NKeysError(NKeysErrorCode.PublicKeyOnly);
- }
- sign(_) {
- if (!this.publicKey) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- throw new NKeysError(NKeysErrorCode.CannotSign);
- }
- verify(input, sig) {
- if (!this.publicKey) {
- throw new NKeysError(NKeysErrorCode.ClearedPair);
- }
- let buf = Codec._decode(this.publicKey);
- return getEd25519Helper().verify(input, sig, buf.slice(1));
- }
- clear() {
- if (!this.publicKey) {
- return;
- }
- this.publicKey.fill(0);
- this.publicKey = void 0;
- }
- };
- function fromPublic(src) {
- const ba = new TextEncoder().encode(src);
- const raw = Codec._decode(ba);
- const prefix = Prefixes.parsePrefix(raw[0]);
- if (Prefixes.isValidPublicPrefix(prefix)) {
- return new PublicKey(ba);
- }
- throw new NKeysError(NKeysErrorCode.InvalidPublicKey);
- }
- function fromSeed(src) {
- Codec.decodeSeed(src);
- return new KP(src);
- }
- function encode1(bytes) {
- return btoa(String.fromCharCode(...bytes));
- }
- function decode1(b64str) {
- const bin = atob(b64str);
- const bytes = new Uint8Array(bin.length);
- for (let i = 0; i < bin.length; i++) {
- bytes[i] = bin.charCodeAt(i);
- }
- return bytes;
- }
- setEd25519Helper(denoHelper);
- var mod = {
- createAccount,
- createOperator,
- createPair,
- createUser,
- fromPublic,
- fromSeed,
- NKeysError,
- NKeysErrorCode,
- Prefix,
- decode: decode1,
- encode: encode1
- };
- function multiAuthenticator(authenticators) {
- return (nonce) => {
- let auth = {};
- authenticators.forEach((a) => {
- const args = a(nonce) || {};
- auth = Object.assign(auth, args);
- });
- return auth;
- };
- }
- function noAuthFn() {
- return () => {
- return;
- };
- }
- function usernamePasswordAuthenticator(user, pass) {
- return () => {
- const u = typeof user === "function" ? user() : user;
- const p = typeof pass === "function" ? pass() : pass;
- return {
- user: u,
- pass: p
- };
- };
- }
- function tokenAuthenticator(token) {
- return () => {
- const auth_token = typeof token === "function" ? token() : token;
- return {
- auth_token
- };
- };
- }
- function nkeyAuthenticator(seed) {
- return (nonce) => {
- const s = typeof seed === "function" ? seed() : seed;
- const kp = s ? mod.fromSeed(s) : void 0;
- const nkey = kp ? kp.getPublicKey() : "";
- const challenge = TE.encode(nonce || "");
- const sigBytes = kp !== void 0 && nonce ? kp.sign(challenge) : void 0;
- const sig = sigBytes ? mod.encode(sigBytes) : "";
- return {
- nkey,
- sig
- };
- };
- }
- function jwtAuthenticator(ajwt, seed) {
- return (nonce) => {
- const jwt = typeof ajwt === "function" ? ajwt() : ajwt;
- const fn = nkeyAuthenticator(seed);
- const { nkey, sig } = fn(nonce);
- return {
- jwt,
- nkey,
- sig
- };
- };
- }
- function credsAuthenticator(creds) {
- const fn = typeof creds !== "function" ? () => creds : creds;
- const parse = () => {
- const CREDS = /\s*(?:(?:[-]{3,}[^\n]*[-]{3,}\n)(.+)(?:\n\s*[-]{3,}[^\n]*[-]{3,}\n))/ig;
- const s = TD.decode(fn());
- let m = CREDS.exec(s);
- if (!m) {
- throw NatsError.errorForCode(ErrorCode.BadCreds);
- }
- const jwt = m[1].trim();
- m = CREDS.exec(s);
- if (!m) {
- throw NatsError.errorForCode(ErrorCode.BadCreds);
- }
- if (!m) {
- throw NatsError.errorForCode(ErrorCode.BadCreds);
- }
- const seed = TE.encode(m[1].trim());
- return {
- jwt,
- seed
- };
- };
- const jwtFn = () => {
- const { jwt } = parse();
- return jwt;
- };
- const nkeyFn = () => {
- const { seed } = parse();
- return seed;
- };
- return jwtAuthenticator(jwtFn, nkeyFn);
- }
- var DEFAULT_PING_INTERVAL = 2 * 60 * 1e3;
- var DEFAULT_MAX_PING_OUT = 2;
- var DEFAULT_RECONNECT_TIME_WAIT = 2 * 1e3;
- function defaultOptions() {
- return {
- maxPingOut: 2,
- maxReconnectAttempts: 10,
- noRandomize: false,
- pedantic: false,
- pingInterval: DEFAULT_PING_INTERVAL,
- reconnect: true,
- reconnectJitter: 100,
- reconnectJitterTLS: 1e3,
- reconnectTimeWait: DEFAULT_RECONNECT_TIME_WAIT,
- tls: void 0,
- verbose: false,
- waitOnFirstConnect: false,
- ignoreAuthErrorAbort: false
- };
- }
- function buildAuthenticator(opts) {
- const buf = [];
- if (typeof opts.authenticator === "function") {
- buf.push(opts.authenticator);
- }
- if (Array.isArray(opts.authenticator)) {
- buf.push(...opts.authenticator);
- }
- if (opts.token) {
- buf.push(tokenAuthenticator(opts.token));
- }
- if (opts.user) {
- buf.push(usernamePasswordAuthenticator(opts.user, opts.pass));
- }
- return buf.length === 0 ? noAuthFn() : multiAuthenticator(buf);
- }
- function parseOptions(opts) {
- const dhp = `${DEFAULT_HOST}:${defaultPort()}`;
- opts = opts || {
- servers: [
- dhp
- ]
- };
- opts.servers = opts.servers || [];
- if (typeof opts.servers === "string") {
- opts.servers = [
- opts.servers
- ];
- }
- if (opts.servers.length > 0 && opts.port) {
- throw new NatsError("port and servers options are mutually exclusive", ErrorCode.InvalidOption);
- }
- if (opts.servers.length === 0 && opts.port) {
- opts.servers = [
- `${DEFAULT_HOST}:${opts.port}`
- ];
- }
- if (opts.servers && opts.servers.length === 0) {
- opts.servers = [
- dhp
- ];
- }
- const options = extend(defaultOptions(), opts);
- options.authenticator = buildAuthenticator(options);
- [
- "reconnectDelayHandler",
- "authenticator"
- ].forEach((n) => {
- if (options[n] && typeof options[n] !== "function") {
- throw new NatsError(`${n} option should be a function`, ErrorCode.NotFunction);
- }
- });
- if (!options.reconnectDelayHandler) {
- options.reconnectDelayHandler = () => {
- let extra = options.tls ? options.reconnectJitterTLS : options.reconnectJitter;
- if (extra) {
- extra++;
- extra = Math.floor(Math.random() * extra);
- }
- return options.reconnectTimeWait + extra;
- };
- }
- if (options.inboxPrefix) {
- try {
- createInbox(options.inboxPrefix);
- } catch (err) {
- throw new NatsError(err.message, ErrorCode.ApiError);
- }
- }
- if (options.resolve) {
- if (typeof getResolveFn() !== "function") {
- throw new NatsError(`'resolve' is not supported on this client`, ErrorCode.InvalidOption);
- }
- }
- return options;
- }
- function checkOptions(info, options) {
- const { proto, tls_required: tlsRequired, tls_available: tlsAvailable } = info;
- if ((proto === void 0 || proto < 1) && options.noEcho) {
- throw new NatsError("noEcho", ErrorCode.ServerOptionNotAvailable);
- }
- const tls = tlsRequired || tlsAvailable || false;
- if (options.tls && !tls) {
- throw new NatsError("tls", ErrorCode.ServerOptionNotAvailable);
- }
- }
- var FLUSH_THRESHOLD = 1024 * 32;
- var INFO = /^INFO\s+([^\r\n]+)\r\n/i;
- var PONG_CMD = encode("PONG\r\n");
- var PING_CMD = encode("PING\r\n");
- var Connect = class {
- constructor(transport, opts, nonce) {
- __publicField(this, "echo");
- __publicField(this, "no_responders");
- __publicField(this, "protocol");
- __publicField(this, "verbose");
- __publicField(this, "pedantic");
- __publicField(this, "jwt");
- __publicField(this, "nkey");
- __publicField(this, "sig");
- __publicField(this, "user");
- __publicField(this, "pass");
- __publicField(this, "auth_token");
- __publicField(this, "tls_required");
- __publicField(this, "name");
- __publicField(this, "lang");
- __publicField(this, "version");
- __publicField(this, "headers");
- this.protocol = 1;
- this.version = transport.version;
- this.lang = transport.lang;
- this.echo = opts.noEcho ? false : void 0;
- this.verbose = opts.verbose;
- this.pedantic = opts.pedantic;
- this.tls_required = opts.tls ? true : void 0;
- this.name = opts.name;
- const creds = (opts && typeof opts.authenticator === "function" ? opts.authenticator(nonce) : {}) || {};
- extend(this, creds);
- }
- };
- var SubscriptionImpl = class extends QueuedIteratorImpl {
- constructor(protocol, subject, opts = {}) {
- var _a;
- super();
- __publicField(this, "sid");
- __publicField(this, "queue");
- __publicField(this, "draining");
- __publicField(this, "max");
- __publicField(this, "subject");
- __publicField(this, "drained");
- __publicField(this, "protocol");
- __publicField(this, "timer");
- __publicField(this, "info");
- __publicField(this, "cleanupFn");
- __publicField(this, "closed");
- __publicField(this, "requestSubject");
- extend(this, opts);
- this.protocol = protocol;
- this.subject = subject;
- this.draining = false;
- this.noIterator = typeof opts.callback === "function";
- this.closed = deferred();
- const asyncTraces = !(((_a = protocol.options) == null ? void 0 : _a.noAsyncTraces) || false);
- if (opts.timeout) {
- this.timer = timeout(opts.timeout, asyncTraces);
- this.timer.then(() => {
- this.timer = void 0;
- }).catch((err) => {
- this.stop(err);
- if (this.noIterator) {
- this.callback(err, {});
- }
- });
- }
- if (!this.noIterator) {
- this.iterClosed.then(() => {
- this.closed.resolve();
- this.unsubscribe();
- });
- }
- }
- setPrePostHandlers(opts) {
- if (this.noIterator) {
- const uc = this.callback;
- const ingestion = opts.ingestionFilterFn ? opts.ingestionFilterFn : () => {
- return {
- ingest: true,
- protocol: false
- };
- };
- const filter = opts.protocolFilterFn ? opts.protocolFilterFn : () => {
- return true;
- };
- const dispatched = opts.dispatchedFn ? opts.dispatchedFn : () => {
- };
- this.callback = (err, msg) => {
- const { ingest } = ingestion(msg);
- if (!ingest) {
- return;
- }
- if (filter(msg)) {
- uc(err, msg);
- dispatched(msg);
- }
- };
- } else {
- this.protocolFilterFn = opts.protocolFilterFn;
- this.dispatchedFn = opts.dispatchedFn;
- }
- }
- callback(err, msg) {
- this.cancelTimeout();
- err ? this.stop(err) : this.push(msg);
- }
- close() {
- if (!this.isClosed()) {
- this.cancelTimeout();
- const fn = () => {
- this.stop();
- if (this.cleanupFn) {
- try {
- this.cleanupFn(this, this.info);
- } catch (_err) {
- }
- }
- this.closed.resolve();
- };
- if (this.noIterator) {
- fn();
- } else {
- this.push(fn);
- }
- }
- }
- unsubscribe(max) {
- this.protocol.unsubscribe(this, max);
- }
- cancelTimeout() {
- if (this.timer) {
- this.timer.cancel();
- this.timer = void 0;
- }
- }
- drain() {
- if (this.protocol.isClosed()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionClosed));
- }
- if (this.isClosed()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.SubClosed));
- }
- if (!this.drained) {
- this.draining = true;
- this.protocol.unsub(this);
- this.drained = this.protocol.flush(deferred()).then(() => {
- this.protocol.subscriptions.cancel(this);
- }).catch(() => {
- this.protocol.subscriptions.cancel(this);
- });
- }
- return this.drained;
- }
- isDraining() {
- return this.draining;
- }
- isClosed() {
- return this.done;
- }
- getSubject() {
- return this.subject;
- }
- getMax() {
- return this.max;
- }
- getID() {
- return this.sid;
- }
- };
- var Subscriptions = class {
- constructor() {
- __publicField(this, "mux");
- __publicField(this, "subs");
- __publicField(this, "sidCounter");
- this.sidCounter = 0;
- this.mux = null;
- this.subs = /* @__PURE__ */ new Map();
- }
- size() {
- return this.subs.size;
- }
- add(s) {
- this.sidCounter++;
- s.sid = this.sidCounter;
- this.subs.set(s.sid, s);
- return s;
- }
- setMux(s) {
- this.mux = s;
- return s;
- }
- getMux() {
- return this.mux;
- }
- get(sid) {
- return this.subs.get(sid);
- }
- resub(s) {
- this.sidCounter++;
- this.subs.delete(s.sid);
- s.sid = this.sidCounter;
- this.subs.set(s.sid, s);
- return s;
- }
- all() {
- return Array.from(this.subs.values());
- }
- cancel(s) {
- if (s) {
- s.close();
- this.subs.delete(s.sid);
- }
- }
- handleError(err) {
- if (err && err.permissionContext) {
- const ctx = err.permissionContext;
- const subs = this.all();
- let sub;
- if (ctx.operation === "subscription") {
- sub = subs.find((s) => {
- return s.subject === ctx.subject;
- });
- }
- if (ctx.operation === "publish") {
- sub = subs.find((s) => {
- return s.requestSubject === ctx.subject;
- });
- }
- if (sub) {
- sub.callback(err, {});
- sub.close();
- this.subs.delete(sub.sid);
- return sub !== this.mux;
- }
- }
- return false;
- }
- close() {
- this.subs.forEach((sub) => {
- sub.close();
- });
- }
- };
- var ProtocolHandler = class {
- constructor(options, publisher) {
- __publicField(this, "connected");
- __publicField(this, "connectedOnce");
- __publicField(this, "infoReceived");
- __publicField(this, "info");
- __publicField(this, "muxSubscriptions");
- __publicField(this, "options");
- __publicField(this, "outbound");
- __publicField(this, "pongs");
- __publicField(this, "subscriptions");
- __publicField(this, "transport");
- __publicField(this, "noMorePublishing");
- __publicField(this, "connectError");
- __publicField(this, "publisher");
- __publicField(this, "_closed");
- __publicField(this, "closed");
- __publicField(this, "listeners");
- __publicField(this, "heartbeats");
- __publicField(this, "parser");
- __publicField(this, "outMsgs");
- __publicField(this, "inMsgs");
- __publicField(this, "outBytes");
- __publicField(this, "inBytes");
- __publicField(this, "pendingLimit");
- __publicField(this, "lastError");
- __publicField(this, "abortReconnect");
- __publicField(this, "servers");
- __publicField(this, "server");
- __publicField(this, "features");
- __publicField(this, "connectPromise");
- this._closed = false;
- this.connected = false;
- this.connectedOnce = false;
- this.infoReceived = false;
- this.noMorePublishing = false;
- this.abortReconnect = false;
- this.listeners = [];
- this.pendingLimit = FLUSH_THRESHOLD;
- this.outMsgs = 0;
- this.inMsgs = 0;
- this.outBytes = 0;
- this.inBytes = 0;
- this.options = options;
- this.publisher = publisher;
- this.subscriptions = new Subscriptions();
- this.muxSubscriptions = new MuxSubscription();
- this.outbound = new DataBuffer();
- this.pongs = [];
- this.pendingLimit = options.pendingLimit || this.pendingLimit;
- this.features = new Features({
- major: 0,
- minor: 0,
- micro: 0
- });
- this.connectPromise = null;
- const servers = typeof options.servers === "string" ? [
- options.servers
- ] : options.servers;
- this.servers = new Servers(servers, {
- randomize: !options.noRandomize
- });
- this.closed = deferred();
- this.parser = new Parser(this);
- this.heartbeats = new Heartbeat(this, this.options.pingInterval || DEFAULT_PING_INTERVAL, this.options.maxPingOut || DEFAULT_MAX_PING_OUT);
- }
- resetOutbound() {
- this.outbound.reset();
- const pongs = this.pongs;
- this.pongs = [];
- const err = NatsError.errorForCode(ErrorCode.Disconnect);
- err.stack = "";
- pongs.forEach((p) => {
- p.reject(err);
- });
- this.parser = new Parser(this);
- this.infoReceived = false;
- }
- dispatchStatus(status) {
- this.listeners.forEach((q) => {
- q.push(status);
- });
- }
- status() {
- const iter = new QueuedIteratorImpl();
- this.listeners.push(iter);
- return iter;
- }
- prepare() {
- if (this.transport) {
- this.transport.discard();
- }
- this.info = void 0;
- this.resetOutbound();
- const pong = deferred();
- pong.catch(() => {
- });
- this.pongs.unshift(pong);
- this.connectError = (err) => {
- pong.reject(err);
- };
- this.transport = newTransport();
- this.transport.closed().then(async (_err) => {
- this.connected = false;
- if (!this.isClosed()) {
- await this.disconnected(this.transport.closeError || this.lastError);
- return;
- }
- });
- return pong;
- }
- disconnect() {
- this.dispatchStatus({
- type: DebugEvents.StaleConnection,
- data: ""
- });
- this.transport.disconnect();
- }
- reconnect() {
- if (this.connected) {
- this.dispatchStatus({
- type: DebugEvents.ClientInitiatedReconnect,
- data: ""
- });
- this.transport.disconnect();
- }
- return Promise.resolve();
- }
- async disconnected(err) {
- this.dispatchStatus({
- type: Events.Disconnect,
- data: this.servers.getCurrentServer().toString()
- });
- if (this.options.reconnect) {
- await this.dialLoop().then(() => {
- var _a;
- this.dispatchStatus({
- type: Events.Reconnect,
- data: this.servers.getCurrentServer().toString()
- });
- if (((_a = this.lastError) == null ? void 0 : _a.code) === ErrorCode.AuthenticationExpired) {
- this.lastError = void 0;
- }
- }).catch((err2) => {
- this._close(err2);
- });
- } else {
- await this._close(err);
- }
- }
- async dial(srv) {
- const pong = this.prepare();
- let timer;
- try {
- timer = timeout(this.options.timeout || 2e4);
- const cp = this.transport.connect(srv, this.options);
- await Promise.race([
- cp,
- timer
- ]);
- (async () => {
- try {
- for await (const b of this.transport) {
- this.parser.parse(b);
- }
- } catch (err) {
- console.log("reader closed", err);
- }
- })().then();
- } catch (err) {
- pong.reject(err);
- }
- try {
- await Promise.race([
- timer,
- pong
- ]);
- if (timer) {
- timer.cancel();
- }
- this.connected = true;
- this.connectError = void 0;
- this.sendSubscriptions();
- this.connectedOnce = true;
- this.server.didConnect = true;
- this.server.reconnects = 0;
- this.flushPending();
- this.heartbeats.start();
- } catch (err) {
- if (timer) {
- timer.cancel();
- }
- await this.transport.close(err);
- throw err;
- }
- }
- async _doDial(srv) {
- const alts = await srv.resolve({
- fn: getResolveFn(),
- debug: this.options.debug,
- randomize: !this.options.noRandomize
- });
- let lastErr = null;
- for (const a of alts) {
- try {
- lastErr = null;
- this.dispatchStatus({
- type: DebugEvents.Reconnecting,
- data: a.toString()
- });
- await this.dial(a);
- return;
- } catch (err) {
- lastErr = err;
- }
- }
- throw lastErr;
- }
- dialLoop() {
- if (this.connectPromise === null) {
- this.connectPromise = this.dodialLoop();
- this.connectPromise.then(() => {
- }).catch(() => {
- }).finally(() => {
- this.connectPromise = null;
- });
- }
- return this.connectPromise;
- }
- async dodialLoop() {
- let lastError;
- while (true) {
- if (this._closed) {
- this.servers.clear();
- }
- const wait = this.options.reconnectDelayHandler ? this.options.reconnectDelayHandler() : DEFAULT_RECONNECT_TIME_WAIT;
- let maxWait = wait;
- const srv = this.selectServer();
- if (!srv || this.abortReconnect) {
- if (lastError) {
- throw lastError;
- } else if (this.lastError) {
- throw this.lastError;
- } else {
- throw NatsError.errorForCode(ErrorCode.ConnectionRefused);
- }
- }
- const now = Date.now();
- if (srv.lastConnect === 0 || srv.lastConnect + wait <= now) {
- srv.lastConnect = Date.now();
- try {
- await this._doDial(srv);
- break;
- } catch (err) {
- lastError = err;
- if (!this.connectedOnce) {
- if (this.options.waitOnFirstConnect) {
- continue;
- }
- this.servers.removeCurrentServer();
- }
- srv.reconnects++;
- const mra = this.options.maxReconnectAttempts || 0;
- if (mra !== -1 && srv.reconnects >= mra) {
- this.servers.removeCurrentServer();
- }
- }
- } else {
- maxWait = Math.min(maxWait, srv.lastConnect + wait - now);
- await delay(maxWait);
- }
- }
- }
- static async connect(options, publisher) {
- const h = new ProtocolHandler(options, publisher);
- await h.dialLoop();
- return h;
- }
- static toError(s) {
- const t = s ? s.toLowerCase() : "";
- if (t.indexOf("permissions violation") !== -1) {
- const err = new NatsError(s, ErrorCode.PermissionsViolation);
- const m = s.match(/(Publish|Subscription) to "(\S+)"/);
- if (m) {
- err.permissionContext = {
- operation: m[1].toLowerCase(),
- subject: m[2]
- };
- }
- return err;
- } else if (t.indexOf("authorization violation") !== -1) {
- return new NatsError(s, ErrorCode.AuthorizationViolation);
- } else if (t.indexOf("user authentication expired") !== -1) {
- return new NatsError(s, ErrorCode.AuthenticationExpired);
- } else if (t.indexOf("authentication timeout") !== -1) {
- return new NatsError(s, ErrorCode.AuthenticationTimeout);
- } else {
- return new NatsError(s, ErrorCode.ProtocolError);
- }
- }
- processMsg(msg, data) {
- this.inMsgs++;
- this.inBytes += data.length;
- if (!this.subscriptions.sidCounter) {
- return;
- }
- const sub = this.subscriptions.get(msg.sid);
- if (!sub) {
- return;
- }
- sub.received += 1;
- if (sub.callback) {
- sub.callback(null, new MsgImpl(msg, data, this));
- }
- if (sub.max !== void 0 && sub.received >= sub.max) {
- sub.unsubscribe();
- }
- }
- processError(m) {
- const s = decode(m);
- const err = ProtocolHandler.toError(s);
- const status = {
- type: Events.Error,
- data: err.code
- };
- if (err.isPermissionError()) {
- let isMuxPermissionError = false;
- if (err.permissionContext) {
- status.permissionContext = err.permissionContext;
- const mux = this.subscriptions.getMux();
- isMuxPermissionError = (mux == null ? void 0 : mux.subject) === err.permissionContext.subject;
- }
- this.subscriptions.handleError(err);
- this.muxSubscriptions.handleError(isMuxPermissionError, err);
- if (isMuxPermissionError) {
- this.subscriptions.setMux(null);
- }
- }
- this.dispatchStatus(status);
- this.handleError(err);
- }
- handleError(err) {
- if (err.isAuthError()) {
- this.handleAuthError(err);
- } else if (err.isProtocolError()) {
- this.lastError = err;
- } else if (err.isAuthTimeout()) {
- this.lastError = err;
- }
- if (!err.isPermissionError()) {
- this.lastError = err;
- }
- }
- handleAuthError(err) {
- if (this.lastError && err.code === this.lastError.code && this.options.ignoreAuthErrorAbort === false) {
- this.abortReconnect = true;
- }
- if (this.connectError) {
- this.connectError(err);
- } else {
- this.disconnect();
- }
- }
- processPing() {
- this.transport.send(PONG_CMD);
- }
- processPong() {
- const cb = this.pongs.shift();
- if (cb) {
- cb.resolve();
- }
- }
- processInfo(m) {
- const info = JSON.parse(decode(m));
- this.info = info;
- const updates = this.options && this.options.ignoreClusterUpdates ? void 0 : this.servers.update(info);
- if (!this.infoReceived) {
- this.features.update(parseSemVer(info.version));
- this.infoReceived = true;
- if (this.transport.isEncrypted()) {
- this.servers.updateTLSName();
- }
- const { version, lang } = this.transport;
- try {
- const c = new Connect({
- version,
- lang
- }, this.options, info.nonce);
- if (info.headers) {
- c.headers = true;
- c.no_responders = true;
- }
- const cs = JSON.stringify(c);
- this.transport.send(encode(`CONNECT ${cs}${CR_LF}`));
- this.transport.send(PING_CMD);
- } catch (err) {
- this._close(err);
- }
- }
- if (updates) {
- this.dispatchStatus({
- type: Events.Update,
- data: updates
- });
- }
- const ldm = info.ldm !== void 0 ? info.ldm : false;
- if (ldm) {
- this.dispatchStatus({
- type: Events.LDM,
- data: this.servers.getCurrentServer().toString()
- });
- }
- }
- push(e) {
- switch (e.kind) {
- case Kind.MSG: {
- const { msg, data } = e;
- this.processMsg(msg, data);
- break;
- }
- case Kind.OK:
- break;
- case Kind.ERR:
- this.processError(e.data);
- break;
- case Kind.PING:
- this.processPing();
- break;
- case Kind.PONG:
- this.processPong();
- break;
- case Kind.INFO:
- this.processInfo(e.data);
- break;
- }
- }
- sendCommand(cmd, ...payloads) {
- const len = this.outbound.length();
- let buf;
- if (typeof cmd === "string") {
- buf = encode(cmd);
- } else {
- buf = cmd;
- }
- this.outbound.fill(buf, ...payloads);
- if (len === 0) {
- queueMicrotask(() => {
- this.flushPending();
- });
- } else if (this.outbound.size() >= this.pendingLimit) {
- this.flushPending();
- }
- }
- publish(subject, payload = Empty, options) {
- let data;
- if (payload instanceof Uint8Array) {
- data = payload;
- } else if (typeof payload === "string") {
- data = TE.encode(payload);
- } else {
- throw NatsError.errorForCode(ErrorCode.BadPayload);
- }
- let len = data.length;
- options = options || {};
- options.reply = options.reply || "";
- let headers2 = Empty;
- let hlen = 0;
- if (options.headers) {
- if (this.info && !this.info.headers) {
- throw new NatsError("headers", ErrorCode.ServerOptionNotAvailable);
- }
- const hdrs = options.headers;
- headers2 = hdrs.encode();
- hlen = headers2.length;
- len = data.length + hlen;
- }
- if (this.info && len > this.info.max_payload) {
- throw NatsError.errorForCode(ErrorCode.MaxPayloadExceeded);
- }
- this.outBytes += len;
- this.outMsgs++;
- let proto;
- if (options.headers) {
- if (options.reply) {
- proto = `HPUB ${subject} ${options.reply} ${hlen} ${len}\r
- `;
- } else {
- proto = `HPUB ${subject} ${hlen} ${len}\r
- `;
- }
- this.sendCommand(proto, headers2, data, CRLF);
- } else {
- if (options.reply) {
- proto = `PUB ${subject} ${options.reply} ${len}\r
- `;
- } else {
- proto = `PUB ${subject} ${len}\r
- `;
- }
- this.sendCommand(proto, data, CRLF);
- }
- }
- request(r) {
- this.initMux();
- this.muxSubscriptions.add(r);
- return r;
- }
- subscribe(s) {
- this.subscriptions.add(s);
- this._subunsub(s);
- return s;
- }
- _sub(s) {
- if (s.queue) {
- this.sendCommand(`SUB ${s.subject} ${s.queue} ${s.sid}\r
- `);
- } else {
- this.sendCommand(`SUB ${s.subject} ${s.sid}\r
- `);
- }
- }
- _subunsub(s) {
- this._sub(s);
- if (s.max) {
- this.unsubscribe(s, s.max);
- }
- return s;
- }
- unsubscribe(s, max) {
- this.unsub(s, max);
- if (s.max === void 0 || s.received >= s.max) {
- this.subscriptions.cancel(s);
- }
- }
- unsub(s, max) {
- if (!s || this.isClosed()) {
- return;
- }
- if (max) {
- this.sendCommand(`UNSUB ${s.sid} ${max}\r
- `);
- } else {
- this.sendCommand(`UNSUB ${s.sid}\r
- `);
- }
- s.max = max;
- }
- resub(s, subject) {
- if (!s || this.isClosed()) {
- return;
- }
- s.subject = subject;
- this.subscriptions.resub(s);
- this._sub(s);
- }
- flush(p) {
- if (!p) {
- p = deferred();
- }
- this.pongs.push(p);
- this.outbound.fill(PING_CMD);
- this.flushPending();
- return p;
- }
- sendSubscriptions() {
- const cmds = [];
- this.subscriptions.all().forEach((s) => {
- const sub = s;
- if (sub.queue) {
- cmds.push(`SUB ${sub.subject} ${sub.queue} ${sub.sid}${CR_LF}`);
- } else {
- cmds.push(`SUB ${sub.subject} ${sub.sid}${CR_LF}`);
- }
- });
- if (cmds.length) {
- this.transport.send(encode(cmds.join("")));
- }
- }
- async _close(err) {
- if (this._closed) {
- return;
- }
- this.heartbeats.cancel();
- if (this.connectError) {
- this.connectError(err);
- this.connectError = void 0;
- }
- this.muxSubscriptions.close();
- this.subscriptions.close();
- this.listeners.forEach((l) => {
- l.stop();
- });
- this._closed = true;
- await this.transport.close(err);
- await this.closed.resolve(err);
- }
- close() {
- return this._close();
- }
- isClosed() {
- return this._closed;
- }
- drain() {
- const subs = this.subscriptions.all();
- const promises = [];
- subs.forEach((sub) => {
- promises.push(sub.drain());
- });
- return Promise.all(promises).then(async () => {
- this.noMorePublishing = true;
- await this.flush();
- return this.close();
- }).catch(() => {
- });
- }
- flushPending() {
- if (!this.infoReceived || !this.connected) {
- return;
- }
- if (this.outbound.size()) {
- const d = this.outbound.drain();
- this.transport.send(d);
- }
- }
- initMux() {
- const mux = this.subscriptions.getMux();
- if (!mux) {
- const inbox = this.muxSubscriptions.init(this.options.inboxPrefix);
- const sub = new SubscriptionImpl(this, `${inbox}*`);
- sub.callback = this.muxSubscriptions.dispatcher();
- this.subscriptions.setMux(sub);
- this.subscribe(sub);
- }
- }
- selectServer() {
- const server = this.servers.selectServer();
- if (server === void 0) {
- return void 0;
- }
- this.server = server;
- return this.server;
- }
- getServer() {
- return this.server;
- }
- };
- var BaseRequest = class {
- constructor(mux, requestSubject, asyncTraces = true) {
- __publicField(this, "token");
- __publicField(this, "received");
- __publicField(this, "ctx");
- __publicField(this, "requestSubject");
- __publicField(this, "mux");
- this.mux = mux;
- this.requestSubject = requestSubject;
- this.received = 0;
- this.token = nuid.next();
- if (asyncTraces) {
- this.ctx = new Error();
- }
- }
- };
- var RequestMany = class extends BaseRequest {
- constructor(mux, requestSubject, opts = {
- maxWait: 1e3
- }) {
- super(mux, requestSubject);
- __publicField(this, "callback");
- __publicField(this, "done");
- __publicField(this, "timer");
- __publicField(this, "max");
- __publicField(this, "opts");
- this.opts = opts;
- if (typeof this.opts.callback !== "function") {
- throw new Error("callback is required");
- }
- this.callback = this.opts.callback;
- this.max = typeof opts.maxMessages === "number" && opts.maxMessages > 0 ? opts.maxMessages : -1;
- this.done = deferred();
- this.done.then(() => {
- this.callback(null, null);
- });
- this.timer = setTimeout(() => {
- this.cancel();
- }, opts.maxWait);
- }
- cancel(err) {
- if (err) {
- this.callback(err, null);
- }
- clearTimeout(this.timer);
- this.mux.cancel(this);
- this.done.resolve();
- }
- resolver(err, msg) {
- if (err) {
- if (this.ctx) {
- err.stack += `
- ${this.ctx.stack}`;
- }
- this.cancel(err);
- } else {
- this.callback(null, msg);
- if (this.opts.strategy === RequestStrategy.Count) {
- this.max--;
- if (this.max === 0) {
- this.cancel();
- }
- }
- if (this.opts.strategy === RequestStrategy.JitterTimer) {
- clearTimeout(this.timer);
- this.timer = setTimeout(() => {
- this.cancel();
- }, this.opts.jitter || 300);
- }
- if (this.opts.strategy === RequestStrategy.SentinelMsg) {
- if (msg && msg.data.length === 0) {
- this.cancel();
- }
- }
- }
- }
- };
- var RequestOne = class extends BaseRequest {
- constructor(mux, requestSubject, opts = {
- timeout: 1e3
- }, asyncTraces = true) {
- super(mux, requestSubject, asyncTraces);
- __publicField(this, "deferred");
- __publicField(this, "timer");
- this.deferred = deferred();
- this.timer = timeout(opts.timeout, asyncTraces);
- }
- resolver(err, msg) {
- if (this.timer) {
- this.timer.cancel();
- }
- if (err) {
- if (this.ctx) {
- err.stack += `
- ${this.ctx.stack}`;
- }
- this.deferred.reject(err);
- } else {
- this.deferred.resolve(msg);
- }
- this.cancel();
- }
- cancel(err) {
- if (this.timer) {
- this.timer.cancel();
- }
- this.mux.cancel(this);
- this.deferred.reject(err ? err : NatsError.errorForCode(ErrorCode.Cancelled));
- }
- };
- function validateDurableName(name) {
- return minValidation("durable", name);
- }
- function validateStreamName(name) {
- return minValidation("stream", name);
- }
- function minValidation(context, name = "") {
- if (name === "") {
- throw Error(`${context} name required`);
- }
- const bad = [
- ".",
- "*",
- ">",
- "/",
- "\\",
- " ",
- " ",
- "\n",
- "\r"
- ];
- bad.forEach((v) => {
- if (name.indexOf(v) !== -1) {
- switch (v) {
- case "\n":
- v = "\\n";
- break;
- case "\r":
- v = "\\r";
- break;
- case " ":
- v = "\\t";
- break;
- default:
- }
- throw Error(`invalid ${context} name - ${context} name cannot contain '${v}'`);
- }
- });
- return "";
- }
- function validateName(context, name = "") {
- if (name === "") {
- throw Error(`${context} name required`);
- }
- const m = validName(name);
- if (m.length) {
- throw new Error(`invalid ${context} name - ${context} name ${m}`);
- }
- }
- function validName(name = "") {
- if (name === "") {
- throw Error(`name required`);
- }
- const RE = /^[-\w]+$/g;
- const m = name.match(RE);
- if (m === null) {
- for (const c of name.split("")) {
- const mm = c.match(RE);
- if (mm === null) {
- return `cannot contain '${c}'`;
- }
- }
- }
- return "";
- }
- function nanos(millis2) {
- return millis2 * 1e6;
- }
- function millis(ns) {
- return Math.floor(ns / 1e6);
- }
- function isFlowControlMsg(msg) {
- if (msg.data.length > 0) {
- return false;
- }
- const h = msg.headers;
- if (!h) {
- return false;
- }
- return h.code >= 100 && h.code < 200;
- }
- function isHeartbeatMsg(msg) {
- var _a;
- return isFlowControlMsg(msg) && ((_a = msg.headers) == null ? void 0 : _a.description) === "Idle Heartbeat";
- }
- function newJsErrorMsg(code2, description, subject) {
- const h = headers(code2, description);
- const arg = {
- hdr: 1,
- sid: 0,
- size: 0
- };
- const msg = new MsgImpl(arg, Empty, {});
- msg._headers = h;
- msg._subject = subject;
- return msg;
- }
- function checkJsError(msg) {
- if (msg.data.length !== 0) {
- return null;
- }
- const h = msg.headers;
- if (!h) {
- return null;
- }
- return checkJsErrorCode(h.code, h.description);
- }
- var Js409Errors;
- (function(Js409Errors2) {
- Js409Errors2["MaxBatchExceeded"] = "exceeded maxrequestbatch of";
- Js409Errors2["MaxExpiresExceeded"] = "exceeded maxrequestexpires of";
- Js409Errors2["MaxBytesExceeded"] = "exceeded maxrequestmaxbytes of";
- Js409Errors2["MaxMessageSizeExceeded"] = "message size exceeds maxbytes";
- Js409Errors2["PushConsumer"] = "consumer is push based";
- Js409Errors2["MaxWaitingExceeded"] = "exceeded maxwaiting";
- Js409Errors2["IdleHeartbeatMissed"] = "idle heartbeats missed";
- Js409Errors2["ConsumerDeleted"] = "consumer deleted";
- })(Js409Errors || (Js409Errors = {}));
- var MAX_WAITING_FAIL = false;
- function isTerminal409(err) {
- if (err.code !== ErrorCode.JetStream409) {
- return false;
- }
- const fatal = [
- Js409Errors.MaxBatchExceeded,
- Js409Errors.MaxExpiresExceeded,
- Js409Errors.MaxBytesExceeded,
- Js409Errors.MaxMessageSizeExceeded,
- Js409Errors.PushConsumer,
- Js409Errors.IdleHeartbeatMissed,
- Js409Errors.ConsumerDeleted
- ];
- if (MAX_WAITING_FAIL) {
- fatal.push(Js409Errors.MaxWaitingExceeded);
- }
- return fatal.find((s) => {
- return err.message.indexOf(s) !== -1;
- }) !== void 0;
- }
- function checkJsErrorCode(code2, description = "") {
- if (code2 < 300) {
- return null;
- }
- description = description.toLowerCase();
- switch (code2) {
- case 404:
- return new NatsError(description, ErrorCode.JetStream404NoMessages);
- case 408:
- return new NatsError(description, ErrorCode.JetStream408RequestTimeout);
- case 409: {
- const ec = description.startsWith(Js409Errors.IdleHeartbeatMissed) ? ErrorCode.JetStreamIdleHeartBeat : ErrorCode.JetStream409;
- return new NatsError(description, ec);
- }
- case 503:
- return NatsError.errorForCode(ErrorCode.JetStreamNotEnabled, new Error(description));
- default:
- if (description === "") {
- description = ErrorCode.Unknown;
- }
- return new NatsError(description, `${code2}`);
- }
- }
- var defaultPrefix = "$JS.API";
- function defaultJsOptions(opts) {
- opts = opts || {};
- if (opts.domain) {
- opts.apiPrefix = `$JS.${opts.domain}.API`;
- delete opts.domain;
- }
- return extend({
- apiPrefix: defaultPrefix,
- timeout: 5e3
- }, opts);
- }
- var BaseApiClient = class {
- constructor(nc, opts) {
- __publicField(this, "nc");
- __publicField(this, "opts");
- __publicField(this, "prefix");
- __publicField(this, "timeout");
- __publicField(this, "jc");
- this.nc = nc;
- this.opts = defaultJsOptions(opts);
- this._parseOpts();
- this.prefix = this.opts.apiPrefix;
- this.timeout = this.opts.timeout;
- this.jc = JSONCodec();
- }
- getOptions() {
- return Object.assign({}, this.opts);
- }
- _parseOpts() {
- let prefix = this.opts.apiPrefix;
- if (!prefix || prefix.length === 0) {
- throw new Error("invalid empty prefix");
- }
- const c = prefix[prefix.length - 1];
- if (c === ".") {
- prefix = prefix.substr(0, prefix.length - 1);
- }
- this.opts.apiPrefix = prefix;
- }
- async _request(subj, data = null, opts) {
- opts = opts || {};
- opts.timeout = this.timeout;
- let a = Empty;
- if (data) {
- a = this.jc.encode(data);
- }
- const m = await this.nc.request(subj, a, opts);
- return this.parseJsResponse(m);
- }
- async findStream(subject) {
- const q = {
- subject
- };
- const r = await this._request(`${this.prefix}.STREAM.NAMES`, q);
- const names = r;
- if (!names.streams || names.streams.length !== 1) {
- throw new Error("no stream matches subject");
- }
- return names.streams[0];
- }
- getConnection() {
- return this.nc;
- }
- parseJsResponse(m) {
- const v = this.jc.decode(m.data);
- const r = v;
- if (r.error) {
- const err = checkJsErrorCode(r.error.code, r.error.description);
- if (err !== null) {
- err.api_error = r.error;
- throw err;
- }
- }
- return v;
- }
- };
- var Base64Codec = class {
- static encode(bytes) {
- if (typeof bytes === "string") {
- return btoa(bytes);
- }
- const a = Array.from(bytes);
- return btoa(String.fromCharCode(...a));
- }
- static decode(s, binary = false) {
- const bin = atob(s);
- if (!binary) {
- return bin;
- }
- return Uint8Array.from(bin, (c) => c.charCodeAt(0));
- }
- };
- var Base64UrlPaddedCodec = class {
- static encode(bytes) {
- return Base64UrlPaddedCodec.toB64URLEncoding(Base64Codec.encode(bytes));
- }
- static decode(s, binary = false) {
- return Base64UrlPaddedCodec.decode(Base64UrlPaddedCodec.fromB64URLEncoding(s), binary);
- }
- static toB64URLEncoding(b64str) {
- return b64str.replace(/\+/g, "-").replace(/\//g, "_");
- }
- static fromB64URLEncoding(b64str) {
- return b64str.replace(/_/g, "/").replace(/-/g, "+");
- }
- };
- var RetentionPolicy;
- (function(RetentionPolicy2) {
- RetentionPolicy2["Limits"] = "limits";
- RetentionPolicy2["Interest"] = "interest";
- RetentionPolicy2["Workqueue"] = "workqueue";
- })(RetentionPolicy || (RetentionPolicy = {}));
- var DiscardPolicy;
- (function(DiscardPolicy2) {
- DiscardPolicy2["Old"] = "old";
- DiscardPolicy2["New"] = "new";
- })(DiscardPolicy || (DiscardPolicy = {}));
- var StorageType;
- (function(StorageType2) {
- StorageType2["File"] = "file";
- StorageType2["Memory"] = "memory";
- })(StorageType || (StorageType = {}));
- var DeliverPolicy;
- (function(DeliverPolicy2) {
- DeliverPolicy2["All"] = "all";
- DeliverPolicy2["Last"] = "last";
- DeliverPolicy2["New"] = "new";
- DeliverPolicy2["StartSequence"] = "by_start_sequence";
- DeliverPolicy2["StartTime"] = "by_start_time";
- DeliverPolicy2["LastPerSubject"] = "last_per_subject";
- })(DeliverPolicy || (DeliverPolicy = {}));
- var AckPolicy;
- (function(AckPolicy2) {
- AckPolicy2["None"] = "none";
- AckPolicy2["All"] = "all";
- AckPolicy2["Explicit"] = "explicit";
- AckPolicy2["NotSet"] = "";
- })(AckPolicy || (AckPolicy = {}));
- var ReplayPolicy;
- (function(ReplayPolicy2) {
- ReplayPolicy2["Instant"] = "instant";
- ReplayPolicy2["Original"] = "original";
- })(ReplayPolicy || (ReplayPolicy = {}));
- var StoreCompression;
- (function(StoreCompression2) {
- StoreCompression2["None"] = "none";
- StoreCompression2["S2"] = "s2";
- })(StoreCompression || (StoreCompression = {}));
- var ConsumerApiAction;
- (function(ConsumerApiAction2) {
- ConsumerApiAction2["CreateOrUpdate"] = "";
- ConsumerApiAction2["Update"] = "update";
- ConsumerApiAction2["Create"] = "create";
- })(ConsumerApiAction || (ConsumerApiAction = {}));
- function defaultConsumer(name, opts = {}) {
- return Object.assign({
- name,
- deliver_policy: DeliverPolicy.All,
- ack_policy: AckPolicy.Explicit,
- ack_wait: nanos(30 * 1e3),
- replay_policy: ReplayPolicy.Instant
- }, opts);
- }
- var AdvisoryKind;
- (function(AdvisoryKind2) {
- AdvisoryKind2["API"] = "api_audit";
- AdvisoryKind2["StreamAction"] = "stream_action";
- AdvisoryKind2["ConsumerAction"] = "consumer_action";
- AdvisoryKind2["SnapshotCreate"] = "snapshot_create";
- AdvisoryKind2["SnapshotComplete"] = "snapshot_complete";
- AdvisoryKind2["RestoreCreate"] = "restore_create";
- AdvisoryKind2["RestoreComplete"] = "restore_complete";
- AdvisoryKind2["MaxDeliver"] = "max_deliver";
- AdvisoryKind2["Terminated"] = "terminated";
- AdvisoryKind2["Ack"] = "consumer_ack";
- AdvisoryKind2["StreamLeaderElected"] = "stream_leader_elected";
- AdvisoryKind2["StreamQuorumLost"] = "stream_quorum_lost";
- AdvisoryKind2["ConsumerLeaderElected"] = "consumer_leader_elected";
- AdvisoryKind2["ConsumerQuorumLost"] = "consumer_quorum_lost";
- })(AdvisoryKind || (AdvisoryKind = {}));
- var JsHeaders;
- (function(JsHeaders2) {
- JsHeaders2["StreamSourceHdr"] = "Nats-Stream-Source";
- JsHeaders2["LastConsumerSeqHdr"] = "Nats-Last-Consumer";
- JsHeaders2["LastStreamSeqHdr"] = "Nats-Last-Stream";
- JsHeaders2["ConsumerStalledHdr"] = "Nats-Consumer-Stalled";
- JsHeaders2["MessageSizeHdr"] = "Nats-Msg-Size";
- JsHeaders2["RollupHdr"] = "Nats-Rollup";
- JsHeaders2["RollupValueSubject"] = "sub";
- JsHeaders2["RollupValueAll"] = "all";
- JsHeaders2["PendingMessagesHdr"] = "Nats-Pending-Messages";
- JsHeaders2["PendingBytesHdr"] = "Nats-Pending-Bytes";
- })(JsHeaders || (JsHeaders = {}));
- var KvWatchInclude;
- (function(KvWatchInclude2) {
- KvWatchInclude2["LastValue"] = "";
- KvWatchInclude2["AllHistory"] = "history";
- KvWatchInclude2["UpdatesOnly"] = "updates";
- })(KvWatchInclude || (KvWatchInclude = {}));
- var DirectMsgHeaders;
- (function(DirectMsgHeaders2) {
- DirectMsgHeaders2["Stream"] = "Nats-Stream";
- DirectMsgHeaders2["Sequence"] = "Nats-Sequence";
- DirectMsgHeaders2["TimeStamp"] = "Nats-Time-Stamp";
- DirectMsgHeaders2["Subject"] = "Nats-Subject";
- })(DirectMsgHeaders || (DirectMsgHeaders = {}));
- var RepublishHeaders;
- (function(RepublishHeaders2) {
- RepublishHeaders2["Stream"] = "Nats-Stream";
- RepublishHeaders2["Subject"] = "Nats-Subject";
- RepublishHeaders2["Sequence"] = "Nats-Sequence";
- RepublishHeaders2["LastSequence"] = "Nats-Last-Sequence";
- RepublishHeaders2["Size"] = "Nats-Msg-Size";
- })(RepublishHeaders || (RepublishHeaders = {}));
- var kvPrefix = "KV_";
- var ConsumerOptsBuilderImpl = class {
- constructor(opts) {
- __publicField(this, "config");
- __publicField(this, "ordered");
- __publicField(this, "mack");
- __publicField(this, "stream");
- __publicField(this, "callbackFn");
- __publicField(this, "max");
- __publicField(this, "qname");
- __publicField(this, "isBind");
- __publicField(this, "filters");
- this.stream = "";
- this.mack = false;
- this.ordered = false;
- this.config = defaultConsumer("", opts || {});
- }
- getOpts() {
- var _a;
- const o = {};
- o.config = Object.assign({}, this.config);
- if (o.config.filter_subject) {
- this.filterSubject(o.config.filter_subject);
- o.config.filter_subject = void 0;
- }
- if (o.config.filter_subjects) {
- (_a = o.config.filter_subjects) == null ? void 0 : _a.forEach((v) => {
- this.filterSubject(v);
- });
- o.config.filter_subjects = void 0;
- }
- o.mack = this.mack;
- o.stream = this.stream;
- o.callbackFn = this.callbackFn;
- o.max = this.max;
- o.queue = this.qname;
- o.ordered = this.ordered;
- o.config.ack_policy = o.ordered ? AckPolicy.None : o.config.ack_policy;
- o.isBind = o.isBind || false;
- if (this.filters) {
- switch (this.filters.length) {
- case 0:
- break;
- case 1:
- o.config.filter_subject = this.filters[0];
- break;
- default:
- o.config.filter_subjects = this.filters;
- }
- }
- return o;
- }
- description(description) {
- this.config.description = description;
- return this;
- }
- deliverTo(subject) {
- this.config.deliver_subject = subject;
- return this;
- }
- durable(name) {
- validateDurableName(name);
- this.config.durable_name = name;
- return this;
- }
- startSequence(seq) {
- if (seq <= 0) {
- throw new Error("sequence must be greater than 0");
- }
- this.config.deliver_policy = DeliverPolicy.StartSequence;
- this.config.opt_start_seq = seq;
- return this;
- }
- startTime(time) {
- this.config.deliver_policy = DeliverPolicy.StartTime;
- this.config.opt_start_time = time.toISOString();
- return this;
- }
- deliverAll() {
- this.config.deliver_policy = DeliverPolicy.All;
- return this;
- }
- deliverLastPerSubject() {
- this.config.deliver_policy = DeliverPolicy.LastPerSubject;
- return this;
- }
- deliverLast() {
- this.config.deliver_policy = DeliverPolicy.Last;
- return this;
- }
- deliverNew() {
- this.config.deliver_policy = DeliverPolicy.New;
- return this;
- }
- startAtTimeDelta(millis2) {
- this.startTime(new Date(Date.now() - millis2));
- return this;
- }
- headersOnly() {
- this.config.headers_only = true;
- return this;
- }
- ackNone() {
- this.config.ack_policy = AckPolicy.None;
- return this;
- }
- ackAll() {
- this.config.ack_policy = AckPolicy.All;
- return this;
- }
- ackExplicit() {
- this.config.ack_policy = AckPolicy.Explicit;
- return this;
- }
- ackWait(millis2) {
- this.config.ack_wait = nanos(millis2);
- return this;
- }
- maxDeliver(max) {
- this.config.max_deliver = max;
- return this;
- }
- filterSubject(s) {
- this.filters = this.filters || [];
- this.filters.push(s);
- return this;
- }
- replayInstantly() {
- this.config.replay_policy = ReplayPolicy.Instant;
- return this;
- }
- replayOriginal() {
- this.config.replay_policy = ReplayPolicy.Original;
- return this;
- }
- sample(n) {
- n = Math.trunc(n);
- if (n < 0 || n > 100) {
- throw new Error(`value must be between 0-100`);
- }
- this.config.sample_freq = `${n}%`;
- return this;
- }
- limit(n) {
- this.config.rate_limit_bps = n;
- return this;
- }
- maxWaiting(max) {
- this.config.max_waiting = max;
- return this;
- }
- maxAckPending(max) {
- this.config.max_ack_pending = max;
- return this;
- }
- idleHeartbeat(millis2) {
- this.config.idle_heartbeat = nanos(millis2);
- return this;
- }
- flowControl() {
- this.config.flow_control = true;
- return this;
- }
- deliverGroup(name) {
- this.queue(name);
- return this;
- }
- manualAck() {
- this.mack = true;
- return this;
- }
- maxMessages(max) {
- this.max = max;
- return this;
- }
- callback(fn) {
- this.callbackFn = fn;
- return this;
- }
- queue(n) {
- this.qname = n;
- this.config.deliver_group = n;
- return this;
- }
- orderedConsumer() {
- this.ordered = true;
- return this;
- }
- bind(stream, durable) {
- this.stream = stream;
- this.config.durable_name = durable;
- this.isBind = true;
- return this;
- }
- bindStream(stream) {
- this.stream = stream;
- return this;
- }
- inactiveEphemeralThreshold(millis2) {
- this.config.inactive_threshold = nanos(millis2);
- return this;
- }
- maxPullBatch(n) {
- this.config.max_batch = n;
- return this;
- }
- maxPullRequestExpires(millis2) {
- this.config.max_expires = nanos(millis2);
- return this;
- }
- memory() {
- this.config.mem_storage = true;
- return this;
- }
- numReplicas(n) {
- this.config.num_replicas = n;
- return this;
- }
- };
- function consumerOpts(opts) {
- return new ConsumerOptsBuilderImpl(opts);
- }
- function isConsumerOptsBuilder(o) {
- return typeof o.getOpts === "function";
- }
- function getLengths(b64) {
- const len = b64.length;
- let validLen = b64.indexOf("=");
- if (validLen === -1) {
- validLen = len;
- }
- const placeHoldersLen = validLen === len ? 0 : 4 - validLen % 4;
- return [
- validLen,
- placeHoldersLen
- ];
- }
- function init(lookup2, revLookup2, urlsafe = false) {
- function _byteLength(validLen, placeHoldersLen) {
- return Math.floor((validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen);
- }
- function tripletToBase64(num) {
- return lookup2[num >> 18 & 63] + lookup2[num >> 12 & 63] + lookup2[num >> 6 & 63] + lookup2[num & 63];
- }
- function encodeChunk(buf, start, end) {
- const out = new Array((end - start) / 3);
- for (let i = start, curTriplet = 0; i < end; i += 3) {
- out[curTriplet++] = tripletToBase64((buf[i] << 16) + (buf[i + 1] << 8) + buf[i + 2]);
- }
- return out.join("");
- }
- return {
- byteLength(b64) {
- return _byteLength.apply(null, getLengths(b64));
- },
- toUint8Array(b64) {
- const [validLen, placeHoldersLen] = getLengths(b64);
- const buf = new Uint8Array(_byteLength(validLen, placeHoldersLen));
- const len = placeHoldersLen ? validLen - 4 : validLen;
- let tmp;
- let curByte = 0;
- let i;
- for (i = 0; i < len; i += 4) {
- tmp = revLookup2[b64.charCodeAt(i)] << 18 | revLookup2[b64.charCodeAt(i + 1)] << 12 | revLookup2[b64.charCodeAt(i + 2)] << 6 | revLookup2[b64.charCodeAt(i + 3)];
- buf[curByte++] = tmp >> 16 & 255;
- buf[curByte++] = tmp >> 8 & 255;
- buf[curByte++] = tmp & 255;
- }
- if (placeHoldersLen === 2) {
- tmp = revLookup2[b64.charCodeAt(i)] << 2 | revLookup2[b64.charCodeAt(i + 1)] >> 4;
- buf[curByte++] = tmp & 255;
- } else if (placeHoldersLen === 1) {
- tmp = revLookup2[b64.charCodeAt(i)] << 10 | revLookup2[b64.charCodeAt(i + 1)] << 4 | revLookup2[b64.charCodeAt(i + 2)] >> 2;
- buf[curByte++] = tmp >> 8 & 255;
- buf[curByte++] = tmp & 255;
- }
- return buf;
- },
- fromUint8Array(buf) {
- const maxChunkLength = 16383;
- const len = buf.length;
- const extraBytes = len % 3;
- const len2 = len - extraBytes;
- const parts = new Array(Math.ceil(len2 / 16383) + (extraBytes ? 1 : 0));
- let curChunk = 0;
- let chunkEnd;
- for (let i = 0; i < len2; i += maxChunkLength) {
- chunkEnd = i + maxChunkLength;
- parts[curChunk++] = encodeChunk(buf, i, chunkEnd > len2 ? len2 : chunkEnd);
- }
- let tmp;
- if (extraBytes === 1) {
- tmp = buf[len2];
- parts[curChunk] = lookup2[tmp >> 2] + lookup2[tmp << 4 & 63];
- if (!urlsafe)
- parts[curChunk] += "==";
- } else if (extraBytes === 2) {
- tmp = buf[len2] << 8 | buf[len2 + 1] & 255;
- parts[curChunk] = lookup2[tmp >> 10] + lookup2[tmp >> 4 & 63] + lookup2[tmp << 2 & 63];
- if (!urlsafe)
- parts[curChunk] += "=";
- }
- return parts.join("");
- }
- };
- }
- var lookup = [];
- var revLookup = [];
- var code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
- for (let i = 0, l = code.length; i < l; ++i) {
- lookup[i] = code[i];
- revLookup[code.charCodeAt(i)] = i;
- }
- var { byteLength, toUint8Array, fromUint8Array } = init(lookup, revLookup, true);
- var decoder = new TextDecoder();
- var encoder = new TextEncoder();
- function toHexString(buf) {
- return buf.reduce((hex, __byte) => `${hex}${__byte < 16 ? "0" : ""}${__byte.toString(16)}`, "");
- }
- function fromHexString(hex) {
- const len = hex.length;
- if (len % 2 || !/^[0-9a-fA-F]+$/.test(hex)) {
- throw new TypeError("Invalid hex string.");
- }
- hex = hex.toLowerCase();
- const buf = new Uint8Array(Math.floor(len / 2));
- const end = len / 2;
- for (let i = 0; i < end; ++i) {
- buf[i] = parseInt(hex.substr(i * 2, 2), 16);
- }
- return buf;
- }
- function decode2(buf, encoding = "utf8") {
- if (/^utf-?8$/i.test(encoding)) {
- return decoder.decode(buf);
- } else if (/^base64$/i.test(encoding)) {
- return fromUint8Array(buf);
- } else if (/^hex(?:adecimal)?$/i.test(encoding)) {
- return toHexString(buf);
- } else {
- throw new TypeError("Unsupported string encoding.");
- }
- }
- function encode2(str, encoding = "utf8") {
- if (/^utf-?8$/i.test(encoding)) {
- return encoder.encode(str);
- } else if (/^base64$/i.test(encoding)) {
- return toUint8Array(str);
- } else if (/^hex(?:adecimal)?$/i.test(encoding)) {
- return fromHexString(str);
- } else {
- throw new TypeError("Unsupported string encoding.");
- }
- }
- var SHA256 = class {
- constructor() {
- __publicField(this, "hashSize", 32);
- __publicField(this, "_buf");
- __publicField(this, "_bufIdx");
- __publicField(this, "_count");
- __publicField(this, "_K");
- __publicField(this, "_H");
- __publicField(this, "_finalized");
- this._buf = new Uint8Array(64);
- this._K = new Uint32Array([
- 1116352408,
- 1899447441,
- 3049323471,
- 3921009573,
- 961987163,
- 1508970993,
- 2453635748,
- 2870763221,
- 3624381080,
- 310598401,
- 607225278,
- 1426881987,
- 1925078388,
- 2162078206,
- 2614888103,
- 3248222580,
- 3835390401,
- 4022224774,
- 264347078,
- 604807628,
- 770255983,
- 1249150122,
- 1555081692,
- 1996064986,
- 2554220882,
- 2821834349,
- 2952996808,
- 3210313671,
- 3336571891,
- 3584528711,
- 113926993,
- 338241895,
- 666307205,
- 773529912,
- 1294757372,
- 1396182291,
- 1695183700,
- 1986661051,
- 2177026350,
- 2456956037,
- 2730485921,
- 2820302411,
- 3259730800,
- 3345764771,
- 3516065817,
- 3600352804,
- 4094571909,
- 275423344,
- 430227734,
- 506948616,
- 659060556,
- 883997877,
- 958139571,
- 1322822218,
- 1537002063,
- 1747873779,
- 1955562222,
- 2024104815,
- 2227730452,
- 2361852424,
- 2428436474,
- 2756734187,
- 3204031479,
- 3329325298
- ]);
- this.init();
- }
- init() {
- this._H = new Uint32Array([
- 1779033703,
- 3144134277,
- 1013904242,
- 2773480762,
- 1359893119,
- 2600822924,
- 528734635,
- 1541459225
- ]);
- this._bufIdx = 0;
- this._count = new Uint32Array(2);
- this._buf.fill(0);
- this._finalized = false;
- return this;
- }
- update(msg, inputEncoding) {
- if (msg === null) {
- throw new TypeError("msg must be a string or Uint8Array.");
- } else if (typeof msg === "string") {
- msg = encode2(msg, inputEncoding);
- }
- for (let i = 0, len = msg.length; i < len; i++) {
- this._buf[this._bufIdx++] = msg[i];
- if (this._bufIdx === 64) {
- this._transform();
- this._bufIdx = 0;
- }
- }
- const c = this._count;
- if ((c[0] += msg.length << 3) < msg.length << 3) {
- c[1]++;
- }
- c[1] += msg.length >>> 29;
- return this;
- }
- digest(outputEncoding) {
- if (this._finalized) {
- throw new Error("digest has already been called.");
- }
- this._finalized = true;
- const b = this._buf;
- let idx = this._bufIdx;
- b[idx++] = 128;
- while (idx !== 56) {
- if (idx === 64) {
- this._transform();
- idx = 0;
- }
- b[idx++] = 0;
- }
- const c = this._count;
- b[56] = c[1] >>> 24 & 255;
- b[57] = c[1] >>> 16 & 255;
- b[58] = c[1] >>> 8 & 255;
- b[59] = c[1] >>> 0 & 255;
- b[60] = c[0] >>> 24 & 255;
- b[61] = c[0] >>> 16 & 255;
- b[62] = c[0] >>> 8 & 255;
- b[63] = c[0] >>> 0 & 255;
- this._transform();
- const hash = new Uint8Array(32);
- for (let i = 0; i < 8; i++) {
- hash[(i << 2) + 0] = this._H[i] >>> 24 & 255;
- hash[(i << 2) + 1] = this._H[i] >>> 16 & 255;
- hash[(i << 2) + 2] = this._H[i] >>> 8 & 255;
- hash[(i << 2) + 3] = this._H[i] >>> 0 & 255;
- }
- this.init();
- return outputEncoding ? decode2(hash, outputEncoding) : hash;
- }
- _transform() {
- const h = this._H;
- let h0 = h[0];
- let h1 = h[1];
- let h2 = h[2];
- let h3 = h[3];
- let h4 = h[4];
- let h5 = h[5];
- let h6 = h[6];
- let h7 = h[7];
- const w = new Uint32Array(16);
- let i;
- for (i = 0; i < 16; i++) {
- w[i] = this._buf[(i << 2) + 3] | this._buf[(i << 2) + 2] << 8 | this._buf[(i << 2) + 1] << 16 | this._buf[i << 2] << 24;
- }
- for (i = 0; i < 64; i++) {
- let tmp;
- if (i < 16) {
- tmp = w[i];
- } else {
- let a = w[i + 1 & 15];
- let b = w[i + 14 & 15];
- tmp = w[i & 15] = (a >>> 7 ^ a >>> 18 ^ a >>> 3 ^ a << 25 ^ a << 14) + (b >>> 17 ^ b >>> 19 ^ b >>> 10 ^ b << 15 ^ b << 13) + w[i & 15] + w[i + 9 & 15] | 0;
- }
- tmp = tmp + h7 + (h4 >>> 6 ^ h4 >>> 11 ^ h4 >>> 25 ^ h4 << 26 ^ h4 << 21 ^ h4 << 7) + (h6 ^ h4 & (h5 ^ h6)) + this._K[i] | 0;
- h7 = h6;
- h6 = h5;
- h5 = h4;
- h4 = h3 + tmp;
- h3 = h2;
- h2 = h1;
- h1 = h0;
- h0 = tmp + (h1 & h2 ^ h3 & (h1 ^ h2)) + (h1 >>> 2 ^ h1 >>> 13 ^ h1 >>> 22 ^ h1 << 30 ^ h1 << 19 ^ h1 << 10) | 0;
- }
- h[0] = h[0] + h0 | 0;
- h[1] = h[1] + h1 | 0;
- h[2] = h[2] + h2 | 0;
- h[3] = h[3] + h3 | 0;
- h[4] = h[4] + h4 | 0;
- h[5] = h[5] + h5 | 0;
- h[6] = h[6] + h6 | 0;
- h[7] = h[7] + h7 | 0;
- }
- };
- var ListerImpl = class {
- constructor(subject, filter, jsm, payload) {
- __publicField(this, "err");
- __publicField(this, "offset");
- __publicField(this, "pageInfo");
- __publicField(this, "subject");
- __publicField(this, "jsm");
- __publicField(this, "filter");
- __publicField(this, "payload");
- if (!subject) {
- throw new Error("subject is required");
- }
- this.subject = subject;
- this.jsm = jsm;
- this.offset = 0;
- this.pageInfo = {};
- this.filter = filter;
- this.payload = payload || {};
- }
- async next() {
- if (this.err) {
- return [];
- }
- if (this.pageInfo && this.offset >= this.pageInfo.total) {
- return [];
- }
- const offset = {
- offset: this.offset
- };
- if (this.payload) {
- Object.assign(offset, this.payload);
- }
- try {
- const r = await this.jsm._request(this.subject, offset, {
- timeout: this.jsm.timeout
- });
- this.pageInfo = r;
- this.offset += this.countResponse(r);
- const a = this.filter(r);
- return a;
- } catch (err) {
- this.err = err;
- throw err;
- }
- }
- countResponse(r) {
- var _a;
- switch (r == null ? void 0 : r.type) {
- case "io.nats.jetstream.api.v1.stream_names_response":
- case "io.nats.jetstream.api.v1.stream_list_response":
- return r.streams.length;
- case "io.nats.jetstream.api.v1.consumer_list_response":
- return r.consumers.length;
- default:
- console.error(`jslister.ts: unknown API response for paged output: ${r == null ? void 0 : r.type}`);
- return ((_a = r.streams) == null ? void 0 : _a.length) || 0;
- }
- return 0;
- }
- async *[Symbol.asyncIterator]() {
- let page = await this.next();
- while (page.length > 0) {
- for (const item of page) {
- yield item;
- }
- page = await this.next();
- }
- }
- };
- var ConsumerAPIImpl = class extends BaseApiClient {
- constructor(nc, opts) {
- super(nc, opts);
- }
- async add(stream, cfg, action = ConsumerApiAction.Create) {
- validateStreamName(stream);
- if (cfg.deliver_group && cfg.flow_control) {
- throw new Error("jetstream flow control is not supported with queue groups");
- }
- if (cfg.deliver_group && cfg.idle_heartbeat) {
- throw new Error("jetstream idle heartbeat is not supported with queue groups");
- }
- const cr = {};
- cr.config = cfg;
- cr.stream_name = stream;
- cr.action = action;
- if (cr.config.durable_name) {
- validateDurableName(cr.config.durable_name);
- }
- const nci = this.nc;
- let { min, ok: newAPI } = nci.features.get(Feature.JS_NEW_CONSUMER_CREATE_API);
- const name = cfg.name === "" ? void 0 : cfg.name;
- if (name && !newAPI) {
- throw new Error(`consumer 'name' requires server ${min}`);
- }
- if (name) {
- try {
- minValidation("name", name);
- } catch (err) {
- const m = err.message;
- const idx = m.indexOf("cannot contain");
- if (idx !== -1) {
- throw new Error(`consumer 'name' ${m.substring(idx)}`);
- }
- throw err;
- }
- }
- let subj;
- let consumerName = "";
- if (Array.isArray(cfg.filter_subjects)) {
- const { min: min2, ok } = nci.features.get(Feature.JS_MULTIPLE_CONSUMER_FILTER);
- if (!ok) {
- throw new Error(`consumer 'filter_subjects' requires server ${min2}`);
- }
- newAPI = false;
- }
- if (cfg.metadata) {
- const { min: min2, ok } = nci.features.get(Feature.JS_STREAM_CONSUMER_METADATA);
- if (!ok) {
- throw new Error(`consumer 'metadata' requires server ${min2}`);
- }
- }
- if (newAPI) {
- consumerName = cfg.name ?? cfg.durable_name ?? "";
- }
- if (consumerName !== "") {
- let fs = cfg.filter_subject ?? void 0;
- if (fs === ">") {
- fs = void 0;
- }
- subj = fs !== void 0 ? `${this.prefix}.CONSUMER.CREATE.${stream}.${consumerName}.${fs}` : `${this.prefix}.CONSUMER.CREATE.${stream}.${consumerName}`;
- } else {
- subj = cfg.durable_name ? `${this.prefix}.CONSUMER.DURABLE.CREATE.${stream}.${cfg.durable_name}` : `${this.prefix}.CONSUMER.CREATE.${stream}`;
- }
- const r = await this._request(subj, cr);
- return r;
- }
- async update(stream, durable, cfg) {
- const ci = await this.info(stream, durable);
- const changable = cfg;
- return this.add(stream, Object.assign(ci.config, changable), ConsumerApiAction.Update);
- }
- async info(stream, name) {
- validateStreamName(stream);
- validateDurableName(name);
- const r = await this._request(`${this.prefix}.CONSUMER.INFO.${stream}.${name}`);
- return r;
- }
- async delete(stream, name) {
- validateStreamName(stream);
- validateDurableName(name);
- const r = await this._request(`${this.prefix}.CONSUMER.DELETE.${stream}.${name}`);
- const cr = r;
- return cr.success;
- }
- list(stream) {
- validateStreamName(stream);
- const filter = (v) => {
- const clr = v;
- return clr.consumers;
- };
- const subj = `${this.prefix}.CONSUMER.LIST.${stream}`;
- return new ListerImpl(subj, filter, this);
- }
- };
- var ACK = Uint8Array.of(43, 65, 67, 75);
- var NAK = Uint8Array.of(45, 78, 65, 75);
- var WPI = Uint8Array.of(43, 87, 80, 73);
- var NXT = Uint8Array.of(43, 78, 88, 84);
- var TERM = Uint8Array.of(43, 84, 69, 82, 77);
- var SPACE = Uint8Array.of(32);
- function toJsMsg(m) {
- return new JsMsgImpl(m);
- }
- function parseInfo(s) {
- const tokens = s.split(".");
- if (tokens.length === 9) {
- tokens.splice(2, 0, "_", "");
- }
- if (tokens.length < 11 || tokens[0] !== "$JS" || tokens[1] !== "ACK") {
- throw new Error(`not js message`);
- }
- const di = {};
- di.domain = tokens[2] === "_" ? "" : tokens[2];
- di.account_hash = tokens[3];
- di.stream = tokens[4];
- di.consumer = tokens[5];
- di.redeliveryCount = parseInt(tokens[6], 10);
- di.redelivered = di.redeliveryCount > 1;
- di.streamSequence = parseInt(tokens[7], 10);
- di.deliverySequence = parseInt(tokens[8], 10);
- di.timestampNanos = parseInt(tokens[9], 10);
- di.pending = parseInt(tokens[10], 10);
- return di;
- }
- var JsMsgImpl = class {
- constructor(msg) {
- __publicField(this, "msg");
- __publicField(this, "di");
- __publicField(this, "didAck");
- this.msg = msg;
- this.didAck = false;
- }
- get subject() {
- return this.msg.subject;
- }
- get sid() {
- return this.msg.sid;
- }
- get data() {
- return this.msg.data;
- }
- get headers() {
- return this.msg.headers;
- }
- get info() {
- if (!this.di) {
- this.di = parseInfo(this.reply);
- }
- return this.di;
- }
- get redelivered() {
- return this.info.redeliveryCount > 1;
- }
- get reply() {
- return this.msg.reply || "";
- }
- get seq() {
- return this.info.streamSequence;
- }
- doAck(payload) {
- if (!this.didAck) {
- this.didAck = !this.isWIP(payload);
- this.msg.respond(payload);
- }
- }
- isWIP(p) {
- return p.length === 4 && p[0] === WPI[0] && p[1] === WPI[1] && p[2] === WPI[2] && p[3] === WPI[3];
- }
- async ackAck() {
- var _a;
- if (!this.didAck) {
- this.didAck = true;
- if (this.msg.reply) {
- const mi = this.msg;
- const proto = mi.publisher;
- const trace = !(((_a = proto.options) == null ? void 0 : _a.noAsyncTraces) || false);
- const r = new RequestOne(proto.muxSubscriptions, this.msg.reply, {
- timeout: 1e3
- }, trace);
- proto.request(r);
- try {
- proto.publish(this.msg.reply, ACK, {
- reply: `${proto.muxSubscriptions.baseInbox}${r.token}`
- });
- } catch (err) {
- r.cancel(err);
- }
- try {
- await Promise.race([
- r.timer,
- r.deferred
- ]);
- return true;
- } catch (err) {
- r.cancel(err);
- }
- }
- }
- return false;
- }
- ack() {
- this.doAck(ACK);
- }
- nak(millis2) {
- let payload = NAK;
- if (millis2) {
- payload = StringCodec().encode(`-NAK ${JSON.stringify({
- delay: nanos(millis2)
- })}`);
- }
- this.doAck(payload);
- }
- working() {
- this.doAck(WPI);
- }
- next(subj, opts = {
- batch: 1
- }) {
- const args = {};
- args.batch = opts.batch || 1;
- args.no_wait = opts.no_wait || false;
- if (opts.expires && opts.expires > 0) {
- args.expires = nanos(opts.expires);
- }
- const data = JSONCodec().encode(args);
- const payload = DataBuffer.concat(NXT, SPACE, data);
- const reqOpts = subj ? {
- reply: subj
- } : void 0;
- this.msg.respond(payload, reqOpts);
- }
- term(reason = "") {
- let term = TERM;
- if ((reason == null ? void 0 : reason.length) > 0) {
- term = StringCodec().encode(`+TERM ${reason}`);
- }
- this.doAck(term);
- }
- json() {
- return this.msg.json();
- }
- string() {
- return this.msg.string();
- }
- };
- function checkFn(fn, name, required = false) {
- if (required === true && !fn) {
- throw NatsError.errorForCode(ErrorCode.ApiError, new Error(`${name} is not a function`));
- }
- if (fn && typeof fn !== "function") {
- throw NatsError.errorForCode(ErrorCode.ApiError, new Error(`${name} is not a function`));
- }
- }
- var TypedSubscription = class extends QueuedIteratorImpl {
- constructor(nc, subject, opts) {
- super();
- __publicField(this, "sub");
- __publicField(this, "adapter");
- __publicField(this, "subIterDone");
- checkFn(opts.adapter, "adapter", true);
- this.adapter = opts.adapter;
- if (opts.callback) {
- checkFn(opts.callback, "callback");
- }
- this.noIterator = typeof opts.callback === "function";
- if (opts.ingestionFilterFn) {
- checkFn(opts.ingestionFilterFn, "ingestionFilterFn");
- this.ingestionFilterFn = opts.ingestionFilterFn;
- }
- if (opts.protocolFilterFn) {
- checkFn(opts.protocolFilterFn, "protocolFilterFn");
- this.protocolFilterFn = opts.protocolFilterFn;
- }
- if (opts.dispatchedFn) {
- checkFn(opts.dispatchedFn, "dispatchedFn");
- this.dispatchedFn = opts.dispatchedFn;
- }
- if (opts.cleanupFn) {
- checkFn(opts.cleanupFn, "cleanupFn");
- }
- let callback = (err, msg) => {
- this.callback(err, msg);
- };
- if (opts.callback) {
- const uh = opts.callback;
- callback = (err, msg) => {
- const [jer, tm] = this.adapter(err, msg);
- if (jer) {
- uh(jer, null);
- return;
- }
- const { ingest } = this.ingestionFilterFn ? this.ingestionFilterFn(tm, this) : {
- ingest: true
- };
- if (ingest) {
- const ok = this.protocolFilterFn ? this.protocolFilterFn(tm) : true;
- if (ok) {
- uh(jer, tm);
- if (this.dispatchedFn && tm) {
- this.dispatchedFn(tm);
- }
- }
- }
- };
- }
- const { max, queue, timeout: timeout2 } = opts;
- const sopts = {
- queue,
- timeout: timeout2,
- callback
- };
- if (max && max > 0) {
- sopts.max = max;
- }
- this.sub = nc.subscribe(subject, sopts);
- if (opts.cleanupFn) {
- this.sub.cleanupFn = opts.cleanupFn;
- }
- if (!this.noIterator) {
- this.iterClosed.then(() => {
- this.unsubscribe();
- });
- }
- this.subIterDone = deferred();
- Promise.all([
- this.sub.closed,
- this.iterClosed
- ]).then(() => {
- this.subIterDone.resolve();
- }).catch(() => {
- this.subIterDone.resolve();
- });
- (async (s) => {
- await s.closed;
- this.stop();
- })(this.sub).then().catch();
- }
- unsubscribe(max) {
- this.sub.unsubscribe(max);
- }
- drain() {
- return this.sub.drain();
- }
- isDraining() {
- return this.sub.isDraining();
- }
- isClosed() {
- return this.sub.isClosed();
- }
- callback(e, msg) {
- this.sub.cancelTimeout();
- const [err, tm] = this.adapter(e, msg);
- if (err) {
- this.stop(err);
- }
- if (tm) {
- this.push(tm);
- }
- }
- getSubject() {
- return this.sub.getSubject();
- }
- getReceived() {
- return this.sub.getReceived();
- }
- getProcessed() {
- return this.sub.getProcessed();
- }
- getPending() {
- return this.sub.getPending();
- }
- getID() {
- return this.sub.getID();
- }
- getMax() {
- return this.sub.getMax();
- }
- get closed() {
- return this.sub.closed;
- }
- };
- var IdleHeartbeatMonitor = class {
- constructor(interval, cb, opts = {
- maxOut: 2
- }) {
- __publicField(this, "interval");
- __publicField(this, "maxOut");
- __publicField(this, "cancelAfter");
- __publicField(this, "timer");
- __publicField(this, "autoCancelTimer");
- __publicField(this, "last");
- __publicField(this, "missed");
- __publicField(this, "count");
- __publicField(this, "callback");
- this.interval = interval;
- this.maxOut = (opts == null ? void 0 : opts.maxOut) || 2;
- this.cancelAfter = (opts == null ? void 0 : opts.cancelAfter) || 0;
- this.last = Date.now();
- this.missed = 0;
- this.count = 0;
- this.callback = cb;
- this._schedule();
- }
- cancel() {
- if (this.autoCancelTimer) {
- clearTimeout(this.autoCancelTimer);
- }
- if (this.timer) {
- clearInterval(this.timer);
- }
- this.timer = 0;
- this.autoCancelTimer = 0;
- }
- work() {
- this.last = Date.now();
- this.missed = 0;
- }
- _change(interval, cancelAfter = 0, maxOut = 2) {
- this.interval = interval;
- this.maxOut = maxOut;
- this.cancelAfter = cancelAfter;
- this.restart();
- }
- restart() {
- this.cancel();
- this._schedule();
- }
- _schedule() {
- if (this.cancelAfter > 0) {
- this.autoCancelTimer = setTimeout(() => {
- this.cancel();
- }, this.cancelAfter);
- }
- this.timer = setInterval(() => {
- this.count++;
- if (Date.now() - this.last > this.interval) {
- this.missed++;
- }
- if (this.missed >= this.maxOut) {
- try {
- if (this.callback(this.missed) === true) {
- this.cancel();
- }
- } catch (err) {
- console.log(err);
- }
- }
- }, this.interval);
- }
- };
- var PullConsumerType;
- (function(PullConsumerType2) {
- PullConsumerType2[PullConsumerType2["Unset"] = -1] = "Unset";
- PullConsumerType2[PullConsumerType2["Consume"] = 0] = "Consume";
- PullConsumerType2[PullConsumerType2["Fetch"] = 1] = "Fetch";
- })(PullConsumerType || (PullConsumerType = {}));
- var ConsumerEvents;
- (function(ConsumerEvents2) {
- ConsumerEvents2["HeartbeatsMissed"] = "heartbeats_missed";
- ConsumerEvents2["ConsumerNotFound"] = "consumer_not_found";
- ConsumerEvents2["OrderedConsumerRecreated"] = "ordered_consumer_recreated";
- })(ConsumerEvents || (ConsumerEvents = {}));
- var ConsumerDebugEvents;
- (function(ConsumerDebugEvents2) {
- ConsumerDebugEvents2["DebugEvent"] = "debug";
- ConsumerDebugEvents2["Discard"] = "discard";
- ConsumerDebugEvents2["Next"] = "next";
- })(ConsumerDebugEvents || (ConsumerDebugEvents = {}));
- var PullConsumerMessagesImpl = class extends QueuedIteratorImpl {
- constructor(c, opts, refilling = false) {
- super();
- __publicField(this, "consumer");
- __publicField(this, "opts");
- __publicField(this, "sub");
- __publicField(this, "monitor");
- __publicField(this, "pending");
- __publicField(this, "inbox");
- __publicField(this, "refilling");
- __publicField(this, "stack");
- __publicField(this, "pong");
- __publicField(this, "callback");
- __publicField(this, "timeout");
- __publicField(this, "cleanupHandler");
- __publicField(this, "listeners");
- __publicField(this, "statusIterator");
- __publicField(this, "forOrderedConsumer");
- __publicField(this, "resetHandler");
- this.consumer = c;
- this.opts = this.parseOptions(opts, refilling);
- this.callback = opts.callback || null;
- this.noIterator = typeof this.callback === "function";
- this.monitor = null;
- this.pong = null;
- this.pending = {
- msgs: 0,
- bytes: 0,
- requests: 0
- };
- this.refilling = refilling;
- this.stack = new Error().stack.split("\n").slice(1).join("\n");
- this.timeout = null;
- this.inbox = createInbox(c.api.nc.options.inboxPrefix);
- this.listeners = [];
- this.forOrderedConsumer = false;
- this.start();
- }
- start() {
- const { max_messages, max_bytes, idle_heartbeat, threshold_bytes, threshold_messages } = this.opts;
- this.closed().then(() => {
- if (this.cleanupHandler) {
- try {
- this.cleanupHandler();
- } catch (_err) {
- }
- }
- });
- const { sub } = this;
- if (sub) {
- sub.unsubscribe();
- }
- this.sub = this.consumer.api.nc.subscribe(this.inbox, {
- callback: (err, msg) => {
- var _a, _b, _c, _d;
- if (err) {
- this.stop();
- return;
- }
- (_a = this.monitor) == null ? void 0 : _a.work();
- const isProtocol = msg.subject === this.inbox;
- if (isProtocol) {
- if (isHeartbeatMsg(msg)) {
- return;
- }
- const code2 = (_b = msg.headers) == null ? void 0 : _b.code;
- const description = ((_d = (_c = msg.headers) == null ? void 0 : _c.description) == null ? void 0 : _d.toLowerCase()) || "unknown";
- const { msgsLeft, bytesLeft } = this.parseDiscard(msg.headers);
- if (msgsLeft > 0 || bytesLeft > 0) {
- this.pending.msgs -= msgsLeft;
- this.pending.bytes -= bytesLeft;
- this.pending.requests--;
- this.notify(ConsumerDebugEvents.Discard, {
- msgsLeft,
- bytesLeft
- });
- } else {
- const toErr = () => {
- const err2 = new NatsError(description, `${code2}`);
- err2.stack += `
- ${this.stack}`;
- return err2;
- };
- if (code2 === 400) {
- const error = toErr();
- this._push(() => {
- this.stop(error);
- });
- } else if (code2 === 409 && description === "consumer deleted") {
- const error = toErr();
- this.stop(error);
- } else {
- this.notify(ConsumerDebugEvents.DebugEvent, `${code2} ${description}`);
- }
- }
- } else {
- this._push(toJsMsg(msg));
- this.received++;
- if (this.pending.msgs) {
- this.pending.msgs--;
- }
- if (this.pending.bytes) {
- this.pending.bytes -= msg.size();
- }
- }
- if (this.pending.msgs === 0 && this.pending.bytes === 0) {
- this.pending.requests = 0;
- }
- if (this.refilling) {
- if (max_messages && this.pending.msgs <= threshold_messages || max_bytes && this.pending.bytes <= threshold_bytes) {
- const batch = this.pullOptions();
- this.pull(batch);
- }
- } else if (this.pending.requests === 0) {
- this._push(() => {
- this.stop();
- });
- }
- }
- });
- this.sub.closed.then(() => {
- if (this.sub.draining) {
- this._push(() => {
- this.stop();
- });
- }
- });
- if (idle_heartbeat) {
- this.monitor = new IdleHeartbeatMonitor(idle_heartbeat, (data) => {
- this.notify(ConsumerEvents.HeartbeatsMissed, data);
- this.resetPending().then(() => {
- }).catch(() => {
- });
- return false;
- }, {
- maxOut: 2
- });
- }
- (async () => {
- var _a;
- const status = this.consumer.api.nc.status();
- this.statusIterator = status;
- for await (const s of status) {
- switch (s.type) {
- case Events.Disconnect:
- (_a = this.monitor) == null ? void 0 : _a.cancel();
- break;
- case Events.Reconnect:
- this.resetPending().then((ok) => {
- var _a2;
- if (ok) {
- (_a2 = this.monitor) == null ? void 0 : _a2.restart();
- }
- }).catch(() => {
- });
- break;
- default:
- }
- }
- })();
- this.pull(this.pullOptions());
- }
- _push(r) {
- if (!this.callback) {
- super.push(r);
- } else {
- const fn = typeof r === "function" ? r : null;
- try {
- if (!fn) {
- this.callback(r);
- } else {
- fn();
- }
- } catch (err) {
- this.stop(err);
- }
- }
- }
- notify(type, data) {
- if (this.listeners.length > 0) {
- (() => {
- this.listeners.forEach((l) => {
- if (!l.done) {
- l.push({
- type,
- data
- });
- }
- });
- })();
- }
- }
- async resetPending() {
- let notFound = 0;
- const bo = backoff();
- let attempt = 0;
- while (true) {
- if (this.consumer.api.nc.isClosed()) {
- console.error("aborting resetPending - connection is closed");
- return false;
- }
- try {
- await this.consumer.info();
- notFound = 0;
- this.pending.msgs = 0;
- this.pending.bytes = 0;
- this.pending.requests = 0;
- this.pull(this.pullOptions());
- return true;
- } catch (err) {
- if (err.message === "consumer not found") {
- notFound++;
- this.notify(ConsumerEvents.ConsumerNotFound, notFound);
- if (this.resetHandler) {
- try {
- this.resetHandler();
- } catch (_) {
- }
- }
- if (this.forOrderedConsumer) {
- return false;
- }
- } else {
- notFound = 0;
- }
- const to = bo.backoff(attempt);
- await Promise.race([
- delay(to),
- this.consumer.api.nc.closed()
- ]);
- attempt++;
- }
- }
- }
- pull(opts) {
- this.pending.bytes += opts.max_bytes ?? 0;
- this.pending.msgs += opts.batch ?? 0;
- this.pending.requests++;
- const nc = this.consumer.api.nc;
- this._push(() => {
- nc.publish(`${this.consumer.api.prefix}.CONSUMER.MSG.NEXT.${this.consumer.stream}.${this.consumer.name}`, this.consumer.api.jc.encode(opts), {
- reply: this.inbox
- });
- this.notify(ConsumerDebugEvents.Next, opts);
- });
- }
- pullOptions() {
- const batch = this.opts.max_messages - this.pending.msgs;
- const max_bytes = this.opts.max_bytes - this.pending.bytes;
- const idle_heartbeat = nanos(this.opts.idle_heartbeat);
- const expires = nanos(this.opts.expires);
- return {
- batch,
- max_bytes,
- idle_heartbeat,
- expires
- };
- }
- parseDiscard(headers2) {
- const discard = {
- msgsLeft: 0,
- bytesLeft: 0
- };
- const msgsLeft = headers2 == null ? void 0 : headers2.get(JsHeaders.PendingMessagesHdr);
- if (msgsLeft) {
- discard.msgsLeft = parseInt(msgsLeft);
- }
- const bytesLeft = headers2 == null ? void 0 : headers2.get(JsHeaders.PendingBytesHdr);
- if (bytesLeft) {
- discard.bytesLeft = parseInt(bytesLeft);
- }
- return discard;
- }
- trackTimeout(t) {
- this.timeout = t;
- }
- close() {
- this.stop();
- return this.iterClosed;
- }
- closed() {
- return this.iterClosed;
- }
- clearTimers() {
- var _a, _b;
- (_a = this.monitor) == null ? void 0 : _a.cancel();
- this.monitor = null;
- (_b = this.timeout) == null ? void 0 : _b.cancel();
- this.timeout = null;
- }
- setCleanupHandler(fn) {
- this.cleanupHandler = fn;
- }
- stop(err) {
- var _a, _b;
- (_a = this.sub) == null ? void 0 : _a.unsubscribe();
- this.clearTimers();
- (_b = this.statusIterator) == null ? void 0 : _b.stop();
- this._push(() => {
- super.stop(err);
- this.listeners.forEach((n) => {
- n.stop();
- });
- });
- }
- parseOptions(opts, refilling = false) {
- const args = opts || {};
- args.max_messages = args.max_messages || 0;
- args.max_bytes = args.max_bytes || 0;
- if (args.max_messages !== 0 && args.max_bytes !== 0) {
- throw new Error(`only specify one of max_messages or max_bytes`);
- }
- if (args.max_messages === 0) {
- args.max_messages = 100;
- }
- args.expires = args.expires || 3e4;
- if (args.expires < 1e3) {
- throw new Error("expires should be at least 1000ms");
- }
- args.idle_heartbeat = args.idle_heartbeat || args.expires / 2;
- args.idle_heartbeat = args.idle_heartbeat > 3e4 ? 3e4 : args.idle_heartbeat;
- if (refilling) {
- const minMsgs = Math.round(args.max_messages * 0.75) || 1;
- args.threshold_messages = args.threshold_messages || minMsgs;
- const minBytes = Math.round(args.max_bytes * 0.75) || 1;
- args.threshold_bytes = args.threshold_bytes || minBytes;
- }
- return args;
- }
- status() {
- const iter = new QueuedIteratorImpl();
- this.listeners.push(iter);
- return Promise.resolve(iter);
- }
- };
- var OrderedConsumerMessages = class extends QueuedIteratorImpl {
- constructor() {
- super();
- __publicField(this, "src");
- __publicField(this, "listeners");
- this.listeners = [];
- }
- setSource(src) {
- if (this.src) {
- this.src.resetHandler = void 0;
- this.src.setCleanupHandler();
- this.src.stop();
- }
- this.src = src;
- this.src.setCleanupHandler(() => {
- this.close().catch();
- });
- (async () => {
- const status = await this.src.status();
- for await (const s of status) {
- this.notify(s.type, s.data);
- }
- })().catch(() => {
- });
- }
- notify(type, data) {
- if (this.listeners.length > 0) {
- (() => {
- this.listeners.forEach((l) => {
- if (!l.done) {
- l.push({
- type,
- data
- });
- }
- });
- })();
- }
- }
- stop(err) {
- var _a;
- (_a = this.src) == null ? void 0 : _a.stop(err);
- super.stop(err);
- this.listeners.forEach((n) => {
- n.stop();
- });
- }
- close() {
- this.stop();
- return this.iterClosed;
- }
- status() {
- const iter = new QueuedIteratorImpl();
- this.listeners.push(iter);
- return Promise.resolve(iter);
- }
- };
- var PullConsumerImpl = class {
- constructor(api, info) {
- __publicField(this, "api");
- __publicField(this, "_info");
- __publicField(this, "stream");
- __publicField(this, "name");
- this.api = api;
- this._info = info;
- this.stream = info.stream_name;
- this.name = info.name;
- }
- consume(opts = {
- max_messages: 100,
- expires: 3e4
- }) {
- return Promise.resolve(new PullConsumerMessagesImpl(this, opts, true));
- }
- fetch(opts = {
- max_messages: 100,
- expires: 3e4
- }) {
- const m = new PullConsumerMessagesImpl(this, opts, false);
- const to = Math.round(m.opts.expires * 1.05);
- const timer = timeout(to);
- m.closed().then(() => {
- timer.cancel();
- });
- timer.catch(() => {
- m.close().catch();
- });
- m.trackTimeout(timer);
- return Promise.resolve(m);
- }
- next(opts = {
- expires: 3e4
- }) {
- const d = deferred();
- const fopts = opts;
- fopts.max_messages = 1;
- const iter = new PullConsumerMessagesImpl(this, fopts, false);
- const to = Math.round(iter.opts.expires * 1.05);
- if (to >= 6e4) {
- (async () => {
- for await (const s of await iter.status()) {
- if (s.type === ConsumerEvents.HeartbeatsMissed && s.data >= 2) {
- d.reject(new Error("consumer missed heartbeats"));
- break;
- }
- }
- })().catch();
- }
- (async () => {
- for await (const m of iter) {
- d.resolve(m);
- break;
- }
- })().catch();
- const timer = timeout(to);
- iter.closed().then(() => {
- d.resolve(null);
- timer.cancel();
- }).catch((err) => {
- d.reject(err);
- });
- timer.catch((_err) => {
- d.resolve(null);
- iter.close().catch();
- });
- iter.trackTimeout(timer);
- return d;
- }
- delete() {
- const { stream_name, name } = this._info;
- return this.api.delete(stream_name, name);
- }
- info(cached = false) {
- if (cached) {
- return Promise.resolve(this._info);
- }
- const { stream_name, name } = this._info;
- return this.api.info(stream_name, name).then((ci) => {
- this._info = ci;
- return this._info;
- });
- }
- };
- var OrderedPullConsumerImpl = class {
- constructor(api, stream, opts = {}) {
- __publicField(this, "api");
- __publicField(this, "consumerOpts");
- __publicField(this, "consumer");
- __publicField(this, "opts");
- __publicField(this, "cursor");
- __publicField(this, "stream");
- __publicField(this, "namePrefix");
- __publicField(this, "serial");
- __publicField(this, "currentConsumer");
- __publicField(this, "userCallback");
- __publicField(this, "iter");
- __publicField(this, "type");
- __publicField(this, "startSeq");
- this.api = api;
- this.stream = stream;
- this.cursor = {
- stream_seq: 1,
- deliver_seq: 0
- };
- this.namePrefix = nuid.next();
- this.serial = 0;
- this.currentConsumer = null;
- this.userCallback = null;
- this.iter = null;
- this.type = PullConsumerType.Unset;
- this.consumerOpts = opts;
- this.startSeq = this.consumerOpts.opt_start_seq || 0;
- this.cursor.stream_seq = this.startSeq > 0 ? this.startSeq - 1 : 0;
- }
- getConsumerOpts(seq) {
- this.serial++;
- const name = `${this.namePrefix}_${this.serial}`;
- seq = seq === 0 ? 1 : seq;
- const config = {
- name,
- deliver_policy: DeliverPolicy.StartSequence,
- opt_start_seq: seq,
- ack_policy: AckPolicy.None,
- inactive_threshold: nanos(5 * 60 * 1e3),
- num_replicas: 1
- };
- if (this.consumerOpts.headers_only === true) {
- config.headers_only = true;
- }
- if (Array.isArray(this.consumerOpts.filterSubjects)) {
- config.filter_subjects = this.consumerOpts.filterSubjects;
- }
- if (typeof this.consumerOpts.filterSubjects === "string") {
- config.filter_subject = this.consumerOpts.filterSubjects;
- }
- if (seq === this.startSeq + 1) {
- config.deliver_policy = this.consumerOpts.deliver_policy || DeliverPolicy.StartSequence;
- if (this.consumerOpts.deliver_policy === DeliverPolicy.LastPerSubject || this.consumerOpts.deliver_policy === DeliverPolicy.New || this.consumerOpts.deliver_policy === DeliverPolicy.Last) {
- delete config.opt_start_seq;
- config.deliver_policy = this.consumerOpts.deliver_policy;
- }
- if (config.deliver_policy === DeliverPolicy.LastPerSubject) {
- if (typeof config.filter_subjects === "undefined" && typeof config.filter_subject === "undefined") {
- config.filter_subject = ">";
- }
- }
- if (this.consumerOpts.opt_start_time) {
- delete config.opt_start_seq;
- config.deliver_policy = DeliverPolicy.StartTime;
- config.opt_start_time = this.consumerOpts.opt_start_time;
- }
- if (this.consumerOpts.inactive_threshold) {
- config.inactive_threshold = nanos(this.consumerOpts.inactive_threshold);
- }
- }
- return config;
- }
- async resetConsumer(seq = 0) {
- var _a, _b;
- (_a = this.consumer) == null ? void 0 : _a.delete().catch(() => {
- });
- seq = seq === 0 ? 1 : seq;
- this.cursor.deliver_seq = 0;
- const config = this.getConsumerOpts(seq);
- config.max_deliver = 1;
- config.mem_storage = true;
- const bo = backoff();
- let ci;
- for (let i = 0; ; i++) {
- try {
- ci = await this.api.add(this.stream, config);
- (_b = this.iter) == null ? void 0 : _b.notify(ConsumerEvents.OrderedConsumerRecreated, ci.name);
- break;
- } catch (err) {
- if (seq === 0 && i >= 30) {
- throw err;
- } else {
- await delay(bo.backoff(i + 1));
- }
- }
- }
- return ci;
- }
- internalHandler(serial) {
- return (m) => {
- var _a;
- if (this.serial !== serial) {
- return;
- }
- const dseq = m.info.deliverySequence;
- if (dseq !== this.cursor.deliver_seq + 1) {
- this.reset(this.opts);
- return;
- }
- this.cursor.deliver_seq = dseq;
- this.cursor.stream_seq = m.info.streamSequence;
- if (this.userCallback) {
- this.userCallback(m);
- } else {
- (_a = this.iter) == null ? void 0 : _a.push(m);
- }
- };
- }
- async reset(opts = {
- max_messages: 100,
- expires: 3e4
- }, fromFetch = false) {
- this.currentConsumer = await this.resetConsumer(this.cursor.stream_seq + 1);
- if (this.iter === null) {
- this.iter = new OrderedConsumerMessages();
- }
- this.consumer = new PullConsumerImpl(this.api, this.currentConsumer);
- const copts = opts;
- copts.callback = this.internalHandler(this.serial);
- let msgs = null;
- if (this.type === PullConsumerType.Fetch && fromFetch) {
- msgs = await this.consumer.fetch(opts);
- } else if (this.type === PullConsumerType.Consume) {
- msgs = await this.consumer.consume(opts);
- } else {
- return Promise.reject("reset called with unset consumer type");
- }
- const msgsImpl = msgs;
- msgsImpl.forOrderedConsumer = true;
- msgsImpl.resetHandler = () => {
- this.reset(this.opts);
- };
- this.iter.setSource(msgsImpl);
- return this.iter;
- }
- consume(opts = {
- max_messages: 100,
- expires: 3e4
- }) {
- if (this.type === PullConsumerType.Fetch) {
- return Promise.reject(new Error("ordered consumer initialized as fetch"));
- }
- if (this.type === PullConsumerType.Consume) {
- return Promise.reject(new Error("ordered consumer doesn't support concurrent consume"));
- }
- const { callback } = opts;
- if (callback) {
- this.userCallback = callback;
- }
- this.type = PullConsumerType.Consume;
- this.opts = opts;
- return this.reset(opts);
- }
- fetch(opts = {
- max_messages: 100,
- expires: 3e4
- }) {
- var _a;
- if (this.type === PullConsumerType.Consume) {
- return Promise.reject(new Error("ordered consumer already initialized as consume"));
- }
- if (((_a = this.iter) == null ? void 0 : _a.done) === false) {
- return Promise.reject(new Error("ordered consumer doesn't support concurrent fetch"));
- }
- const { callback } = opts;
- if (callback) {
- this.userCallback = callback;
- }
- this.type = PullConsumerType.Fetch;
- this.opts = opts;
- this.iter = new OrderedConsumerMessages();
- return this.reset(opts, true);
- }
- async next(opts = {
- expires: 3e4
- }) {
- const d = deferred();
- const copts = opts;
- copts.max_messages = 1;
- copts.callback = (m) => {
- this.userCallback = null;
- d.resolve(m);
- };
- const iter = await this.fetch(copts);
- iter.iterClosed.then(() => {
- d.resolve(null);
- }).catch((err) => {
- d.reject(err);
- });
- return d;
- }
- delete() {
- if (!this.currentConsumer) {
- return Promise.resolve(false);
- }
- return this.api.delete(this.stream, this.currentConsumer.name).then((tf) => {
- return Promise.resolve(tf);
- }).catch((err) => {
- return Promise.reject(err);
- }).finally(() => {
- this.currentConsumer = null;
- });
- }
- async info(cached) {
- if (this.currentConsumer == null) {
- this.currentConsumer = await this.resetConsumer(this.serial);
- return Promise.resolve(this.currentConsumer);
- }
- if (cached && this.currentConsumer) {
- return Promise.resolve(this.currentConsumer);
- }
- return this.api.info(this.stream, this.currentConsumer.name);
- }
- };
- function convertStreamSourceDomain(s) {
- if (s === void 0) {
- return void 0;
- }
- const { domain } = s;
- if (domain === void 0) {
- return s;
- }
- const copy2 = Object.assign({}, s);
- delete copy2.domain;
- if (domain === "") {
- return copy2;
- }
- if (copy2.external) {
- throw new Error("domain and external are both set");
- }
- copy2.external = {
- api: `$JS.${domain}.API`
- };
- return copy2;
- }
- var PubHeaders;
- var osPrefix = "OBJ_";
- var ConsumersImpl = class {
- constructor(api) {
- __publicField(this, "api");
- __publicField(this, "notified");
- this.api = api;
- this.notified = false;
- }
- checkVersion() {
- const fv = this.api.nc.features.get(Feature.JS_SIMPLIFICATION);
- if (!fv.ok) {
- return Promise.reject(new Error(`consumers framework is only supported on servers ${fv.min} or better`));
- }
- return Promise.resolve();
- }
- async get(stream, name = {}) {
- if (typeof name === "object") {
- return this.ordered(stream, name);
- }
- await this.checkVersion();
- return this.api.info(stream, name).then((ci) => {
- if (ci.config.deliver_subject !== void 0) {
- return Promise.reject(new Error("push consumer not supported"));
- }
- return new PullConsumerImpl(this.api, ci);
- }).catch((err) => {
- return Promise.reject(err);
- });
- }
- async ordered(stream, opts) {
- await this.checkVersion();
- const impl = this.api;
- const sapi = new StreamAPIImpl(impl.nc, impl.opts);
- return sapi.info(stream).then((_si) => {
- return Promise.resolve(new OrderedPullConsumerImpl(this.api, stream, opts));
- }).catch((err) => {
- return Promise.reject(err);
- });
- }
- };
- var StreamImpl = class {
- constructor(api, info) {
- __publicField(this, "api");
- __publicField(this, "_info");
- this.api = api;
- this._info = info;
- }
- get name() {
- return this._info.config.name;
- }
- alternates() {
- return this.info().then((si) => {
- return si.alternates ? si.alternates : [];
- });
- }
- async best() {
- await this.info();
- if (this._info.alternates) {
- const asi = await this.api.info(this._info.alternates[0].name);
- return new StreamImpl(this.api, asi);
- } else {
- return this;
- }
- }
- info(cached = false, opts) {
- if (cached) {
- return Promise.resolve(this._info);
- }
- return this.api.info(this.name, opts).then((si) => {
- this._info = si;
- return this._info;
- });
- }
- getConsumer(name) {
- return new ConsumersImpl(new ConsumerAPIImpl(this.api.nc, this.api.opts)).get(this.name, name);
- }
- getMessage(query) {
- return this.api.getMessage(this.name, query);
- }
- deleteMessage(seq, erase) {
- return this.api.deleteMessage(this.name, seq, erase);
- }
- };
- function NoopKvCodecs() {
- return {
- key: {
- encode(k) {
- return k;
- },
- decode(k) {
- return k;
- }
- },
- value: {
- encode(v) {
- return v;
- },
- decode(v) {
- return v;
- }
- }
- };
- }
- function defaultBucketOpts() {
- return {
- replicas: 1,
- history: 1,
- timeout: 2e3,
- maxBucketSize: -1,
- maxValueSize: -1,
- codec: NoopKvCodecs(),
- storage: StorageType.File
- };
- }
- var kvOperationHdr = "KV-Operation";
- var kvSubjectPrefix = "$KV";
- var validKeyRe = /^[-/=.\w]+$/;
- var validSearchKey = /^[-/=.>*\w]+$/;
- var validBucketRe = /^[-\w]+$/;
- function validateKey(k) {
- if (k.startsWith(".") || k.endsWith(".") || !validKeyRe.test(k)) {
- throw new Error(`invalid key: ${k}`);
- }
- }
- function validateSearchKey(k) {
- if (k.startsWith(".") || k.endsWith(".") || !validSearchKey.test(k)) {
- throw new Error(`invalid key: ${k}`);
- }
- }
- function hasWildcards(k) {
- if (k.startsWith(".") || k.endsWith(".")) {
- throw new Error(`invalid key: ${k}`);
- }
- const chunks = k.split(".");
- let hasWildcards2 = false;
- for (let i = 0; i < chunks.length; i++) {
- switch (chunks[i]) {
- case "*":
- hasWildcards2 = true;
- break;
- case ">":
- if (i !== chunks.length - 1) {
- throw new Error(`invalid key: ${k}`);
- }
- hasWildcards2 = true;
- break;
- default:
- }
- }
- return hasWildcards2;
- }
- function validateBucket(name) {
- if (!validBucketRe.test(name)) {
- throw new Error(`invalid bucket name: ${name}`);
- }
- }
- (function(PubHeaders2) {
- PubHeaders2["MsgIdHdr"] = "Nats-Msg-Id";
- PubHeaders2["ExpectedStreamHdr"] = "Nats-Expected-Stream";
- PubHeaders2["ExpectedLastSeqHdr"] = "Nats-Expected-Last-Sequence";
- PubHeaders2["ExpectedLastMsgIdHdr"] = "Nats-Expected-Last-Msg-Id";
- PubHeaders2["ExpectedLastSubjectSequenceHdr"] = "Nats-Expected-Last-Subject-Sequence";
- })(PubHeaders || (PubHeaders = {}));
- var Bucket = class {
- constructor(bucket, js, jsm) {
- __publicField(this, "js");
- __publicField(this, "jsm");
- __publicField(this, "stream");
- __publicField(this, "bucket");
- __publicField(this, "direct");
- __publicField(this, "codec");
- __publicField(this, "prefix");
- __publicField(this, "editPrefix");
- __publicField(this, "useJsPrefix");
- __publicField(this, "_prefixLen");
- __publicField(this, "validateKey", validateKey);
- __publicField(this, "validateSearchKey", validateSearchKey);
- __publicField(this, "hasWildcards", hasWildcards);
- validateBucket(bucket);
- this.js = js;
- this.jsm = jsm;
- this.bucket = bucket;
- this.prefix = kvSubjectPrefix;
- this.editPrefix = "";
- this.useJsPrefix = false;
- this._prefixLen = 0;
- }
- static async create(js, name, opts = {}) {
- validateBucket(name);
- const jsm = await js.jetstreamManager();
- const bucket = new Bucket(name, js, jsm);
- await bucket.init(opts);
- return bucket;
- }
- static async bind(js, name, opts = {}) {
- const jsm = await js.jetstreamManager();
- const info = await jsm.streams.info(`${kvPrefix}${name}`);
- validateBucket(info.config.name);
- const bucket = new Bucket(name, js, jsm);
- Object.assign(bucket, info);
- bucket.codec = opts.codec || NoopKvCodecs();
- bucket.direct = info.config.allow_direct ?? false;
- bucket.initializePrefixes(info);
- return bucket;
- }
- async init(opts = {}) {
- const bo = Object.assign(defaultBucketOpts(), opts);
- this.codec = bo.codec;
- const sc = {};
- this.stream = sc.name = opts.streamName ?? this.bucketName();
- sc.retention = RetentionPolicy.Limits;
- sc.max_msgs_per_subject = bo.history;
- if (bo.maxBucketSize) {
- bo.max_bytes = bo.maxBucketSize;
- }
- if (bo.max_bytes) {
- sc.max_bytes = bo.max_bytes;
- }
- sc.max_msg_size = bo.maxValueSize;
- sc.storage = bo.storage;
- const location = opts.placementCluster ?? "";
- if (location) {
- opts.placement = {};
- opts.placement.cluster = location;
- opts.placement.tags = [];
- }
- if (opts.placement) {
- sc.placement = opts.placement;
- }
- if (opts.republish) {
- sc.republish = opts.republish;
- }
- if (opts.description) {
- sc.description = opts.description;
- }
- if (opts.mirror) {
- const mirror = Object.assign({}, opts.mirror);
- if (!mirror.name.startsWith(kvPrefix)) {
- mirror.name = `${kvPrefix}${mirror.name}`;
- }
- sc.mirror = mirror;
- sc.mirror_direct = true;
- } else if (opts.sources) {
- const sources = opts.sources.map((s) => {
- const c = Object.assign({}, s);
- if (!c.name.startsWith(kvPrefix)) {
- c.name = `${kvPrefix}${c.name}`;
- }
- });
- sc.sources = sources;
- } else {
- sc.subjects = [
- this.subjectForBucket()
- ];
- }
- if (opts.metadata) {
- sc.metadata = opts.metadata;
- }
- if (typeof opts.compression === "boolean") {
- sc.compression = opts.compression ? StoreCompression.S2 : StoreCompression.None;
- }
- const nci = this.js.nc;
- const have = nci.getServerVersion();
- const discardNew = have ? compare(have, parseSemVer("2.7.2")) >= 0 : false;
- sc.discard = discardNew ? DiscardPolicy.New : DiscardPolicy.Old;
- const { ok: direct, min } = nci.features.get(Feature.JS_ALLOW_DIRECT);
- if (!direct && opts.allow_direct === true) {
- const v = have ? `${have.major}.${have.minor}.${have.micro}` : "unknown";
- return Promise.reject(new Error(`allow_direct is not available on server version ${v} - requires ${min}`));
- }
- opts.allow_direct = typeof opts.allow_direct === "boolean" ? opts.allow_direct : direct;
- sc.allow_direct = opts.allow_direct;
- this.direct = sc.allow_direct;
- sc.num_replicas = bo.replicas;
- if (bo.ttl) {
- sc.max_age = nanos(bo.ttl);
- }
- sc.allow_rollup_hdrs = true;
- let info;
- try {
- info = await this.jsm.streams.info(sc.name);
- if (!info.config.allow_direct && this.direct === true) {
- this.direct = false;
- }
- } catch (err) {
- if (err.message === "stream not found") {
- info = await this.jsm.streams.add(sc);
- } else {
- throw err;
- }
- }
- this.initializePrefixes(info);
- }
- initializePrefixes(info) {
- this._prefixLen = 0;
- this.prefix = `$KV.${this.bucket}`;
- this.useJsPrefix = this.js.apiPrefix !== "$JS.API";
- const { mirror } = info.config;
- if (mirror) {
- let n = mirror.name;
- if (n.startsWith(kvPrefix)) {
- n = n.substring(kvPrefix.length);
- }
- if (mirror.external && mirror.external.api !== "") {
- const mb = mirror.name.substring(kvPrefix.length);
- this.useJsPrefix = false;
- this.prefix = `$KV.${mb}`;
- this.editPrefix = `${mirror.external.api}.$KV.${n}`;
- } else {
- this.editPrefix = this.prefix;
- }
- }
- }
- bucketName() {
- return this.stream ?? `${kvPrefix}${this.bucket}`;
- }
- subjectForBucket() {
- return `${this.prefix}.${this.bucket}.>`;
- }
- subjectForKey(k, edit = false) {
- const builder = [];
- if (edit) {
- if (this.useJsPrefix) {
- builder.push(this.js.apiPrefix);
- }
- if (this.editPrefix !== "") {
- builder.push(this.editPrefix);
- } else {
- builder.push(this.prefix);
- }
- } else {
- if (this.prefix) {
- builder.push(this.prefix);
- }
- }
- builder.push(k);
- return builder.join(".");
- }
- fullKeyName(k) {
- if (this.prefix !== "") {
- return `${this.prefix}.${k}`;
- }
- return `${kvSubjectPrefix}.${this.bucket}.${k}`;
- }
- get prefixLen() {
- if (this._prefixLen === 0) {
- this._prefixLen = this.prefix.length + 1;
- }
- return this._prefixLen;
- }
- encodeKey(key) {
- const chunks = [];
- for (const t of key.split(".")) {
- switch (t) {
- case ">":
- case "*":
- chunks.push(t);
- break;
- default:
- chunks.push(this.codec.key.encode(t));
- break;
- }
- }
- return chunks.join(".");
- }
- decodeKey(ekey) {
- const chunks = [];
- for (const t of ekey.split(".")) {
- switch (t) {
- case ">":
- case "*":
- chunks.push(t);
- break;
- default:
- chunks.push(this.codec.key.decode(t));
- break;
- }
- }
- return chunks.join(".");
- }
- close() {
- return Promise.resolve();
- }
- dataLen(data, h) {
- const slen = h ? h.get(JsHeaders.MessageSizeHdr) || "" : "";
- if (slen !== "") {
- return parseInt(slen, 10);
- }
- return data.length;
- }
- smToEntry(sm) {
- return new KvStoredEntryImpl(this.bucket, this.prefixLen, sm);
- }
- jmToEntry(jm) {
- const key = this.decodeKey(jm.subject.substring(this.prefixLen));
- return new KvJsMsgEntryImpl(this.bucket, key, jm);
- }
- async create(k, data) {
- var _a;
- let firstErr;
- try {
- const n = await this.put(k, data, {
- previousSeq: 0
- });
- return Promise.resolve(n);
- } catch (err) {
- firstErr = err;
- if (((_a = err == null ? void 0 : err.api_error) == null ? void 0 : _a.err_code) !== 10071) {
- return Promise.reject(err);
- }
- }
- let rev = 0;
- try {
- const e = await this.get(k);
- if ((e == null ? void 0 : e.operation) === "DEL" || (e == null ? void 0 : e.operation) === "PURGE") {
- rev = e !== null ? e.revision : 0;
- return this.update(k, data, rev);
- } else {
- return Promise.reject(firstErr);
- }
- } catch (err) {
- return Promise.reject(err);
- }
- }
- update(k, data, version) {
- if (version <= 0) {
- throw new Error("version must be greater than 0");
- }
- return this.put(k, data, {
- previousSeq: version
- });
- }
- async put(k, data, opts = {}) {
- var _a, _b;
- const ek = this.encodeKey(k);
- this.validateKey(ek);
- const o = {};
- if (opts.previousSeq !== void 0) {
- const h = headers();
- o.headers = h;
- h.set(PubHeaders.ExpectedLastSubjectSequenceHdr, `${opts.previousSeq}`);
- }
- try {
- const pa = await this.js.publish(this.subjectForKey(ek, true), data, o);
- return pa.seq;
- } catch (err) {
- const ne = err;
- if (ne.isJetStreamError()) {
- ne.message = (_a = ne.api_error) == null ? void 0 : _a.description;
- ne.code = `${(_b = ne.api_error) == null ? void 0 : _b.code}`;
- return Promise.reject(ne);
- }
- return Promise.reject(err);
- }
- }
- async get(k, opts) {
- const ek = this.encodeKey(k);
- this.validateKey(ek);
- let arg = {
- last_by_subj: this.subjectForKey(ek)
- };
- if (opts && opts.revision > 0) {
- arg = {
- seq: opts.revision
- };
- }
- let sm;
- try {
- if (this.direct) {
- const direct = this.jsm.direct;
- sm = await direct.getMessage(this.bucketName(), arg);
- } else {
- sm = await this.jsm.streams.getMessage(this.bucketName(), arg);
- }
- const ke = this.smToEntry(sm);
- if (ke.key !== ek) {
- return null;
- }
- return ke;
- } catch (err) {
- if (err.code === ErrorCode.JetStream404NoMessages) {
- return null;
- }
- throw err;
- }
- }
- purge(k) {
- return this._deleteOrPurge(k, "PURGE");
- }
- delete(k) {
- return this._deleteOrPurge(k, "DEL");
- }
- async purgeDeletes(olderMillis = 30 * 60 * 1e3) {
- const done = deferred();
- const buf = [];
- const i = await this.watch({
- key: ">",
- initializedFn: () => {
- done.resolve();
- }
- });
- (async () => {
- for await (const e of i) {
- if (e.operation === "DEL" || e.operation === "PURGE") {
- buf.push(e);
- }
- }
- })().then();
- await done;
- i.stop();
- const min = Date.now() - olderMillis;
- const proms = buf.map((e) => {
- const subj = this.subjectForKey(e.key);
- if (e.created.getTime() >= min) {
- return this.jsm.streams.purge(this.stream, {
- filter: subj,
- keep: 1
- });
- } else {
- return this.jsm.streams.purge(this.stream, {
- filter: subj,
- keep: 0
- });
- }
- });
- const purged = await Promise.all(proms);
- purged.unshift({
- success: true,
- purged: 0
- });
- return purged.reduce((pv, cv) => {
- pv.purged += cv.purged;
- return pv;
- });
- }
- async _deleteOrPurge(k, op) {
- if (!this.hasWildcards(k)) {
- return this._doDeleteOrPurge(k, op);
- }
- const iter = await this.keys(k);
- const buf = [];
- for await (const k2 of iter) {
- buf.push(this._doDeleteOrPurge(k2, op));
- if (buf.length === 100) {
- await Promise.all(buf);
- buf.length = 0;
- }
- }
- if (buf.length > 0) {
- await Promise.all(buf);
- }
- }
- async _doDeleteOrPurge(k, op) {
- const ek = this.encodeKey(k);
- this.validateKey(ek);
- const h = headers();
- h.set(kvOperationHdr, op);
- if (op === "PURGE") {
- h.set(JsHeaders.RollupHdr, JsHeaders.RollupValueSubject);
- }
- await this.js.publish(this.subjectForKey(ek, true), Empty, {
- headers: h
- });
- }
- _buildCC(k, content, opts = {}) {
- const ek = this.encodeKey(k);
- this.validateSearchKey(k);
- let deliver_policy = DeliverPolicy.LastPerSubject;
- if (content === KvWatchInclude.AllHistory) {
- deliver_policy = DeliverPolicy.All;
- }
- if (content === KvWatchInclude.UpdatesOnly) {
- deliver_policy = DeliverPolicy.New;
- }
- return Object.assign({
- deliver_policy,
- "ack_policy": AckPolicy.None,
- "filter_subject": this.fullKeyName(ek),
- "flow_control": true,
- "idle_heartbeat": nanos(5 * 1e3)
- }, opts);
- }
- remove(k) {
- return this.purge(k);
- }
- async history(opts = {}) {
- const k = opts.key ?? ">";
- const qi = new QueuedIteratorImpl();
- const co = {};
- co.headers_only = opts.headers_only || false;
- let fn;
- fn = () => {
- qi.stop();
- };
- let count = 0;
- const cc2 = this._buildCC(k, KvWatchInclude.AllHistory, co);
- const subj = cc2.filter_subject;
- const copts = consumerOpts(cc2);
- copts.bindStream(this.stream);
- copts.orderedConsumer();
- copts.callback((err, jm) => {
- if (err) {
- qi.stop(err);
- return;
- }
- if (jm) {
- const e = this.jmToEntry(jm);
- qi.push(e);
- qi.received++;
- if (fn && count > 0 && qi.received >= count || jm.info.pending === 0) {
- qi.push(fn);
- fn = void 0;
- }
- }
- });
- const sub = await this.js.subscribe(subj, copts);
- if (fn) {
- const { info: { last } } = sub;
- const expect = last.num_pending + last.delivered.consumer_seq;
- if (expect === 0 || qi.received >= expect) {
- try {
- fn();
- } catch (err) {
- qi.stop(err);
- } finally {
- fn = void 0;
- }
- } else {
- count = expect;
- }
- }
- qi._data = sub;
- qi.iterClosed.then(() => {
- sub.unsubscribe();
- });
- sub.closed.then(() => {
- qi.stop();
- }).catch((err) => {
- qi.stop(err);
- });
- return qi;
- }
- async watch(opts = {}) {
- const k = opts.key ?? ">";
- const qi = new QueuedIteratorImpl();
- const co = {};
- co.headers_only = opts.headers_only || false;
- let content = KvWatchInclude.LastValue;
- if (opts.include === KvWatchInclude.AllHistory) {
- content = KvWatchInclude.AllHistory;
- } else if (opts.include === KvWatchInclude.UpdatesOnly) {
- content = KvWatchInclude.UpdatesOnly;
- }
- const ignoreDeletes = opts.ignoreDeletes === true;
- let fn = opts.initializedFn;
- let count = 0;
- const cc2 = this._buildCC(k, content, co);
- const subj = cc2.filter_subject;
- const copts = consumerOpts(cc2);
- copts.bindStream(this.stream);
- copts.orderedConsumer();
- copts.callback((err, jm) => {
- if (err) {
- qi.stop(err);
- return;
- }
- if (jm) {
- const e = this.jmToEntry(jm);
- if (ignoreDeletes && e.operation === "DEL") {
- return;
- }
- qi.push(e);
- qi.received++;
- if (fn && (count > 0 && qi.received >= count || jm.info.pending === 0)) {
- qi.push(fn);
- fn = void 0;
- }
- }
- });
- const sub = await this.js.subscribe(subj, copts);
- if (fn) {
- const { info: { last } } = sub;
- const expect = last.num_pending + last.delivered.consumer_seq;
- if (expect === 0 || qi.received >= expect) {
- try {
- fn();
- } catch (err) {
- qi.stop(err);
- } finally {
- fn = void 0;
- }
- } else {
- count = expect;
- }
- }
- qi._data = sub;
- qi.iterClosed.then(() => {
- sub.unsubscribe();
- });
- sub.closed.then(() => {
- qi.stop();
- }).catch((err) => {
- qi.stop(err);
- });
- return qi;
- }
- async keys(k = ">") {
- const keys = new QueuedIteratorImpl();
- const cc2 = this._buildCC(k, KvWatchInclude.LastValue, {
- headers_only: true
- });
- const subj = cc2.filter_subject;
- const copts = consumerOpts(cc2);
- copts.bindStream(this.stream);
- copts.orderedConsumer();
- const sub = await this.js.subscribe(subj, copts);
- (async () => {
- var _a;
- for await (const jm of sub) {
- const op = (_a = jm.headers) == null ? void 0 : _a.get(kvOperationHdr);
- if (op !== "DEL" && op !== "PURGE") {
- const key = this.decodeKey(jm.subject.substring(this.prefixLen));
- keys.push(key);
- }
- if (jm.info.pending === 0) {
- sub.unsubscribe();
- }
- }
- })().then(() => {
- keys.stop();
- }).catch((err) => {
- keys.stop(err);
- });
- const si = sub;
- if (si.info.last.num_pending === 0) {
- sub.unsubscribe();
- }
- return keys;
- }
- purgeBucket(opts) {
- return this.jsm.streams.purge(this.bucketName(), opts);
- }
- destroy() {
- return this.jsm.streams.delete(this.bucketName());
- }
- async status() {
- var _a;
- const nc = this.js.nc;
- const cluster = ((_a = nc.info) == null ? void 0 : _a.cluster) ?? "";
- const bn = this.bucketName();
- const si = await this.jsm.streams.info(bn);
- return new KvStatusImpl(si, cluster);
- }
- };
- var KvStatusImpl = class {
- constructor(si, cluster = "") {
- __publicField(this, "si");
- __publicField(this, "cluster");
- this.si = si;
- this.cluster = cluster;
- }
- get bucket() {
- return this.si.config.name.startsWith(kvPrefix) ? this.si.config.name.substring(kvPrefix.length) : this.si.config.name;
- }
- get values() {
- return this.si.state.messages;
- }
- get history() {
- return this.si.config.max_msgs_per_subject;
- }
- get ttl() {
- return millis(this.si.config.max_age);
- }
- get bucket_location() {
- return this.cluster;
- }
- get backingStore() {
- return this.si.config.storage;
- }
- get storage() {
- return this.si.config.storage;
- }
- get replicas() {
- return this.si.config.num_replicas;
- }
- get description() {
- return this.si.config.description ?? "";
- }
- get maxBucketSize() {
- return this.si.config.max_bytes;
- }
- get maxValueSize() {
- return this.si.config.max_msg_size;
- }
- get max_bytes() {
- return this.si.config.max_bytes;
- }
- get placement() {
- return this.si.config.placement || {
- cluster: "",
- tags: []
- };
- }
- get placementCluster() {
- var _a;
- return ((_a = this.si.config.placement) == null ? void 0 : _a.cluster) ?? "";
- }
- get republish() {
- return this.si.config.republish ?? {
- src: "",
- dest: ""
- };
- }
- get streamInfo() {
- return this.si;
- }
- get size() {
- return this.si.state.bytes;
- }
- get metadata() {
- return this.si.config.metadata ?? {};
- }
- get compression() {
- if (this.si.config.compression) {
- return this.si.config.compression !== StoreCompression.None;
- }
- return false;
- }
- };
- var digestType = "SHA-256=";
- function objectStoreStreamName(bucket) {
- validateBucket(bucket);
- return `${osPrefix}${bucket}`;
- }
- function objectStoreBucketName(stream) {
- if (stream.startsWith(osPrefix)) {
- return stream.substring(4);
- }
- return stream;
- }
- var ObjectStoreStatusImpl = class {
- constructor(si) {
- __publicField(this, "si");
- __publicField(this, "backingStore");
- this.si = si;
- this.backingStore = "JetStream";
- }
- get bucket() {
- return objectStoreBucketName(this.si.config.name);
- }
- get description() {
- return this.si.config.description ?? "";
- }
- get ttl() {
- return this.si.config.max_age;
- }
- get storage() {
- return this.si.config.storage;
- }
- get replicas() {
- return this.si.config.num_replicas;
- }
- get sealed() {
- return this.si.config.sealed;
- }
- get size() {
- return this.si.state.bytes;
- }
- get streamInfo() {
- return this.si;
- }
- get metadata() {
- return this.si.config.metadata;
- }
- get compression() {
- if (this.si.config.compression) {
- return this.si.config.compression !== StoreCompression.None;
- }
- return false;
- }
- };
- var StreamAPIImpl = class extends BaseApiClient {
- constructor(nc, opts) {
- super(nc, opts);
- }
- checkStreamConfigVersions(cfg) {
- const nci = this.nc;
- if (cfg.metadata) {
- const { min, ok } = nci.features.get(Feature.JS_STREAM_CONSUMER_METADATA);
- if (!ok) {
- throw new Error(`stream 'metadata' requires server ${min}`);
- }
- }
- if (cfg.first_seq) {
- const { min, ok } = nci.features.get(Feature.JS_STREAM_FIRST_SEQ);
- if (!ok) {
- throw new Error(`stream 'first_seq' requires server ${min}`);
- }
- }
- if (cfg.subject_transform) {
- const { min, ok } = nci.features.get(Feature.JS_STREAM_SUBJECT_TRANSFORM);
- if (!ok) {
- throw new Error(`stream 'subject_transform' requires server ${min}`);
- }
- }
- if (cfg.compression) {
- const { min, ok } = nci.features.get(Feature.JS_STREAM_COMPRESSION);
- if (!ok) {
- throw new Error(`stream 'compression' requires server ${min}`);
- }
- }
- if (cfg.consumer_limits) {
- const { min, ok } = nci.features.get(Feature.JS_DEFAULT_CONSUMER_LIMITS);
- if (!ok) {
- throw new Error(`stream 'consumer_limits' requires server ${min}`);
- }
- }
- function validateStreamSource(context, src) {
- var _a;
- const count = ((_a = src.subject_transforms) == null ? void 0 : _a.length) || 0;
- if (count > 0) {
- const { min, ok } = nci.features.get(Feature.JS_STREAM_SOURCE_SUBJECT_TRANSFORM);
- if (!ok) {
- throw new Error(`${context} 'subject_transforms' requires server ${min}`);
- }
- }
- }
- if (cfg.sources) {
- cfg.sources.forEach((src) => {
- validateStreamSource("stream sources", src);
- });
- }
- if (cfg.mirror) {
- validateStreamSource("stream mirror", cfg.mirror);
- }
- }
- async add(cfg = {}) {
- var _a;
- this.checkStreamConfigVersions(cfg);
- validateStreamName(cfg.name);
- cfg.mirror = convertStreamSourceDomain(cfg.mirror);
- cfg.sources = (_a = cfg.sources) == null ? void 0 : _a.map(convertStreamSourceDomain);
- const r = await this._request(`${this.prefix}.STREAM.CREATE.${cfg.name}`, cfg);
- const si = r;
- this._fixInfo(si);
- return si;
- }
- async delete(stream) {
- validateStreamName(stream);
- const r = await this._request(`${this.prefix}.STREAM.DELETE.${stream}`);
- const cr = r;
- return cr.success;
- }
- async update(name, cfg = {}) {
- var _a;
- if (typeof name === "object") {
- const sc = name;
- name = sc.name;
- cfg = sc;
- console.trace(`\x1B[33m >> streams.update(config: StreamConfig) api changed to streams.update(name: string, config: StreamUpdateConfig) - this shim will be removed - update your code. \x1B[0m`);
- }
- this.checkStreamConfigVersions(cfg);
- validateStreamName(name);
- const old = await this.info(name);
- const update = Object.assign(old.config, cfg);
- update.mirror = convertStreamSourceDomain(update.mirror);
- update.sources = (_a = update.sources) == null ? void 0 : _a.map(convertStreamSourceDomain);
- const r = await this._request(`${this.prefix}.STREAM.UPDATE.${name}`, update);
- const si = r;
- this._fixInfo(si);
- return si;
- }
- async info(name, data) {
- validateStreamName(name);
- const subj = `${this.prefix}.STREAM.INFO.${name}`;
- const r = await this._request(subj, data);
- let si = r;
- let { total, limit } = si;
- let have = si.state.subjects ? Object.getOwnPropertyNames(si.state.subjects).length : 1;
- if (total && total > have) {
- const infos = [
- si
- ];
- const paged = data || {};
- let i = 0;
- while (total > have) {
- i++;
- paged.offset = limit * i;
- const r2 = await this._request(subj, paged);
- total = r2.total;
- infos.push(r2);
- const count = Object.getOwnPropertyNames(r2.state.subjects).length;
- have += count;
- if (count < limit) {
- break;
- }
- }
- let subjects = {};
- for (let i2 = 0; i2 < infos.length; i2++) {
- si = infos[i2];
- if (si.state.subjects) {
- subjects = Object.assign(subjects, si.state.subjects);
- }
- }
- si.offset = 0;
- si.total = 0;
- si.limit = 0;
- si.state.subjects = subjects;
- }
- this._fixInfo(si);
- return si;
- }
- list(subject = "") {
- const payload = (subject == null ? void 0 : subject.length) ? {
- subject
- } : {};
- const listerFilter = (v) => {
- const slr = v;
- slr.streams.forEach((si) => {
- this._fixInfo(si);
- });
- return slr.streams;
- };
- const subj = `${this.prefix}.STREAM.LIST`;
- return new ListerImpl(subj, listerFilter, this, payload);
- }
- _fixInfo(si) {
- si.config.sealed = si.config.sealed || false;
- si.config.deny_delete = si.config.deny_delete || false;
- si.config.deny_purge = si.config.deny_purge || false;
- si.config.allow_rollup_hdrs = si.config.allow_rollup_hdrs || false;
- }
- async purge(name, opts) {
- if (opts) {
- const { keep, seq } = opts;
- if (typeof keep === "number" && typeof seq === "number") {
- throw new Error("can specify one of keep or seq");
- }
- }
- validateStreamName(name);
- const v = await this._request(`${this.prefix}.STREAM.PURGE.${name}`, opts);
- return v;
- }
- async deleteMessage(stream, seq, erase = true) {
- validateStreamName(stream);
- const dr = {
- seq
- };
- if (!erase) {
- dr.no_erase = true;
- }
- const r = await this._request(`${this.prefix}.STREAM.MSG.DELETE.${stream}`, dr);
- const cr = r;
- return cr.success;
- }
- async getMessage(stream, query) {
- validateStreamName(stream);
- const r = await this._request(`${this.prefix}.STREAM.MSG.GET.${stream}`, query);
- const sm = r;
- return new StoredMsgImpl(sm);
- }
- find(subject) {
- return this.findStream(subject);
- }
- listKvs() {
- const filter = (v) => {
- var _a;
- const slr = v;
- const kvStreams = slr.streams.filter((v2) => {
- return v2.config.name.startsWith(kvPrefix);
- });
- kvStreams.forEach((si) => {
- this._fixInfo(si);
- });
- let cluster = "";
- if (kvStreams.length) {
- cluster = ((_a = this.nc.info) == null ? void 0 : _a.cluster) ?? "";
- }
- const status = kvStreams.map((si) => {
- return new KvStatusImpl(si, cluster);
- });
- return status;
- };
- const subj = `${this.prefix}.STREAM.LIST`;
- return new ListerImpl(subj, filter, this);
- }
- listObjectStores() {
- const filter = (v) => {
- const slr = v;
- const objStreams = slr.streams.filter((v2) => {
- return v2.config.name.startsWith(osPrefix);
- });
- objStreams.forEach((si) => {
- this._fixInfo(si);
- });
- const status = objStreams.map((si) => {
- return new ObjectStoreStatusImpl(si);
- });
- return status;
- };
- const subj = `${this.prefix}.STREAM.LIST`;
- return new ListerImpl(subj, filter, this);
- }
- names(subject = "") {
- const payload = (subject == null ? void 0 : subject.length) ? {
- subject
- } : {};
- const listerFilter = (v) => {
- const sr = v;
- return sr.streams;
- };
- const subj = `${this.prefix}.STREAM.NAMES`;
- return new ListerImpl(subj, listerFilter, this, payload);
- }
- async get(name) {
- const si = await this.info(name);
- return Promise.resolve(new StreamImpl(this, si));
- }
- };
- var StoredMsgImpl = class {
- constructor(smr) {
- __publicField(this, "_header");
- __publicField(this, "smr");
- this.smr = smr;
- }
- get subject() {
- return this.smr.message.subject;
- }
- get seq() {
- return this.smr.message.seq;
- }
- get timestamp() {
- return this.smr.message.time;
- }
- get time() {
- return new Date(Date.parse(this.timestamp));
- }
- get data() {
- return this.smr.message.data ? this._parse(this.smr.message.data) : Empty;
- }
- get header() {
- if (!this._header) {
- if (this.smr.message.hdrs) {
- const hd = this._parse(this.smr.message.hdrs);
- this._header = MsgHdrsImpl.decode(hd);
- } else {
- this._header = headers();
- }
- }
- return this._header;
- }
- _parse(s) {
- const bs = atob(s);
- const len = bs.length;
- const bytes = new Uint8Array(len);
- for (let i = 0; i < len; i++) {
- bytes[i] = bs.charCodeAt(i);
- }
- return bytes;
- }
- json(reviver) {
- return JSONCodec(reviver).decode(this.data);
- }
- string() {
- return TD.decode(this.data);
- }
- };
- __publicField(StoredMsgImpl, "jc");
- var StreamsImpl = class {
- constructor(api) {
- __publicField(this, "api");
- this.api = api;
- }
- get(stream) {
- return this.api.info(stream).then((si) => {
- return new StreamImpl(this.api, si);
- });
- }
- };
- var KvStoredEntryImpl = class {
- constructor(bucket, prefixLen, sm) {
- __publicField(this, "bucket");
- __publicField(this, "sm");
- __publicField(this, "prefixLen");
- this.bucket = bucket;
- this.prefixLen = prefixLen;
- this.sm = sm;
- }
- get key() {
- return this.sm.subject.substring(this.prefixLen);
- }
- get value() {
- return this.sm.data;
- }
- get delta() {
- return 0;
- }
- get created() {
- return this.sm.time;
- }
- get revision() {
- return this.sm.seq;
- }
- get operation() {
- return this.sm.header.get(kvOperationHdr) || "PUT";
- }
- get length() {
- const slen = this.sm.header.get(JsHeaders.MessageSizeHdr) || "";
- if (slen !== "") {
- return parseInt(slen, 10);
- }
- return this.sm.data.length;
- }
- json() {
- return this.sm.json();
- }
- string() {
- return this.sm.string();
- }
- };
- var KvJsMsgEntryImpl = class {
- constructor(bucket, key, sm) {
- __publicField(this, "bucket");
- __publicField(this, "key");
- __publicField(this, "sm");
- this.bucket = bucket;
- this.key = key;
- this.sm = sm;
- }
- get value() {
- return this.sm.data;
- }
- get created() {
- return new Date(millis(this.sm.info.timestampNanos));
- }
- get revision() {
- return this.sm.seq;
- }
- get operation() {
- var _a;
- return ((_a = this.sm.headers) == null ? void 0 : _a.get(kvOperationHdr)) || "PUT";
- }
- get delta() {
- return this.sm.info.pending;
- }
- get length() {
- var _a;
- const slen = ((_a = this.sm.headers) == null ? void 0 : _a.get(JsHeaders.MessageSizeHdr)) || "";
- if (slen !== "") {
- return parseInt(slen, 10);
- }
- return this.sm.data.length;
- }
- json() {
- return this.sm.json();
- }
- string() {
- return this.sm.string();
- }
- };
- var ObjectInfoImpl = class {
- constructor(oi) {
- __publicField(this, "info");
- __publicField(this, "hdrs");
- this.info = oi;
- }
- get name() {
- return this.info.name;
- }
- get description() {
- return this.info.description ?? "";
- }
- get headers() {
- if (!this.hdrs) {
- this.hdrs = MsgHdrsImpl.fromRecord(this.info.headers || {});
- }
- return this.hdrs;
- }
- get options() {
- return this.info.options;
- }
- get bucket() {
- return this.info.bucket;
- }
- get chunks() {
- return this.info.chunks;
- }
- get deleted() {
- return this.info.deleted ?? false;
- }
- get digest() {
- return this.info.digest;
- }
- get mtime() {
- return this.info.mtime;
- }
- get nuid() {
- return this.info.nuid;
- }
- get size() {
- return this.info.size;
- }
- get revision() {
- return this.info.revision;
- }
- get metadata() {
- return this.info.metadata || {};
- }
- isLink() {
- var _a, _b;
- return ((_a = this.info.options) == null ? void 0 : _a.link) !== void 0 && ((_b = this.info.options) == null ? void 0 : _b.link) !== null;
- }
- };
- function toServerObjectStoreMeta(meta) {
- const v = {
- name: meta.name,
- description: meta.description ?? "",
- options: meta.options,
- metadata: meta.metadata
- };
- if (meta.headers) {
- const mhi = meta.headers;
- v.headers = mhi.toRecord();
- }
- return v;
- }
- function emptyReadableStream() {
- return new ReadableStream({
- pull(c) {
- c.enqueue(new Uint8Array(0));
- c.close();
- }
- });
- }
- var ObjectStoreImpl = class {
- constructor(name, jsm, js) {
- __publicField(this, "jsm");
- __publicField(this, "js");
- __publicField(this, "stream");
- __publicField(this, "name");
- this.name = name;
- this.jsm = jsm;
- this.js = js;
- }
- _checkNotEmpty(name) {
- if (!name || name.length === 0) {
- return {
- name,
- error: new Error("name cannot be empty")
- };
- }
- return {
- name
- };
- }
- async info(name) {
- const info = await this.rawInfo(name);
- return info ? new ObjectInfoImpl(info) : null;
- }
- async list() {
- const buf = [];
- const iter = await this.watch({
- ignoreDeletes: true,
- includeHistory: true
- });
- for await (const info of iter) {
- if (info === null) {
- break;
- }
- buf.push(info);
- }
- return Promise.resolve(buf);
- }
- async rawInfo(name) {
- const { name: obj, error } = this._checkNotEmpty(name);
- if (error) {
- return Promise.reject(error);
- }
- const meta = this._metaSubject(obj);
- try {
- const m = await this.jsm.streams.getMessage(this.stream, {
- last_by_subj: meta
- });
- const jc = JSONCodec();
- const soi = jc.decode(m.data);
- soi.revision = m.seq;
- return soi;
- } catch (err) {
- if (err.code === "404") {
- return null;
- }
- return Promise.reject(err);
- }
- }
- async _si(opts) {
- try {
- return await this.jsm.streams.info(this.stream, opts);
- } catch (err) {
- const nerr = err;
- if (nerr.code === "404") {
- return null;
- }
- return Promise.reject(err);
- }
- }
- async seal() {
- let info = await this._si();
- if (info === null) {
- return Promise.reject(new Error("object store not found"));
- }
- info.config.sealed = true;
- info = await this.jsm.streams.update(this.stream, info.config);
- return Promise.resolve(new ObjectStoreStatusImpl(info));
- }
- async status(opts) {
- const info = await this._si(opts);
- if (info === null) {
- return Promise.reject(new Error("object store not found"));
- }
- return Promise.resolve(new ObjectStoreStatusImpl(info));
- }
- destroy() {
- return this.jsm.streams.delete(this.stream);
- }
- async _put(meta, rs, opts) {
- var _a;
- const jsopts = this.js.getOptions();
- opts = opts || {
- timeout: jsopts.timeout
- };
- opts.timeout = opts.timeout || jsopts.timeout;
- opts.previousRevision = opts.previousRevision ?? void 0;
- const { timeout: timeout2, previousRevision } = opts;
- const si = this.js.nc.info;
- const maxPayload = (si == null ? void 0 : si.max_payload) || 1024;
- meta = meta || {};
- meta.options = meta.options || {};
- let maxChunk = ((_a = meta.options) == null ? void 0 : _a.max_chunk_size) || 128 * 1024;
- maxChunk = maxChunk > maxPayload ? maxPayload : maxChunk;
- meta.options.max_chunk_size = maxChunk;
- const old = await this.info(meta.name);
- const { name: n, error } = this._checkNotEmpty(meta.name);
- if (error) {
- return Promise.reject(error);
- }
- const id = nuid.next();
- const chunkSubj = this._chunkSubject(id);
- const metaSubj = this._metaSubject(n);
- const info = Object.assign({
- bucket: this.name,
- nuid: id,
- size: 0,
- chunks: 0
- }, toServerObjectStoreMeta(meta));
- const d = deferred();
- const proms = [];
- const db = new DataBuffer();
- try {
- const reader = rs ? rs.getReader() : null;
- const sha = new SHA256();
- while (true) {
- const { done, value } = reader ? await reader.read() : {
- done: true,
- value: void 0
- };
- if (done) {
- if (db.size() > 0) {
- const payload = db.drain();
- sha.update(payload);
- info.chunks++;
- info.size += payload.length;
- proms.push(this.js.publish(chunkSubj, payload, {
- timeout: timeout2
- }));
- }
- await Promise.all(proms);
- proms.length = 0;
- info.mtime = (/* @__PURE__ */ new Date()).toISOString();
- const digest = sha.digest("base64");
- const pad = digest.length % 3;
- const padding = pad > 0 ? "=".repeat(pad) : "";
- info.digest = `${digestType}${digest}${padding}`;
- info.deleted = false;
- const h = headers();
- if (typeof previousRevision === "number") {
- h.set(PubHeaders.ExpectedLastSubjectSequenceHdr, `${previousRevision}`);
- }
- h.set(JsHeaders.RollupHdr, JsHeaders.RollupValueSubject);
- const pa = await this.js.publish(metaSubj, JSONCodec().encode(info), {
- headers: h,
- timeout: timeout2
- });
- info.revision = pa.seq;
- if (old) {
- try {
- await this.jsm.streams.purge(this.stream, {
- filter: `$O.${this.name}.C.${old.nuid}`
- });
- } catch (_err) {
- }
- }
- d.resolve(new ObjectInfoImpl(info));
- break;
- }
- if (value) {
- db.fill(value);
- while (db.size() > maxChunk) {
- info.chunks++;
- info.size += maxChunk;
- const payload = db.drain(meta.options.max_chunk_size);
- sha.update(payload);
- proms.push(this.js.publish(chunkSubj, payload, {
- timeout: timeout2
- }));
- }
- }
- }
- } catch (err) {
- await this.jsm.streams.purge(this.stream, {
- filter: chunkSubj
- });
- d.reject(err);
- }
- return d;
- }
- putBlob(meta, data, opts) {
- function readableStreamFrom(data2) {
- return new ReadableStream({
- pull(controller) {
- controller.enqueue(data2);
- controller.close();
- }
- });
- }
- if (data === null) {
- data = new Uint8Array(0);
- }
- return this.put(meta, readableStreamFrom(data), opts);
- }
- put(meta, rs, opts) {
- var _a;
- if ((_a = meta == null ? void 0 : meta.options) == null ? void 0 : _a.link) {
- return Promise.reject(new Error("link cannot be set when putting the object in bucket"));
- }
- return this._put(meta, rs, opts);
- }
- async getBlob(name) {
- async function fromReadableStream(rs) {
- const buf = new DataBuffer();
- const reader = rs.getReader();
- while (true) {
- const { done, value } = await reader.read();
- if (done) {
- return buf.drain();
- }
- if (value && value.length) {
- buf.fill(value);
- }
- }
- }
- const r = await this.get(name);
- if (r === null) {
- return Promise.resolve(null);
- }
- const vs = await Promise.all([
- r.error,
- fromReadableStream(r.data)
- ]);
- if (vs[0]) {
- return Promise.reject(vs[0]);
- } else {
- return Promise.resolve(vs[1]);
- }
- }
- async get(name) {
- const info = await this.rawInfo(name);
- if (info === null) {
- return Promise.resolve(null);
- }
- if (info.deleted) {
- return Promise.resolve(null);
- }
- if (info.options && info.options.link) {
- const ln = info.options.link.name || "";
- if (ln === "") {
- throw new Error("link is a bucket");
- }
- const os = info.options.link.bucket !== this.name ? await ObjectStoreImpl.create(this.js, info.options.link.bucket) : this;
- return os.get(ln);
- }
- const d = deferred();
- const r = {
- info: new ObjectInfoImpl(info),
- error: d
- };
- if (info.size === 0) {
- r.data = emptyReadableStream();
- d.resolve(null);
- return Promise.resolve(r);
- }
- let controller;
- const oc = consumerOpts();
- oc.orderedConsumer();
- const sha = new SHA256();
- const subj = `$O.${this.name}.C.${info.nuid}`;
- const sub = await this.js.subscribe(subj, oc);
- (async () => {
- for await (const jm of sub) {
- if (jm.data.length > 0) {
- sha.update(jm.data);
- controller.enqueue(jm.data);
- }
- if (jm.info.pending === 0) {
- const hash = sha.digest("base64");
- const pad = hash.length % 3;
- const padding = pad > 0 ? "=".repeat(pad) : "";
- const digest = `${digestType}${hash}${padding}`;
- if (digest !== info.digest) {
- controller.error(new Error(`received a corrupt object, digests do not match received: ${info.digest} calculated ${digest}`));
- } else {
- controller.close();
- }
- sub.unsubscribe();
- }
- }
- })().then(() => {
- d.resolve();
- }).catch((err) => {
- controller.error(err);
- d.reject(err);
- });
- r.data = new ReadableStream({
- start(c) {
- controller = c;
- },
- cancel() {
- sub.unsubscribe();
- }
- });
- return r;
- }
- linkStore(name, bucket) {
- if (!(bucket instanceof ObjectStoreImpl)) {
- return Promise.reject("bucket required");
- }
- const osi = bucket;
- const { name: n, error } = this._checkNotEmpty(name);
- if (error) {
- return Promise.reject(error);
- }
- const meta = {
- name: n,
- options: {
- link: {
- bucket: osi.name
- }
- }
- };
- return this._put(meta, null);
- }
- async link(name, info) {
- const { name: n, error } = this._checkNotEmpty(name);
- if (error) {
- return Promise.reject(error);
- }
- if (info.deleted) {
- return Promise.reject(new Error("src object is deleted"));
- }
- if (info.isLink()) {
- return Promise.reject(new Error("src object is a link"));
- }
- const dest = await this.rawInfo(name);
- if (dest !== null && !dest.deleted) {
- return Promise.reject(new Error("an object already exists with that name"));
- }
- const link = {
- bucket: info.bucket,
- name: info.name
- };
- const mm = {
- name: n,
- bucket: info.bucket,
- options: {
- link
- }
- };
- await this.js.publish(this._metaSubject(name), JSON.stringify(mm));
- const i = await this.info(name);
- return Promise.resolve(i);
- }
- async delete(name) {
- const info = await this.rawInfo(name);
- if (info === null) {
- return Promise.resolve({
- purged: 0,
- success: false
- });
- }
- info.deleted = true;
- info.size = 0;
- info.chunks = 0;
- info.digest = "";
- const jc = JSONCodec();
- const h = headers();
- h.set(JsHeaders.RollupHdr, JsHeaders.RollupValueSubject);
- await this.js.publish(this._metaSubject(info.name), jc.encode(info), {
- headers: h
- });
- return this.jsm.streams.purge(this.stream, {
- filter: this._chunkSubject(info.nuid)
- });
- }
- async update(name, meta = {}) {
- const info = await this.rawInfo(name);
- if (info === null) {
- return Promise.reject(new Error("object not found"));
- }
- if (info.deleted) {
- return Promise.reject(new Error("cannot update meta for a deleted object"));
- }
- meta.name = meta.name ?? info.name;
- const { name: n, error } = this._checkNotEmpty(meta.name);
- if (error) {
- return Promise.reject(error);
- }
- if (name !== meta.name) {
- const i = await this.info(meta.name);
- if (i && !i.deleted) {
- return Promise.reject(new Error("an object already exists with that name"));
- }
- }
- meta.name = n;
- const ii = Object.assign({}, info, toServerObjectStoreMeta(meta));
- const ack = await this.js.publish(this._metaSubject(ii.name), JSON.stringify(ii));
- if (name !== meta.name) {
- await this.jsm.streams.purge(this.stream, {
- filter: this._metaSubject(name)
- });
- }
- return Promise.resolve(ack);
- }
- async watch(opts = {}) {
- opts.includeHistory = opts.includeHistory ?? false;
- opts.ignoreDeletes = opts.ignoreDeletes ?? false;
- let initialized = false;
- const qi = new QueuedIteratorImpl();
- const subj = this._metaSubjectAll();
- try {
- await this.jsm.streams.getMessage(this.stream, {
- last_by_subj: subj
- });
- } catch (err) {
- if (err.code === "404") {
- qi.push(null);
- initialized = true;
- } else {
- qi.stop(err);
- }
- }
- const jc = JSONCodec();
- const copts = consumerOpts();
- copts.orderedConsumer();
- if (opts.includeHistory) {
- copts.deliverLastPerSubject();
- } else {
- initialized = true;
- copts.deliverNew();
- }
- copts.callback((err, jm) => {
- var _a;
- if (err) {
- qi.stop(err);
- return;
- }
- if (jm !== null) {
- const oi = jc.decode(jm.data);
- if (oi.deleted && opts.ignoreDeletes === true) {
- } else {
- qi.push(oi);
- }
- if (((_a = jm.info) == null ? void 0 : _a.pending) === 0 && !initialized) {
- initialized = true;
- qi.push(null);
- }
- }
- });
- const sub = await this.js.subscribe(subj, copts);
- qi._data = sub;
- qi.iterClosed.then(() => {
- sub.unsubscribe();
- });
- sub.closed.then(() => {
- qi.stop();
- }).catch((err) => {
- qi.stop(err);
- });
- return qi;
- }
- _chunkSubject(id) {
- return `$O.${this.name}.C.${id}`;
- }
- _metaSubject(n) {
- return `$O.${this.name}.M.${Base64UrlPaddedCodec.encode(n)}`;
- }
- _metaSubjectAll() {
- return `$O.${this.name}.M.>`;
- }
- async init(opts = {}) {
- try {
- this.stream = objectStoreStreamName(this.name);
- } catch (err) {
- return Promise.reject(err);
- }
- const max_age = (opts == null ? void 0 : opts.ttl) || 0;
- delete opts.ttl;
- const sc = Object.assign({
- max_age
- }, opts);
- sc.name = this.stream;
- sc.allow_direct = true;
- sc.allow_rollup_hdrs = true;
- sc.discard = DiscardPolicy.New;
- sc.subjects = [
- `$O.${this.name}.C.>`,
- `$O.${this.name}.M.>`
- ];
- if (opts.placement) {
- sc.placement = opts.placement;
- }
- if (opts.metadata) {
- sc.metadata = opts.metadata;
- }
- if (typeof opts.compression === "boolean") {
- sc.compression = opts.compression ? StoreCompression.S2 : StoreCompression.None;
- }
- try {
- await this.jsm.streams.info(sc.name);
- } catch (err) {
- if (err.message === "stream not found") {
- await this.jsm.streams.add(sc);
- }
- }
- }
- static async create(js, name, opts = {}) {
- const jsm = await js.jetstreamManager();
- const os = new ObjectStoreImpl(name, jsm, js);
- await os.init(opts);
- return Promise.resolve(os);
- }
- };
- var ViewsImpl = class {
- constructor(js) {
- __publicField(this, "js");
- __publicField(this, "jsm");
- this.js = js;
- }
- kv(name, opts = {}) {
- const jsi = this.js;
- const { ok, min } = jsi.nc.features.get(Feature.JS_KV);
- if (!ok) {
- return Promise.reject(new Error(`kv is only supported on servers ${min} or better`));
- }
- if (opts.bindOnly) {
- return Bucket.bind(this.js, name);
- }
- return Bucket.create(this.js, name, opts);
- }
- os(name, opts = {}) {
- var _a;
- if (typeof ((_a = crypto == null ? void 0 : crypto.subtle) == null ? void 0 : _a.digest) !== "function") {
- return Promise.reject(new Error("objectstore: unable to calculate hashes - crypto.subtle.digest with sha256 support is required"));
- }
- const jsi = this.js;
- const { ok, min } = jsi.nc.features.get(Feature.JS_OBJECTSTORE);
- if (!ok) {
- return Promise.reject(new Error(`objectstore is only supported on servers ${min} or better`));
- }
- return ObjectStoreImpl.create(this.js, name, opts);
- }
- };
- var JetStreamClientImpl = class extends BaseApiClient {
- constructor(nc, opts) {
- super(nc, opts);
- __publicField(this, "consumers");
- __publicField(this, "streams");
- __publicField(this, "consumerAPI");
- __publicField(this, "streamAPI");
- this.consumerAPI = new ConsumerAPIImpl(nc, opts);
- this.streamAPI = new StreamAPIImpl(nc, opts);
- this.consumers = new ConsumersImpl(this.consumerAPI);
- this.streams = new StreamsImpl(this.streamAPI);
- }
- jetstreamManager(checkAPI) {
- const opts = Object.assign({
- checkAPI
- }, this.opts);
- return this.nc.jetstreamManager(opts);
- }
- get apiPrefix() {
- return this.prefix;
- }
- get views() {
- return new ViewsImpl(this);
- }
- async publish(subj, data = Empty, opts) {
- opts = opts || {};
- opts.expect = opts.expect || {};
- const mh = (opts == null ? void 0 : opts.headers) || headers();
- if (opts) {
- if (opts.msgID) {
- mh.set(PubHeaders.MsgIdHdr, opts.msgID);
- }
- if (opts.expect.lastMsgID) {
- mh.set(PubHeaders.ExpectedLastMsgIdHdr, opts.expect.lastMsgID);
- }
- if (opts.expect.streamName) {
- mh.set(PubHeaders.ExpectedStreamHdr, opts.expect.streamName);
- }
- if (typeof opts.expect.lastSequence === "number") {
- mh.set(PubHeaders.ExpectedLastSeqHdr, `${opts.expect.lastSequence}`);
- }
- if (typeof opts.expect.lastSubjectSequence === "number") {
- mh.set(PubHeaders.ExpectedLastSubjectSequenceHdr, `${opts.expect.lastSubjectSequence}`);
- }
- }
- const to = opts.timeout || this.timeout;
- const ro = {};
- if (to) {
- ro.timeout = to;
- }
- if (opts) {
- ro.headers = mh;
- }
- let { retries, retry_delay } = opts;
- retries = retries || 1;
- retry_delay = retry_delay || 250;
- let r;
- for (let i = 0; i < retries; i++) {
- try {
- r = await this.nc.request(subj, data, ro);
- break;
- } catch (err) {
- const ne = err;
- if (ne.code === "503" && i + 1 < retries) {
- await delay(retry_delay);
- } else {
- throw err;
- }
- }
- }
- const pa = this.parseJsResponse(r);
- if (pa.stream === "") {
- throw NatsError.errorForCode(ErrorCode.JetStreamInvalidAck);
- }
- pa.duplicate = pa.duplicate ? pa.duplicate : false;
- return pa;
- }
- async pull(stream, durable, expires = 0) {
- validateStreamName(stream);
- validateDurableName(durable);
- let timeout2 = this.timeout;
- if (expires > timeout2) {
- timeout2 = expires;
- }
- expires = expires < 0 ? 0 : nanos(expires);
- const pullOpts = {
- batch: 1,
- no_wait: expires === 0,
- expires
- };
- const msg = await this.nc.request(`${this.prefix}.CONSUMER.MSG.NEXT.${stream}.${durable}`, this.jc.encode(pullOpts), {
- noMux: true,
- timeout: timeout2
- });
- const err = checkJsError(msg);
- if (err) {
- throw err;
- }
- return toJsMsg(msg);
- }
- fetch(stream, durable, opts = {}) {
- validateStreamName(stream);
- validateDurableName(durable);
- let timer = null;
- const trackBytes = (opts.max_bytes ?? 0) > 0;
- let receivedBytes = 0;
- const max_bytes = trackBytes ? opts.max_bytes : 0;
- let monitor = null;
- const args = {};
- args.batch = opts.batch || 1;
- if (max_bytes) {
- const fv = this.nc.features.get(Feature.JS_PULL_MAX_BYTES);
- if (!fv.ok) {
- throw new Error(`max_bytes is only supported on servers ${fv.min} or better`);
- }
- args.max_bytes = max_bytes;
- }
- args.no_wait = opts.no_wait || false;
- if (args.no_wait && args.expires) {
- args.expires = 0;
- }
- const expires = opts.expires || 0;
- if (expires) {
- args.expires = nanos(expires);
- }
- if (expires === 0 && args.no_wait === false) {
- throw new Error("expires or no_wait is required");
- }
- const hb = opts.idle_heartbeat || 0;
- if (hb) {
- args.idle_heartbeat = nanos(hb);
- if (opts.delay_heartbeat === true) {
- args.idle_heartbeat = nanos(hb * 4);
- }
- }
- const qi = new QueuedIteratorImpl();
- const wants = args.batch;
- let received = 0;
- qi.protocolFilterFn = (jm, _ingest = false) => {
- const jsmi = jm;
- if (isHeartbeatMsg(jsmi.msg)) {
- monitor == null ? void 0 : monitor.work();
- return false;
- }
- return true;
- };
- qi.dispatchedFn = (m) => {
- if (m) {
- if (trackBytes) {
- receivedBytes += m.data.length;
- }
- received++;
- if (timer && m.info.pending === 0) {
- return;
- }
- if (qi.getPending() === 1 && m.info.pending === 0 || wants === received || max_bytes > 0 && receivedBytes >= max_bytes) {
- qi.stop();
- }
- }
- };
- const inbox = createInbox(this.nc.options.inboxPrefix);
- const sub = this.nc.subscribe(inbox, {
- max: opts.batch,
- callback: (err, msg) => {
- if (err === null) {
- err = checkJsError(msg);
- }
- if (err !== null) {
- if (timer) {
- timer.cancel();
- timer = null;
- }
- if (isNatsError(err)) {
- qi.stop(hideNonTerminalJsErrors(err) === null ? void 0 : err);
- } else {
- qi.stop(err);
- }
- } else {
- monitor == null ? void 0 : monitor.work();
- qi.received++;
- qi.push(toJsMsg(msg));
- }
- }
- });
- if (expires) {
- timer = timeout(expires);
- timer.catch(() => {
- if (!sub.isClosed()) {
- sub.drain().catch(() => {
- });
- timer = null;
- }
- if (monitor) {
- monitor.cancel();
- }
- });
- }
- (async () => {
- try {
- if (hb) {
- monitor = new IdleHeartbeatMonitor(hb, (v) => {
- qi.push(() => {
- qi.err = new NatsError(`${Js409Errors.IdleHeartbeatMissed}: ${v}`, ErrorCode.JetStreamIdleHeartBeat);
- });
- return true;
- });
- }
- } catch (_err) {
- }
- await sub.closed;
- if (timer !== null) {
- timer.cancel();
- timer = null;
- }
- if (monitor) {
- monitor.cancel();
- }
- qi.stop();
- })().catch();
- this.nc.publish(`${this.prefix}.CONSUMER.MSG.NEXT.${stream}.${durable}`, this.jc.encode(args), {
- reply: inbox
- });
- return qi;
- }
- async pullSubscribe(subject, opts = consumerOpts()) {
- const cso = await this._processOptions(subject, opts);
- if (cso.ordered) {
- throw new Error("pull subscribers cannot be be ordered");
- }
- if (cso.config.deliver_subject) {
- throw new Error("consumer info specifies deliver_subject - pull consumers cannot have deliver_subject set");
- }
- const ackPolicy = cso.config.ack_policy;
- if (ackPolicy === AckPolicy.None || ackPolicy === AckPolicy.All) {
- throw new Error("ack policy for pull consumers must be explicit");
- }
- const so = this._buildTypedSubscriptionOpts(cso);
- const sub = new JetStreamPullSubscriptionImpl(this, cso.deliver, so);
- sub.info = cso;
- try {
- await this._maybeCreateConsumer(cso);
- } catch (err) {
- sub.unsubscribe();
- throw err;
- }
- return sub;
- }
- async subscribe(subject, opts = consumerOpts()) {
- const cso = await this._processOptions(subject, opts);
- if (!cso.isBind && !cso.config.deliver_subject) {
- throw new Error("push consumer requires deliver_subject");
- }
- const so = this._buildTypedSubscriptionOpts(cso);
- const sub = new JetStreamSubscriptionImpl(this, cso.deliver, so);
- sub.info = cso;
- try {
- await this._maybeCreateConsumer(cso);
- } catch (err) {
- sub.unsubscribe();
- throw err;
- }
- sub._maybeSetupHbMonitoring();
- return sub;
- }
- async _processOptions(subject, opts = consumerOpts()) {
- const jsi = isConsumerOptsBuilder(opts) ? opts.getOpts() : opts;
- jsi.isBind = isConsumerOptsBuilder(opts) ? opts.isBind : false;
- jsi.flow_control = {
- heartbeat_count: 0,
- fc_count: 0,
- consumer_restarts: 0
- };
- if (jsi.ordered) {
- jsi.ordered_consumer_sequence = {
- stream_seq: 0,
- delivery_seq: 0
- };
- if (jsi.config.ack_policy !== AckPolicy.NotSet && jsi.config.ack_policy !== AckPolicy.None) {
- throw new NatsError("ordered consumer: ack_policy can only be set to 'none'", ErrorCode.ApiError);
- }
- if (jsi.config.durable_name && jsi.config.durable_name.length > 0) {
- throw new NatsError("ordered consumer: durable_name cannot be set", ErrorCode.ApiError);
- }
- if (jsi.config.deliver_subject && jsi.config.deliver_subject.length > 0) {
- throw new NatsError("ordered consumer: deliver_subject cannot be set", ErrorCode.ApiError);
- }
- if (jsi.config.max_deliver !== void 0 && jsi.config.max_deliver > 1) {
- throw new NatsError("ordered consumer: max_deliver cannot be set", ErrorCode.ApiError);
- }
- if (jsi.config.deliver_group && jsi.config.deliver_group.length > 0) {
- throw new NatsError("ordered consumer: deliver_group cannot be set", ErrorCode.ApiError);
- }
- jsi.config.deliver_subject = createInbox(this.nc.options.inboxPrefix);
- jsi.config.ack_policy = AckPolicy.None;
- jsi.config.max_deliver = 1;
- jsi.config.flow_control = true;
- jsi.config.idle_heartbeat = jsi.config.idle_heartbeat || nanos(5e3);
- jsi.config.ack_wait = nanos(22 * 60 * 60 * 1e3);
- jsi.config.mem_storage = true;
- jsi.config.num_replicas = 1;
- }
- if (jsi.config.ack_policy === AckPolicy.NotSet) {
- jsi.config.ack_policy = AckPolicy.All;
- }
- jsi.api = this;
- jsi.config = jsi.config || {};
- jsi.stream = jsi.stream ? jsi.stream : await this.findStream(subject);
- jsi.attached = false;
- if (jsi.config.durable_name) {
- try {
- const info = await this.consumerAPI.info(jsi.stream, jsi.config.durable_name);
- if (info) {
- if (info.config.filter_subject && info.config.filter_subject !== subject) {
- throw new Error("subject does not match consumer");
- }
- const qn = jsi.config.deliver_group ?? "";
- if (qn === "" && info.push_bound === true) {
- throw new Error(`duplicate subscription`);
- }
- const rqn = info.config.deliver_group ?? "";
- if (qn !== rqn) {
- if (rqn === "") {
- throw new Error(`durable requires no queue group`);
- } else {
- throw new Error(`durable requires queue group '${rqn}'`);
- }
- }
- jsi.last = info;
- jsi.config = info.config;
- jsi.attached = true;
- if (!jsi.config.durable_name) {
- jsi.name = info.name;
- }
- }
- } catch (err) {
- if (err.code !== "404") {
- throw err;
- }
- }
- }
- if (!jsi.attached && jsi.config.filter_subject === void 0 && jsi.config.filter_subjects === void 0) {
- jsi.config.filter_subject = subject;
- }
- jsi.deliver = jsi.config.deliver_subject || createInbox(this.nc.options.inboxPrefix);
- return jsi;
- }
- _buildTypedSubscriptionOpts(jsi) {
- const so = {};
- so.adapter = msgAdapter(jsi.callbackFn === void 0);
- so.ingestionFilterFn = JetStreamClientImpl.ingestionFn(jsi.ordered);
- so.protocolFilterFn = (jm, ingest = false) => {
- const jsmi = jm;
- if (isFlowControlMsg(jsmi.msg)) {
- if (!ingest) {
- jsmi.msg.respond();
- }
- return false;
- }
- return true;
- };
- if (!jsi.mack && jsi.config.ack_policy !== AckPolicy.None) {
- so.dispatchedFn = autoAckJsMsg;
- }
- if (jsi.callbackFn) {
- so.callback = jsi.callbackFn;
- }
- so.max = jsi.max || 0;
- so.queue = jsi.queue;
- return so;
- }
- async _maybeCreateConsumer(jsi) {
- if (jsi.attached) {
- return;
- }
- if (jsi.isBind) {
- throw new Error(`unable to bind - durable consumer ${jsi.config.durable_name} doesn't exist in ${jsi.stream}`);
- }
- jsi.config = Object.assign({
- deliver_policy: DeliverPolicy.All,
- ack_policy: AckPolicy.Explicit,
- ack_wait: nanos(30 * 1e3),
- replay_policy: ReplayPolicy.Instant
- }, jsi.config);
- const ci = await this.consumerAPI.add(jsi.stream, jsi.config);
- if (Array.isArray(jsi.config.filter_subjects && !Array.isArray(ci.config.filter_subjects))) {
- throw new Error(`jetstream server doesn't support consumers with multiple filter subjects`);
- }
- jsi.name = ci.name;
- jsi.config = ci.config;
- jsi.last = ci;
- }
- static ingestionFn(ordered) {
- return (jm, ctx) => {
- var _a;
- const jsub = ctx;
- if (!jm)
- return {
- ingest: false,
- protocol: false
- };
- const jmi = jm;
- if (!checkJsError(jmi.msg)) {
- (_a = jsub.monitor) == null ? void 0 : _a.work();
- }
- if (isHeartbeatMsg(jmi.msg)) {
- const ingest2 = ordered ? jsub._checkHbOrderConsumer(jmi.msg) : true;
- if (!ordered) {
- jsub.info.flow_control.heartbeat_count++;
- }
- return {
- ingest: ingest2,
- protocol: true
- };
- } else if (isFlowControlMsg(jmi.msg)) {
- jsub.info.flow_control.fc_count++;
- return {
- ingest: true,
- protocol: true
- };
- }
- const ingest = ordered ? jsub._checkOrderedConsumer(jm) : true;
- return {
- ingest,
- protocol: false
- };
- };
- }
- };
- var JetStreamSubscriptionImpl = class extends TypedSubscription {
- constructor(js, subject, opts) {
- super(js.nc, subject, opts);
- __publicField(this, "js");
- __publicField(this, "monitor");
- this.js = js;
- this.monitor = null;
- this.sub.closed.then(() => {
- if (this.monitor) {
- this.monitor.cancel();
- }
- });
- }
- set info(info) {
- this.sub.info = info;
- }
- get info() {
- return this.sub.info;
- }
- _resetOrderedConsumer(sseq) {
- if (this.info === null || this.sub.isClosed()) {
- return;
- }
- const newDeliver = createInbox(this.js.nc.options.inboxPrefix);
- const nci = this.js.nc;
- nci._resub(this.sub, newDeliver);
- const info = this.info;
- info.ordered_consumer_sequence.delivery_seq = 0;
- info.flow_control.heartbeat_count = 0;
- info.flow_control.fc_count = 0;
- info.flow_control.consumer_restarts++;
- info.deliver = newDeliver;
- info.config.deliver_subject = newDeliver;
- info.config.deliver_policy = DeliverPolicy.StartSequence;
- info.config.opt_start_seq = sseq;
- const req = {};
- req.stream_name = this.info.stream;
- req.config = info.config;
- const subj = `${info.api.prefix}.CONSUMER.CREATE.${info.stream}`;
- this.js._request(subj, req).then((v) => {
- const ci = v;
- this.info.config = ci.config;
- this.info.name = ci.name;
- }).catch((err) => {
- const nerr = new NatsError(`unable to recreate ordered consumer ${info.stream} at seq ${sseq}`, ErrorCode.RequestError, err);
- this.sub.callback(nerr, {});
- });
- }
- _maybeSetupHbMonitoring() {
- var _a, _b;
- const ns = ((_b = (_a = this.info) == null ? void 0 : _a.config) == null ? void 0 : _b.idle_heartbeat) || 0;
- if (ns) {
- this._setupHbMonitoring(millis(ns));
- }
- }
- _setupHbMonitoring(millis2, cancelAfter = 0) {
- const opts = {
- cancelAfter: 0,
- maxOut: 2
- };
- if (cancelAfter) {
- opts.cancelAfter = cancelAfter;
- }
- const sub = this.sub;
- const handler = (v) => {
- var _a, _b, _c;
- const msg = newJsErrorMsg(409, `${Js409Errors.IdleHeartbeatMissed}: ${v}`, this.sub.subject);
- const ordered = (_a = this.info) == null ? void 0 : _a.ordered;
- if (!ordered) {
- this.sub.callback(null, msg);
- } else {
- if (!this.js.nc.protocol.connected) {
- return false;
- }
- const seq = ((_c = (_b = this.info) == null ? void 0 : _b.ordered_consumer_sequence) == null ? void 0 : _c.stream_seq) || 0;
- this._resetOrderedConsumer(seq + 1);
- return false;
- }
- return !sub.noIterator;
- };
- this.monitor = new IdleHeartbeatMonitor(millis2, handler, opts);
- }
- _checkHbOrderConsumer(msg) {
- const rm = msg.headers.get(JsHeaders.ConsumerStalledHdr);
- if (rm !== "") {
- const nci = this.js.nc;
- nci.publish(rm);
- }
- const lastDelivered = parseInt(msg.headers.get(JsHeaders.LastConsumerSeqHdr), 10);
- const ordered = this.info.ordered_consumer_sequence;
- this.info.flow_control.heartbeat_count++;
- if (lastDelivered !== ordered.delivery_seq) {
- this._resetOrderedConsumer(ordered.stream_seq + 1);
- }
- return false;
- }
- _checkOrderedConsumer(jm) {
- const ordered = this.info.ordered_consumer_sequence;
- const sseq = jm.info.streamSequence;
- const dseq = jm.info.deliverySequence;
- if (dseq != ordered.delivery_seq + 1) {
- this._resetOrderedConsumer(ordered.stream_seq + 1);
- return false;
- }
- ordered.delivery_seq = dseq;
- ordered.stream_seq = sseq;
- return true;
- }
- async destroy() {
- if (!this.isClosed()) {
- await this.drain();
- }
- const jinfo = this.sub.info;
- const name = jinfo.config.durable_name || jinfo.name;
- const subj = `${jinfo.api.prefix}.CONSUMER.DELETE.${jinfo.stream}.${name}`;
- await jinfo.api._request(subj);
- }
- async consumerInfo() {
- const jinfo = this.sub.info;
- const name = jinfo.config.durable_name || jinfo.name;
- const subj = `${jinfo.api.prefix}.CONSUMER.INFO.${jinfo.stream}.${name}`;
- const ci = await jinfo.api._request(subj);
- jinfo.last = ci;
- return ci;
- }
- };
- var JetStreamPullSubscriptionImpl = class extends JetStreamSubscriptionImpl {
- constructor(js, subject, opts) {
- super(js, subject, opts);
- }
- pull(opts = {
- batch: 1
- }) {
- const { stream, config, name } = this.sub.info;
- const consumer = config.durable_name ?? name;
- const args = {};
- args.batch = opts.batch || 1;
- args.no_wait = opts.no_wait || false;
- if ((opts.max_bytes ?? 0) > 0) {
- const fv = this.js.nc.features.get(Feature.JS_PULL_MAX_BYTES);
- if (!fv.ok) {
- throw new Error(`max_bytes is only supported on servers ${fv.min} or better`);
- }
- args.max_bytes = opts.max_bytes;
- }
- let expires = 0;
- if (opts.expires && opts.expires > 0) {
- expires = opts.expires;
- args.expires = nanos(expires);
- }
- let hb = 0;
- if (opts.idle_heartbeat && opts.idle_heartbeat > 0) {
- hb = opts.idle_heartbeat;
- args.idle_heartbeat = nanos(hb);
- }
- if (hb && expires === 0) {
- throw new Error("idle_heartbeat requires expires");
- }
- if (hb > expires) {
- throw new Error("expires must be greater than idle_heartbeat");
- }
- if (this.info) {
- if (this.monitor) {
- this.monitor.cancel();
- }
- if (expires && hb) {
- if (!this.monitor) {
- this._setupHbMonitoring(hb, expires);
- } else {
- this.monitor._change(hb, expires);
- }
- }
- const api = this.info.api;
- const subj = `${api.prefix}.CONSUMER.MSG.NEXT.${stream}.${consumer}`;
- const reply = this.sub.subject;
- api.nc.publish(subj, api.jc.encode(args), {
- reply
- });
- }
- }
- };
- function msgAdapter(iterator) {
- if (iterator) {
- return iterMsgAdapter;
- } else {
- return cbMsgAdapter;
- }
- }
- function cbMsgAdapter(err, msg) {
- if (err) {
- return [
- err,
- null
- ];
- }
- err = checkJsError(msg);
- if (err) {
- return [
- err,
- null
- ];
- }
- return [
- null,
- toJsMsg(msg)
- ];
- }
- function iterMsgAdapter(err, msg) {
- if (err) {
- return [
- err,
- null
- ];
- }
- const ne = checkJsError(msg);
- if (ne !== null) {
- return [
- hideNonTerminalJsErrors(ne),
- null
- ];
- }
- return [
- null,
- toJsMsg(msg)
- ];
- }
- function hideNonTerminalJsErrors(ne) {
- if (ne !== null) {
- switch (ne.code) {
- case ErrorCode.JetStream404NoMessages:
- case ErrorCode.JetStream408RequestTimeout:
- return null;
- case ErrorCode.JetStream409:
- if (isTerminal409(ne)) {
- return ne;
- }
- return null;
- default:
- return ne;
- }
- }
- return null;
- }
- function autoAckJsMsg(data) {
- if (data) {
- data.ack();
- }
- }
- var DirectStreamAPIImpl = class extends BaseApiClient {
- constructor(nc, opts) {
- super(nc, opts);
- }
- async getMessage(stream, query) {
- validateStreamName(stream);
- let qq = query;
- const { last_by_subj } = qq;
- if (last_by_subj) {
- qq = null;
- }
- const payload = qq ? this.jc.encode(qq) : Empty;
- const pre = this.opts.apiPrefix || "$JS.API";
- const subj = last_by_subj ? `${pre}.DIRECT.GET.${stream}.${last_by_subj}` : `${pre}.DIRECT.GET.${stream}`;
- const r = await this.nc.request(subj, payload);
- const err = checkJsError(r);
- if (err) {
- return Promise.reject(err);
- }
- const dm = new DirectMsgImpl(r);
- return Promise.resolve(dm);
- }
- };
- var DirectMsgImpl = class {
- constructor(m) {
- __publicField(this, "data");
- __publicField(this, "header");
- if (!m.headers) {
- throw new Error("headers expected");
- }
- this.data = m.data;
- this.header = m.headers;
- }
- get subject() {
- return this.header.last(DirectMsgHeaders.Subject);
- }
- get seq() {
- const v = this.header.last(DirectMsgHeaders.Sequence);
- return typeof v === "string" ? parseInt(v) : 0;
- }
- get time() {
- return new Date(Date.parse(this.timestamp));
- }
- get timestamp() {
- return this.header.last(DirectMsgHeaders.TimeStamp);
- }
- get stream() {
- return this.header.last(DirectMsgHeaders.Stream);
- }
- json(reviver) {
- return JSONCodec(reviver).decode(this.data);
- }
- string() {
- return TD.decode(this.data);
- }
- };
- __publicField(DirectMsgImpl, "jc");
- var JetStreamManagerImpl = class extends BaseApiClient {
- constructor(nc, opts) {
- super(nc, opts);
- __publicField(this, "streams");
- __publicField(this, "consumers");
- __publicField(this, "direct");
- this.streams = new StreamAPIImpl(nc, opts);
- this.consumers = new ConsumerAPIImpl(nc, opts);
- this.direct = new DirectStreamAPIImpl(nc, opts);
- }
- async getAccountInfo() {
- const r = await this._request(`${this.prefix}.INFO`);
- return r;
- }
- jetstream() {
- return this.nc.jetstream(this.getOptions());
- }
- advisories() {
- const iter = new QueuedIteratorImpl();
- this.nc.subscribe(`$JS.EVENT.ADVISORY.>`, {
- callback: (err, msg) => {
- if (err) {
- throw err;
- }
- try {
- const d = this.parseJsResponse(msg);
- const chunks = d.type.split(".");
- const kind = chunks[chunks.length - 1];
- iter.push({
- kind,
- data: d
- });
- } catch (err2) {
- iter.stop(err2);
- }
- }
- });
- return iter;
- }
- };
- var ServiceApiPrefix = "$SRV";
- var ServiceMsgImpl = class {
- constructor(msg) {
- __publicField(this, "msg");
- this.msg = msg;
- }
- get data() {
- return this.msg.data;
- }
- get sid() {
- return this.msg.sid;
- }
- get subject() {
- return this.msg.subject;
- }
- get reply() {
- return this.msg.reply || "";
- }
- get headers() {
- return this.msg.headers;
- }
- respond(data, opts) {
- return this.msg.respond(data, opts);
- }
- respondError(code2, description, data, opts) {
- var _a, _b;
- opts = opts || {};
- opts.headers = opts.headers || headers();
- (_a = opts.headers) == null ? void 0 : _a.set(ServiceErrorCodeHeader, `${code2}`);
- (_b = opts.headers) == null ? void 0 : _b.set(ServiceErrorHeader, description);
- return this.msg.respond(data, opts);
- }
- json(reviver) {
- return this.msg.json(reviver);
- }
- string() {
- return this.msg.string();
- }
- };
- var ServiceGroupImpl = class {
- constructor(parent, name = "", queue = "") {
- __publicField(this, "subject");
- __publicField(this, "queue");
- __publicField(this, "srv");
- if (name !== "") {
- validInternalToken("service group", name);
- }
- let root = "";
- if (parent instanceof ServiceImpl) {
- this.srv = parent;
- root = "";
- } else if (parent instanceof ServiceGroupImpl) {
- const sg = parent;
- this.srv = sg.srv;
- if (queue === "" && sg.queue !== "") {
- queue = sg.queue;
- }
- root = sg.subject;
- } else {
- throw new Error("unknown ServiceGroup type");
- }
- this.subject = this.calcSubject(root, name);
- this.queue = queue;
- }
- calcSubject(root, name = "") {
- if (name === "") {
- return root;
- }
- return root !== "" ? `${root}.${name}` : name;
- }
- addEndpoint(name = "", opts) {
- opts = opts || {
- subject: name
- };
- const args = typeof opts === "function" ? {
- handler: opts,
- subject: name
- } : opts;
- validateName("endpoint", name);
- let { subject, handler, metadata, queue } = args;
- subject = subject || name;
- queue = queue || this.queue;
- validSubjectName("endpoint subject", subject);
- subject = this.calcSubject(this.subject, subject);
- const ne = {
- name,
- subject,
- queue,
- handler,
- metadata
- };
- return this.srv._addEndpoint(ne);
- }
- addGroup(name = "", queue = "") {
- return new ServiceGroupImpl(this, name, queue);
- }
- };
- function validSubjectName(context, subj) {
- if (subj === "") {
- throw new Error(`${context} cannot be empty`);
- }
- if (subj.indexOf(" ") !== -1) {
- throw new Error(`${context} cannot contain spaces: '${subj}'`);
- }
- const tokens = subj.split(".");
- tokens.forEach((v, idx) => {
- if (v === ">" && idx !== tokens.length - 1) {
- throw new Error(`${context} cannot have internal '>': '${subj}'`);
- }
- });
- }
- function validInternalToken(context, subj) {
- if (subj.indexOf(" ") !== -1) {
- throw new Error(`${context} cannot contain spaces: '${subj}'`);
- }
- const tokens = subj.split(".");
- tokens.forEach((v) => {
- if (v === ">") {
- throw new Error(`${context} name cannot contain internal '>': '${subj}'`);
- }
- });
- }
- var ServiceImpl = class {
- constructor(nc, config = {
- name: "",
- version: ""
- }) {
- __publicField(this, "nc");
- __publicField(this, "_id");
- __publicField(this, "config");
- __publicField(this, "handlers");
- __publicField(this, "internal");
- __publicField(this, "_stopped");
- __publicField(this, "_done");
- __publicField(this, "started");
- this.nc = nc;
- this.config = Object.assign({}, config);
- if (!this.config.queue) {
- this.config.queue = "q";
- }
- validateName("name", this.config.name);
- validateName("queue", this.config.queue);
- parseSemVer(this.config.version);
- this._id = nuid.next();
- this.internal = [];
- this._done = deferred();
- this._stopped = false;
- this.handlers = [];
- this.started = (/* @__PURE__ */ new Date()).toISOString();
- this.reset();
- this.nc.closed().then(() => {
- this.close().catch();
- }).catch((err) => {
- this.close(err).catch();
- });
- }
- static controlSubject(verb, name = "", id = "", prefix) {
- const pre = prefix ?? ServiceApiPrefix;
- if (name === "" && id === "") {
- return `${pre}.${verb}`;
- }
- validateName("control subject name", name);
- if (id !== "") {
- validateName("control subject id", id);
- return `${pre}.${verb}.${name}.${id}`;
- }
- return `${pre}.${verb}.${name}`;
- }
- get subjects() {
- return this.handlers.filter((s) => {
- return s.internal === false;
- }).map((s) => {
- return s.subject;
- });
- }
- get id() {
- return this._id;
- }
- get name() {
- return this.config.name;
- }
- get description() {
- return this.config.description ?? "";
- }
- get version() {
- return this.config.version;
- }
- get metadata() {
- return this.config.metadata;
- }
- errorToHeader(err) {
- const h = headers();
- if (err instanceof ServiceError) {
- const se = err;
- h.set(ServiceErrorHeader, se.message);
- h.set(ServiceErrorCodeHeader, `${se.code}`);
- } else {
- h.set(ServiceErrorHeader, err.message);
- h.set(ServiceErrorCodeHeader, "500");
- }
- return h;
- }
- setupHandler(h, internal = false) {
- const queue = internal ? "" : h.queue ? h.queue : this.config.queue;
- const { name, subject, handler } = h;
- const sv = h;
- sv.internal = internal;
- if (internal) {
- this.internal.push(sv);
- }
- sv.stats = new NamedEndpointStatsImpl(name, subject, queue);
- sv.queue = queue;
- const callback = handler ? (err, msg) => {
- if (err) {
- this.close(err);
- return;
- }
- const start = Date.now();
- try {
- handler(err, new ServiceMsgImpl(msg));
- } catch (err2) {
- sv.stats.countError(err2);
- msg == null ? void 0 : msg.respond(Empty, {
- headers: this.errorToHeader(err2)
- });
- } finally {
- sv.stats.countLatency(start);
- }
- } : void 0;
- sv.sub = this.nc.subscribe(subject, {
- callback,
- queue
- });
- sv.sub.closed.then(() => {
- if (!this._stopped) {
- this.close(new Error(`required subscription ${h.subject} stopped`)).catch();
- }
- }).catch((err) => {
- if (!this._stopped) {
- const ne = new Error(`required subscription ${h.subject} errored: ${err.message}`);
- ne.stack = err.stack;
- this.close(ne).catch();
- }
- });
- return sv;
- }
- info() {
- return {
- type: ServiceResponseType.INFO,
- name: this.name,
- id: this.id,
- version: this.version,
- description: this.description,
- metadata: this.metadata,
- endpoints: this.endpoints()
- };
- }
- endpoints() {
- return this.handlers.map((v) => {
- const { subject, metadata, name, queue } = v;
- return {
- subject,
- metadata,
- name,
- queue_group: queue
- };
- });
- }
- async stats() {
- const endpoints = [];
- for (const h of this.handlers) {
- if (typeof this.config.statsHandler === "function") {
- try {
- h.stats.data = await this.config.statsHandler(h);
- } catch (err) {
- h.stats.countError(err);
- }
- }
- endpoints.push(h.stats.stats(h.qi));
- }
- return {
- type: ServiceResponseType.STATS,
- name: this.name,
- id: this.id,
- version: this.version,
- started: this.started,
- metadata: this.metadata,
- endpoints
- };
- }
- addInternalHandler(verb, handler) {
- const v = `${verb}`.toUpperCase();
- this._doAddInternalHandler(`${v}-all`, verb, handler);
- this._doAddInternalHandler(`${v}-kind`, verb, handler, this.name);
- this._doAddInternalHandler(`${v}`, verb, handler, this.name, this.id);
- }
- _doAddInternalHandler(name, verb, handler, kind = "", id = "") {
- const endpoint = {};
- endpoint.name = name;
- endpoint.subject = ServiceImpl.controlSubject(verb, kind, id);
- endpoint.handler = handler;
- this.setupHandler(endpoint, true);
- }
- start() {
- const jc = JSONCodec();
- const statsHandler = (err, msg) => {
- if (err) {
- this.close(err);
- return Promise.reject(err);
- }
- return this.stats().then((s) => {
- msg == null ? void 0 : msg.respond(jc.encode(s));
- return Promise.resolve();
- });
- };
- const infoHandler = (err, msg) => {
- if (err) {
- this.close(err);
- return Promise.reject(err);
- }
- msg == null ? void 0 : msg.respond(jc.encode(this.info()));
- return Promise.resolve();
- };
- const ping = jc.encode(this.ping());
- const pingHandler = (err, msg) => {
- if (err) {
- this.close(err).then().catch();
- return Promise.reject(err);
- }
- msg.respond(ping);
- return Promise.resolve();
- };
- this.addInternalHandler(ServiceVerb.PING, pingHandler);
- this.addInternalHandler(ServiceVerb.STATS, statsHandler);
- this.addInternalHandler(ServiceVerb.INFO, infoHandler);
- this.handlers.forEach((h) => {
- const { subject } = h;
- if (typeof subject !== "string") {
- return;
- }
- if (h.handler === null) {
- return;
- }
- this.setupHandler(h);
- });
- return Promise.resolve(this);
- }
- close(err) {
- if (this._stopped) {
- return this._done;
- }
- this._stopped = true;
- let buf = [];
- if (!this.nc.isClosed()) {
- buf = this.handlers.concat(this.internal).map((h) => {
- return h.sub.drain();
- });
- }
- Promise.allSettled(buf).then(() => {
- this._done.resolve(err ? err : null);
- });
- return this._done;
- }
- get stopped() {
- return this._done;
- }
- get isStopped() {
- return this._stopped;
- }
- stop(err) {
- return this.close(err);
- }
- ping() {
- return {
- type: ServiceResponseType.PING,
- name: this.name,
- id: this.id,
- version: this.version,
- metadata: this.metadata
- };
- }
- reset() {
- this.started = (/* @__PURE__ */ new Date()).toISOString();
- if (this.handlers) {
- for (const h of this.handlers) {
- h.stats.reset(h.qi);
- }
- }
- }
- addGroup(name, queue) {
- return new ServiceGroupImpl(this, name, queue);
- }
- addEndpoint(name, handler) {
- const sg = new ServiceGroupImpl(this);
- return sg.addEndpoint(name, handler);
- }
- _addEndpoint(e) {
- const qi = new QueuedIteratorImpl();
- qi.noIterator = typeof e.handler === "function";
- if (!qi.noIterator) {
- e.handler = (err, msg) => {
- err ? this.stop(err).catch() : qi.push(new ServiceMsgImpl(msg));
- };
- qi.iterClosed.then(() => {
- this.close().catch();
- });
- }
- const ss = this.setupHandler(e, false);
- ss.qi = qi;
- this.handlers.push(ss);
- return qi;
- }
- };
- var NamedEndpointStatsImpl = class {
- constructor(name, subject, queue = "") {
- __publicField(this, "name");
- __publicField(this, "subject");
- __publicField(this, "average_processing_time");
- __publicField(this, "num_requests");
- __publicField(this, "processing_time");
- __publicField(this, "num_errors");
- __publicField(this, "last_error");
- __publicField(this, "data");
- __publicField(this, "metadata");
- __publicField(this, "queue");
- this.name = name;
- this.subject = subject;
- this.average_processing_time = 0;
- this.num_errors = 0;
- this.num_requests = 0;
- this.processing_time = 0;
- this.queue = queue;
- }
- reset(qi) {
- this.num_requests = 0;
- this.processing_time = 0;
- this.average_processing_time = 0;
- this.num_errors = 0;
- this.last_error = void 0;
- this.data = void 0;
- const qii = qi;
- if (qii) {
- qii.time = 0;
- qii.processed = 0;
- }
- }
- countLatency(start) {
- this.num_requests++;
- this.processing_time += nanos(Date.now() - start);
- this.average_processing_time = Math.round(this.processing_time / this.num_requests);
- }
- countError(err) {
- this.num_errors++;
- this.last_error = err.message;
- }
- _stats() {
- const { name, subject, average_processing_time, num_errors, num_requests, processing_time, last_error, data, queue } = this;
- return {
- name,
- subject,
- average_processing_time,
- num_errors,
- num_requests,
- processing_time,
- last_error,
- data,
- queue_group: queue
- };
- }
- stats(qi) {
- const qii = qi;
- if ((qii == null ? void 0 : qii.noIterator) === false) {
- this.processing_time = qii.time;
- this.num_requests = qii.processed;
- this.average_processing_time = this.processing_time > 0 && this.num_requests > 0 ? this.processing_time / this.num_requests : 0;
- }
- return this._stats();
- }
- };
- var ServiceClientImpl = class {
- constructor(nc, opts = {
- strategy: RequestStrategy.JitterTimer,
- maxWait: 2e3
- }, prefix) {
- __publicField(this, "nc");
- __publicField(this, "prefix");
- __publicField(this, "opts");
- this.nc = nc;
- this.prefix = prefix;
- this.opts = opts;
- }
- ping(name = "", id = "") {
- return this.q(ServiceVerb.PING, name, id);
- }
- stats(name = "", id = "") {
- return this.q(ServiceVerb.STATS, name, id);
- }
- info(name = "", id = "") {
- return this.q(ServiceVerb.INFO, name, id);
- }
- async q(v, name = "", id = "") {
- const iter = new QueuedIteratorImpl();
- const jc = JSONCodec();
- const subj = ServiceImpl.controlSubject(v, name, id, this.prefix);
- const responses = await this.nc.requestMany(subj, Empty, this.opts);
- (async () => {
- for await (const m of responses) {
- try {
- const s = jc.decode(m.data);
- iter.push(s);
- } catch (err) {
- iter.push(() => {
- iter.stop(err);
- });
- }
- }
- iter.push(() => {
- iter.stop();
- });
- })().catch((err) => {
- iter.stop(err);
- });
- return iter;
- }
- };
- var NatsConnectionImpl = class {
- constructor(opts) {
- __publicField(this, "options");
- __publicField(this, "protocol");
- __publicField(this, "draining");
- __publicField(this, "listeners");
- __publicField(this, "_services");
- this.draining = false;
- this.options = parseOptions(opts);
- this.listeners = [];
- }
- static connect(opts = {}) {
- return new Promise((resolve, reject) => {
- const nc = new NatsConnectionImpl(opts);
- ProtocolHandler.connect(nc.options, nc).then((ph) => {
- nc.protocol = ph;
- (async function() {
- for await (const s of ph.status()) {
- nc.listeners.forEach((l) => {
- l.push(s);
- });
- }
- })();
- resolve(nc);
- }).catch((err) => {
- reject(err);
- });
- });
- }
- closed() {
- return this.protocol.closed;
- }
- async close() {
- await this.protocol.close();
- }
- _check(subject, sub, pub) {
- if (this.isClosed()) {
- throw NatsError.errorForCode(ErrorCode.ConnectionClosed);
- }
- if (sub && this.isDraining()) {
- throw NatsError.errorForCode(ErrorCode.ConnectionDraining);
- }
- if (pub && this.protocol.noMorePublishing) {
- throw NatsError.errorForCode(ErrorCode.ConnectionDraining);
- }
- subject = subject || "";
- if (subject.length === 0) {
- throw NatsError.errorForCode(ErrorCode.BadSubject);
- }
- }
- publish(subject, data, options) {
- this._check(subject, false, true);
- this.protocol.publish(subject, data, options);
- }
- subscribe(subject, opts = {}) {
- this._check(subject, true, false);
- const sub = new SubscriptionImpl(this.protocol, subject, opts);
- this.protocol.subscribe(sub);
- return sub;
- }
- _resub(s, subject, max) {
- this._check(subject, true, false);
- const si = s;
- si.max = max;
- if (max) {
- si.max = max + si.received;
- }
- this.protocol.resub(si, subject);
- }
- requestMany(subject, data = Empty, opts = {
- maxWait: 1e3,
- maxMessages: -1
- }) {
- const asyncTraces = !(this.protocol.options.noAsyncTraces || false);
- try {
- this._check(subject, true, true);
- } catch (err) {
- return Promise.reject(err);
- }
- opts.strategy = opts.strategy || RequestStrategy.Timer;
- opts.maxWait = opts.maxWait || 1e3;
- if (opts.maxWait < 1) {
- return Promise.reject(new NatsError("timeout", ErrorCode.InvalidOption));
- }
- const qi = new QueuedIteratorImpl();
- function stop(err) {
- qi.push(() => {
- qi.stop(err);
- });
- }
- function callback(err, msg) {
- if (err || msg === null) {
- stop(err === null ? void 0 : err);
- } else {
- qi.push(msg);
- }
- }
- if (opts.noMux) {
- const stack = asyncTraces ? new Error().stack : null;
- let max = typeof opts.maxMessages === "number" && opts.maxMessages > 0 ? opts.maxMessages : -1;
- const sub = this.subscribe(createInbox(this.options.inboxPrefix), {
- callback: (err, msg) => {
- var _a, _b;
- if (((_a = msg == null ? void 0 : msg.data) == null ? void 0 : _a.length) === 0 && ((_b = msg == null ? void 0 : msg.headers) == null ? void 0 : _b.status) === ErrorCode.NoResponders) {
- err = NatsError.errorForCode(ErrorCode.NoResponders);
- }
- if (err) {
- if (stack) {
- err.stack += `
- ${stack}`;
- }
- cancel(err);
- return;
- }
- callback(null, msg);
- if (opts.strategy === RequestStrategy.Count) {
- max--;
- if (max === 0) {
- cancel();
- }
- }
- if (opts.strategy === RequestStrategy.JitterTimer) {
- clearTimers();
- timer = setTimeout(() => {
- cancel();
- }, 300);
- }
- if (opts.strategy === RequestStrategy.SentinelMsg) {
- if (msg && msg.data.length === 0) {
- cancel();
- }
- }
- }
- });
- sub.closed.then(() => {
- stop();
- }).catch((err) => {
- qi.stop(err);
- });
- const cancel = (err) => {
- if (err) {
- qi.push(() => {
- throw err;
- });
- }
- clearTimers();
- sub.drain().then(() => {
- stop();
- }).catch((_err) => {
- stop();
- });
- };
- qi.iterClosed.then(() => {
- clearTimers();
- sub == null ? void 0 : sub.unsubscribe();
- }).catch((_err) => {
- clearTimers();
- sub == null ? void 0 : sub.unsubscribe();
- });
- try {
- this.publish(subject, data, {
- reply: sub.getSubject()
- });
- } catch (err) {
- cancel(err);
- }
- let timer = setTimeout(() => {
- cancel();
- }, opts.maxWait);
- const clearTimers = () => {
- if (timer) {
- clearTimeout(timer);
- }
- };
- } else {
- const rmo = opts;
- rmo.callback = callback;
- qi.iterClosed.then(() => {
- r.cancel();
- }).catch((err) => {
- r.cancel(err);
- });
- const r = new RequestMany(this.protocol.muxSubscriptions, subject, rmo);
- this.protocol.request(r);
- try {
- this.publish(subject, data, {
- reply: `${this.protocol.muxSubscriptions.baseInbox}${r.token}`,
- headers: opts.headers
- });
- } catch (err) {
- r.cancel(err);
- }
- }
- return Promise.resolve(qi);
- }
- request(subject, data, opts = {
- timeout: 1e3,
- noMux: false
- }) {
- try {
- this._check(subject, true, true);
- } catch (err) {
- return Promise.reject(err);
- }
- const asyncTraces = !(this.protocol.options.noAsyncTraces || false);
- opts.timeout = opts.timeout || 1e3;
- if (opts.timeout < 1) {
- return Promise.reject(new NatsError("timeout", ErrorCode.InvalidOption));
- }
- if (!opts.noMux && opts.reply) {
- return Promise.reject(new NatsError("reply can only be used with noMux", ErrorCode.InvalidOption));
- }
- if (opts.noMux) {
- const inbox = opts.reply ? opts.reply : createInbox(this.options.inboxPrefix);
- const d = deferred();
- const errCtx = asyncTraces ? new Error() : null;
- const sub = this.subscribe(inbox, {
- max: 1,
- timeout: opts.timeout,
- callback: (err, msg) => {
- if (err) {
- if (errCtx && err.code !== ErrorCode.Timeout) {
- err.stack += `
- ${errCtx.stack}`;
- }
- d.reject(err);
- } else {
- err = isRequestError(msg);
- if (err) {
- if (errCtx) {
- err.stack += `
- ${errCtx.stack}`;
- }
- d.reject(err);
- } else {
- d.resolve(msg);
- }
- }
- }
- });
- sub.requestSubject = subject;
- this.protocol.publish(subject, data, {
- reply: inbox,
- headers: opts.headers
- });
- return d;
- } else {
- const r = new RequestOne(this.protocol.muxSubscriptions, subject, opts, asyncTraces);
- this.protocol.request(r);
- try {
- this.publish(subject, data, {
- reply: `${this.protocol.muxSubscriptions.baseInbox}${r.token}`,
- headers: opts.headers
- });
- } catch (err) {
- r.cancel(err);
- }
- const p = Promise.race([
- r.timer,
- r.deferred
- ]);
- p.catch(() => {
- r.cancel();
- });
- return p;
- }
- }
- flush() {
- if (this.isClosed()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionClosed));
- }
- return this.protocol.flush();
- }
- drain() {
- if (this.isClosed()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionClosed));
- }
- if (this.isDraining()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionDraining));
- }
- this.draining = true;
- return this.protocol.drain();
- }
- isClosed() {
- return this.protocol.isClosed();
- }
- isDraining() {
- return this.draining;
- }
- getServer() {
- const srv = this.protocol.getServer();
- return srv ? srv.listen : "";
- }
- status() {
- const iter = new QueuedIteratorImpl();
- iter.iterClosed.then(() => {
- const idx = this.listeners.indexOf(iter);
- this.listeners.splice(idx, 1);
- });
- this.listeners.push(iter);
- return iter;
- }
- get info() {
- return this.protocol.isClosed() ? void 0 : this.protocol.info;
- }
- stats() {
- return {
- inBytes: this.protocol.inBytes,
- outBytes: this.protocol.outBytes,
- inMsgs: this.protocol.inMsgs,
- outMsgs: this.protocol.outMsgs
- };
- }
- async jetstreamManager(opts = {}) {
- const adm = new JetStreamManagerImpl(this, opts);
- if (opts.checkAPI !== false) {
- try {
- await adm.getAccountInfo();
- } catch (err) {
- const ne = err;
- if (ne.code === ErrorCode.NoResponders) {
- ne.code = ErrorCode.JetStreamNotEnabled;
- }
- throw ne;
- }
- }
- return adm;
- }
- jetstream(opts = {}) {
- return new JetStreamClientImpl(this, opts);
- }
- getServerVersion() {
- const info = this.info;
- return info ? parseSemVer(info.version) : void 0;
- }
- async rtt() {
- if (!this.protocol._closed && !this.protocol.connected) {
- throw NatsError.errorForCode(ErrorCode.Disconnect);
- }
- const start = Date.now();
- await this.flush();
- return Date.now() - start;
- }
- get features() {
- return this.protocol.features;
- }
- get services() {
- if (!this._services) {
- this._services = new ServicesFactory(this);
- }
- return this._services;
- }
- reconnect() {
- if (this.isClosed()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionClosed));
- }
- if (this.isDraining()) {
- return Promise.reject(NatsError.errorForCode(ErrorCode.ConnectionDraining));
- }
- return this.protocol.reconnect();
- }
- };
- var ServicesFactory = class {
- constructor(nc) {
- __publicField(this, "nc");
- this.nc = nc;
- }
- add(config) {
- try {
- const s = new ServiceImpl(this.nc, config);
- return s.start();
- } catch (err) {
- return Promise.reject(err);
- }
- }
- client(opts, prefix) {
- return new ServiceClientImpl(this.nc, opts, prefix);
- }
- };
- var Metric = class {
- constructor(name, duration) {
- __publicField(this, "name");
- __publicField(this, "duration");
- __publicField(this, "date");
- __publicField(this, "payload");
- __publicField(this, "msgs");
- __publicField(this, "lang");
- __publicField(this, "version");
- __publicField(this, "bytes");
- __publicField(this, "asyncRequests");
- __publicField(this, "min");
- __publicField(this, "max");
- this.name = name;
- this.duration = duration;
- this.date = Date.now();
- this.payload = 0;
- this.msgs = 0;
- this.bytes = 0;
- }
- toString() {
- const sec = this.duration / 1e3;
- const mps = Math.round(this.msgs / sec);
- const label = this.asyncRequests ? "asyncRequests" : "";
- let minmax = "";
- if (this.max) {
- minmax = `${this.min}/${this.max}`;
- }
- return `${this.name}${label ? " [asyncRequests]" : ""} ${humanizeNumber(mps)} msgs/sec - [${sec.toFixed(2)} secs] ~ ${throughput(this.bytes, sec)} ${minmax}`;
- }
- toCsv() {
- return `"${this.name}",${new Date(this.date).toISOString()},${this.lang},${this.version},${this.msgs},${this.payload},${this.bytes},${this.duration},${this.asyncRequests ? this.asyncRequests : false}
- `;
- }
- static header() {
- return `Test,Date,Lang,Version,Count,MsgPayload,Bytes,Millis,Async
- `;
- }
- };
- var Bench = class {
- constructor(nc, opts = {
- msgs: 1e5,
- size: 128,
- subject: "",
- asyncRequests: false,
- pub: false,
- sub: false,
- req: false,
- rep: false
- }) {
- __publicField(this, "nc");
- __publicField(this, "callbacks");
- __publicField(this, "msgs");
- __publicField(this, "size");
- __publicField(this, "subject");
- __publicField(this, "asyncRequests");
- __publicField(this, "pub");
- __publicField(this, "sub");
- __publicField(this, "req");
- __publicField(this, "rep");
- __publicField(this, "perf");
- __publicField(this, "payload");
- this.nc = nc;
- this.callbacks = opts.callbacks || false;
- this.msgs = opts.msgs || 0;
- this.size = opts.size || 0;
- this.subject = opts.subject || nuid.next();
- this.asyncRequests = opts.asyncRequests || false;
- this.pub = opts.pub || false;
- this.sub = opts.sub || false;
- this.req = opts.req || false;
- this.rep = opts.rep || false;
- this.perf = new Perf();
- this.payload = this.size ? new Uint8Array(this.size) : Empty;
- if (!this.pub && !this.sub && !this.req && !this.rep) {
- throw new Error("no bench option selected");
- }
- }
- async run() {
- this.nc.closed().then((err) => {
- if (err) {
- throw new NatsError(`bench closed with an error: ${err.message}`, ErrorCode.Unknown, err);
- }
- });
- if (this.callbacks) {
- await this.runCallbacks();
- } else {
- await this.runAsync();
- }
- return this.processMetrics();
- }
- processMetrics() {
- const nc = this.nc;
- const { lang, version } = nc.protocol.transport;
- if (this.pub && this.sub) {
- this.perf.measure("pubsub", "pubStart", "subStop");
- }
- if (this.req && this.rep) {
- this.perf.measure("reqrep", "reqStart", "reqStop");
- }
- const measures = this.perf.getEntries();
- const pubsub = measures.find((m) => m.name === "pubsub");
- const reqrep = measures.find((m) => m.name === "reqrep");
- const req = measures.find((m) => m.name === "req");
- const rep = measures.find((m) => m.name === "rep");
- const pub = measures.find((m) => m.name === "pub");
- const sub = measures.find((m) => m.name === "sub");
- const stats = this.nc.stats();
- const metrics = [];
- if (pubsub) {
- const { name, duration } = pubsub;
- const m = new Metric(name, duration);
- m.msgs = this.msgs * 2;
- m.bytes = stats.inBytes + stats.outBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- if (reqrep) {
- const { name, duration } = reqrep;
- const m = new Metric(name, duration);
- m.msgs = this.msgs * 2;
- m.bytes = stats.inBytes + stats.outBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- if (pub) {
- const { name, duration } = pub;
- const m = new Metric(name, duration);
- m.msgs = this.msgs;
- m.bytes = stats.outBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- if (sub) {
- const { name, duration } = sub;
- const m = new Metric(name, duration);
- m.msgs = this.msgs;
- m.bytes = stats.inBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- if (rep) {
- const { name, duration } = rep;
- const m = new Metric(name, duration);
- m.msgs = this.msgs;
- m.bytes = stats.inBytes + stats.outBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- if (req) {
- const { name, duration } = req;
- const m = new Metric(name, duration);
- m.msgs = this.msgs;
- m.bytes = stats.inBytes + stats.outBytes;
- m.lang = lang;
- m.version = version;
- m.payload = this.payload.length;
- metrics.push(m);
- }
- return metrics;
- }
- async runCallbacks() {
- const jobs = [];
- if (this.sub) {
- const d = deferred();
- jobs.push(d);
- let i = 0;
- this.nc.subscribe(this.subject, {
- max: this.msgs,
- callback: () => {
- i++;
- if (i === 1) {
- this.perf.mark("subStart");
- }
- if (i === this.msgs) {
- this.perf.mark("subStop");
- this.perf.measure("sub", "subStart", "subStop");
- d.resolve();
- }
- }
- });
- }
- if (this.rep) {
- const d = deferred();
- jobs.push(d);
- let i = 0;
- this.nc.subscribe(this.subject, {
- max: this.msgs,
- callback: (_, m) => {
- m.respond(this.payload);
- i++;
- if (i === 1) {
- this.perf.mark("repStart");
- }
- if (i === this.msgs) {
- this.perf.mark("repStop");
- this.perf.measure("rep", "repStart", "repStop");
- d.resolve();
- }
- }
- });
- }
- if (this.pub) {
- const job = (async () => {
- this.perf.mark("pubStart");
- for (let i = 0; i < this.msgs; i++) {
- this.nc.publish(this.subject, this.payload);
- }
- await this.nc.flush();
- this.perf.mark("pubStop");
- this.perf.measure("pub", "pubStart", "pubStop");
- })();
- jobs.push(job);
- }
- if (this.req) {
- const job = (async () => {
- if (this.asyncRequests) {
- this.perf.mark("reqStart");
- const a = [];
- for (let i = 0; i < this.msgs; i++) {
- a.push(this.nc.request(this.subject, this.payload, {
- timeout: 2e4
- }));
- }
- await Promise.all(a);
- this.perf.mark("reqStop");
- this.perf.measure("req", "reqStart", "reqStop");
- } else {
- this.perf.mark("reqStart");
- for (let i = 0; i < this.msgs; i++) {
- await this.nc.request(this.subject);
- }
- this.perf.mark("reqStop");
- this.perf.measure("req", "reqStart", "reqStop");
- }
- })();
- jobs.push(job);
- }
- await Promise.all(jobs);
- }
- async runAsync() {
- const jobs = [];
- if (this.rep) {
- let first = false;
- const sub = this.nc.subscribe(this.subject, {
- max: this.msgs
- });
- const job = (async () => {
- for await (const m of sub) {
- if (!first) {
- this.perf.mark("repStart");
- first = true;
- }
- m.respond(this.payload);
- }
- await this.nc.flush();
- this.perf.mark("repStop");
- this.perf.measure("rep", "repStart", "repStop");
- })();
- jobs.push(job);
- }
- if (this.sub) {
- let first = false;
- const sub = this.nc.subscribe(this.subject, {
- max: this.msgs
- });
- const job = (async () => {
- for await (const _m of sub) {
- if (!first) {
- this.perf.mark("subStart");
- first = true;
- }
- }
- this.perf.mark("subStop");
- this.perf.measure("sub", "subStart", "subStop");
- })();
- jobs.push(job);
- }
- if (this.pub) {
- const job = (async () => {
- this.perf.mark("pubStart");
- for (let i = 0; i < this.msgs; i++) {
- this.nc.publish(this.subject, this.payload);
- }
- await this.nc.flush();
- this.perf.mark("pubStop");
- this.perf.measure("pub", "pubStart", "pubStop");
- })();
- jobs.push(job);
- }
- if (this.req) {
- const job = (async () => {
- if (this.asyncRequests) {
- this.perf.mark("reqStart");
- const a = [];
- for (let i = 0; i < this.msgs; i++) {
- a.push(this.nc.request(this.subject, this.payload, {
- timeout: 2e4
- }));
- }
- await Promise.all(a);
- this.perf.mark("reqStop");
- this.perf.measure("req", "reqStart", "reqStop");
- } else {
- this.perf.mark("reqStart");
- for (let i = 0; i < this.msgs; i++) {
- await this.nc.request(this.subject);
- }
- this.perf.mark("reqStop");
- this.perf.measure("req", "reqStart", "reqStop");
- }
- })();
- jobs.push(job);
- }
- await Promise.all(jobs);
- }
- };
- function throughput(bytes, seconds) {
- return `${humanizeBytes(bytes / seconds)}/sec`;
- }
- function humanizeBytes(bytes, si = false) {
- const base2 = si ? 1e3 : 1024;
- const pre = si ? [
- "k",
- "M",
- "G",
- "T",
- "P",
- "E"
- ] : [
- "K",
- "M",
- "G",
- "T",
- "P",
- "E"
- ];
- const post = si ? "iB" : "B";
- if (bytes < base2) {
- return `${bytes.toFixed(2)} ${post}`;
- }
- const exp = parseInt(Math.log(bytes) / Math.log(base2) + "");
- const index = parseInt(exp - 1 + "");
- return `${(bytes / Math.pow(base2, exp)).toFixed(2)} ${pre[index]}${post}`;
- }
- function humanizeNumber(n) {
- return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- }
- var VERSION = "1.19.1";
- var LANG = "nats.ws";
- var WsTransport = class {
- constructor() {
- __publicField(this, "version");
- __publicField(this, "lang");
- __publicField(this, "closeError");
- __publicField(this, "connected");
- __publicField(this, "done");
- __publicField(this, "socket");
- __publicField(this, "options");
- __publicField(this, "socketClosed");
- __publicField(this, "encrypted");
- __publicField(this, "peeked");
- __publicField(this, "yields");
- __publicField(this, "signal");
- __publicField(this, "closedNotification");
- this.version = VERSION;
- this.lang = LANG;
- this.connected = false;
- this.done = false;
- this.socketClosed = false;
- this.encrypted = false;
- this.peeked = false;
- this.yields = [];
- this.signal = deferred();
- this.closedNotification = deferred();
- }
- async connect(server, options) {
- const connected = false;
- const connLock = deferred();
- if (options.tls) {
- connLock.reject(new NatsError("tls", ErrorCode.InvalidOption));
- return connLock;
- }
- this.options = options;
- const u = server.src;
- if (options.wsFactory) {
- const { socket, encrypted } = await options.wsFactory(server.src, options);
- this.socket = socket;
- this.encrypted = encrypted;
- } else {
- this.encrypted = u.indexOf("wss://") === 0;
- this.socket = new WebSocket(u);
- }
- this.socket.binaryType = "arraybuffer";
- this.socket.onopen = () => {
- if (this.isDiscarded()) {
- return;
- }
- };
- this.socket.onmessage = (me) => {
- if (this.isDiscarded()) {
- return;
- }
- this.yields.push(new Uint8Array(me.data));
- if (this.peeked) {
- this.signal.resolve();
- return;
- }
- const t = DataBuffer.concat(...this.yields);
- const pm = extractProtocolMessage(t);
- if (pm !== "") {
- const m = INFO.exec(pm);
- if (!m) {
- if (options.debug) {
- console.error("!!!", render(t));
- }
- connLock.reject(new Error("unexpected response from server"));
- return;
- }
- try {
- const info = JSON.parse(m[1]);
- checkOptions(info, this.options);
- this.peeked = true;
- this.connected = true;
- this.signal.resolve();
- connLock.resolve();
- } catch (err) {
- connLock.reject(err);
- return;
- }
- }
- };
- this.socket.onclose = (evt) => {
- if (this.isDiscarded()) {
- return;
- }
- this.socketClosed = true;
- let reason;
- if (this.done)
- return;
- if (!evt.wasClean) {
- reason = new Error(evt.reason);
- }
- this._closed(reason);
- };
- this.socket.onerror = (e) => {
- if (this.isDiscarded()) {
- return;
- }
- const evt = e;
- const err = new NatsError(evt.message, ErrorCode.Unknown, new Error(evt.error));
- if (!connected) {
- connLock.reject(err);
- } else {
- this._closed(err);
- }
- };
- return connLock;
- }
- disconnect() {
- this._closed(void 0, true);
- }
- async _closed(err, internal = true) {
- if (this.isDiscarded()) {
- return;
- }
- if (!this.connected)
- return;
- if (this.done)
- return;
- this.closeError = err;
- if (!err) {
- while (!this.socketClosed && this.socket.bufferedAmount > 0) {
- await delay(100);
- }
- }
- this.done = true;
- try {
- this.socket.close(err ? 1002 : 1e3, err ? err.message : void 0);
- } catch (err2) {
- }
- if (internal) {
- this.closedNotification.resolve(err);
- }
- }
- get isClosed() {
- return this.done;
- }
- [Symbol.asyncIterator]() {
- return this.iterate();
- }
- async *iterate() {
- while (true) {
- if (this.isDiscarded()) {
- return;
- }
- if (this.yields.length === 0) {
- await this.signal;
- }
- const yields = this.yields;
- this.yields = [];
- for (let i = 0; i < yields.length; i++) {
- if (this.options.debug) {
- console.info(`> ${render(yields[i])}`);
- }
- yield yields[i];
- }
- if (this.done) {
- break;
- } else if (this.yields.length === 0) {
- yields.length = 0;
- this.yields = yields;
- this.signal = deferred();
- }
- }
- }
- isEncrypted() {
- return this.connected && this.encrypted;
- }
- send(frame) {
- if (this.isDiscarded()) {
- return;
- }
- try {
- this.socket.send(frame.buffer);
- if (this.options.debug) {
- console.info(`< ${render(frame)}`);
- }
- return;
- } catch (err) {
- if (this.options.debug) {
- console.error(`!!! ${render(frame)}: ${err}`);
- }
- }
- }
- close(err) {
- return this._closed(err, false);
- }
- closed() {
- return this.closedNotification;
- }
- isDiscarded() {
- if (this.done) {
- this.discard();
- return true;
- }
- return false;
- }
- discard() {
- var _a;
- this.done = true;
- try {
- (_a = this.socket) == null ? void 0 : _a.close();
- } catch (_err) {
- }
- }
- };
- function wsUrlParseFn(u) {
- const ut = /^(.*:\/\/)(.*)/;
- if (!ut.test(u)) {
- u = `https://${u}`;
- }
- let url = new URL(u);
- const srcProto = url.protocol.toLowerCase();
- if (srcProto !== "https:" && srcProto !== "http") {
- u = u.replace(/^(.*:\/\/)(.*)/gm, "$2");
- url = new URL(`http://${u}`);
- }
- let protocol;
- let port;
- const host = url.hostname;
- const path = url.pathname;
- const search = url.search || "";
- switch (srcProto) {
- case "http:":
- case "ws:":
- case "nats:":
- port = url.port || "80";
- protocol = "ws:";
- break;
- default:
- port = url.port || "443";
- protocol = "wss:";
- break;
- }
- return `${protocol}//${host}:${port}${path}${search}`;
- }
- function connect(opts = {}) {
- setTransportFactory({
- defaultPort: 443,
- urlParseFn: wsUrlParseFn,
- factory: () => {
- return new WsTransport();
- }
- });
- return NatsConnectionImpl.connect(opts);
- }
- export {
- AckPolicy,
- AdvisoryKind,
- Bench,
- ConsumerDebugEvents,
- ConsumerEvents,
- DebugEvents,
- DeliverPolicy,
- DirectMsgHeaders,
- DiscardPolicy,
- Empty,
- ErrorCode,
- Events,
- JSONCodec,
- JsHeaders,
- Match,
- Metric,
- MsgHdrsImpl,
- NatsError,
- Nuid,
- ReplayPolicy,
- RepublishHeaders,
- RequestStrategy,
- RetentionPolicy,
- ServiceError,
- ServiceErrorCodeHeader,
- ServiceErrorHeader,
- ServiceResponseType,
- ServiceVerb,
- StorageType,
- StoreCompression,
- StringCodec,
- backoff,
- buildAuthenticator,
- canonicalMIMEHeaderKey,
- checkJsError,
- connect,
- consumerOpts,
- createInbox,
- credsAuthenticator,
- deadline,
- deferred,
- delay,
- headers,
- isFlowControlMsg,
- isHeartbeatMsg,
- jwtAuthenticator,
- millis,
- nanos,
- nkeyAuthenticator,
- mod as nkeys,
- nuid,
- syncIterator,
- tokenAuthenticator,
- usernamePasswordAuthenticator
- };
- //# sourceMappingURL=nats__ws.js.map
|