chunk-api-setup.11cfd513.mjs 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559
  1. import { promises } from 'fs';
  2. import { c as createBirpc } from './chunk-vite-node-client.da0a17ff.mjs';
  3. import require$$0$1 from 'stream';
  4. import require$$0 from 'zlib';
  5. import require$$3 from 'net';
  6. import require$$4 from 'tls';
  7. import require$$5 from 'crypto';
  8. import require$$2 from 'events';
  9. import require$$1 from 'https';
  10. import require$$2$1 from 'http';
  11. import url from 'url';
  12. import { A as API_PATH } from './chunk-constants.71e8a211.mjs';
  13. import { i as interpretSourcePos, p as parseStacktrace } from './chunk-utils-source-map.2be5aa48.mjs';
  14. import 'module';
  15. import 'vm';
  16. import './chunk-vite-node-utils.473cd0b2.mjs';
  17. import 'path';
  18. import 'assert';
  19. import 'util';
  20. import 'debug';
  21. import 'tty';
  22. import './chunk-mock-date.2917be60.mjs';
  23. import 'local-pkg';
  24. /*! (c) 2020 Andrea Giammarchi */
  25. const {parse: $parse, stringify: $stringify} = JSON;
  26. const {keys} = Object;
  27. const Primitive = String; // it could be Number
  28. const primitive = 'string'; // it could be 'number'
  29. const ignore = {};
  30. const object = 'object';
  31. const noop = (_, value) => value;
  32. const primitives = value => (
  33. value instanceof Primitive ? Primitive(value) : value
  34. );
  35. const Primitives = (_, value) => (
  36. typeof value === primitive ? new Primitive(value) : value
  37. );
  38. const revive = (input, parsed, output, $) => {
  39. const lazy = [];
  40. for (let ke = keys(output), {length} = ke, y = 0; y < length; y++) {
  41. const k = ke[y];
  42. const value = output[k];
  43. if (value instanceof Primitive) {
  44. const tmp = input[value];
  45. if (typeof tmp === object && !parsed.has(tmp)) {
  46. parsed.add(tmp);
  47. output[k] = ignore;
  48. lazy.push({k, a: [input, parsed, tmp, $]});
  49. }
  50. else
  51. output[k] = $.call(output, k, tmp);
  52. }
  53. else if (output[k] !== ignore)
  54. output[k] = $.call(output, k, value);
  55. }
  56. for (let {length} = lazy, i = 0; i < length; i++) {
  57. const {k, a} = lazy[i];
  58. output[k] = $.call(output, k, revive.apply(null, a));
  59. }
  60. return output;
  61. };
  62. const set = (known, input, value) => {
  63. const index = Primitive(input.push(value) - 1);
  64. known.set(value, index);
  65. return index;
  66. };
  67. const parse$3 = (text, reviver) => {
  68. const input = $parse(text, Primitives).map(primitives);
  69. const value = input[0];
  70. const $ = reviver || noop;
  71. const tmp = typeof value === object && value ?
  72. revive(input, new Set, value, $) :
  73. value;
  74. return $.call({'': tmp}, '', tmp);
  75. };
  76. const stringify = (value, replacer, space) => {
  77. const $ = replacer && typeof replacer === object ?
  78. (k, v) => (k === '' || -1 < replacer.indexOf(k) ? v : void 0) :
  79. (replacer || noop);
  80. const known = new Map;
  81. const input = [];
  82. const output = [];
  83. let i = +set(known, input, $.call({'': value}, '', value));
  84. let firstRun = !i;
  85. while (i < input.length) {
  86. firstRun = true;
  87. output[i] = $stringify(input[i++], replace, space);
  88. }
  89. return '[' + output.join(',') + ']';
  90. function replace(key, value) {
  91. if (firstRun) {
  92. firstRun = !firstRun;
  93. return value;
  94. }
  95. const after = $.call(this, key, value);
  96. switch (typeof after) {
  97. case object:
  98. if (after === null) return after;
  99. case primitive:
  100. return known.get(after) || set(known, input, after);
  101. }
  102. return after;
  103. }
  104. };
  105. var bufferUtil$1 = {exports: {}};
  106. var constants = {
  107. BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
  108. EMPTY_BUFFER: Buffer.alloc(0),
  109. GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
  110. kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
  111. kListener: Symbol('kListener'),
  112. kStatusCode: Symbol('status-code'),
  113. kWebSocket: Symbol('websocket'),
  114. NOOP: () => {}
  115. };
  116. var unmask$1;
  117. var mask;
  118. const { EMPTY_BUFFER: EMPTY_BUFFER$3 } = constants;
  119. /**
  120. * Merges an array of buffers into a new buffer.
  121. *
  122. * @param {Buffer[]} list The array of buffers to concat
  123. * @param {Number} totalLength The total length of buffers in the list
  124. * @return {Buffer} The resulting buffer
  125. * @public
  126. */
  127. function concat$1(list, totalLength) {
  128. if (list.length === 0) return EMPTY_BUFFER$3;
  129. if (list.length === 1) return list[0];
  130. const target = Buffer.allocUnsafe(totalLength);
  131. let offset = 0;
  132. for (let i = 0; i < list.length; i++) {
  133. const buf = list[i];
  134. target.set(buf, offset);
  135. offset += buf.length;
  136. }
  137. if (offset < totalLength) return target.slice(0, offset);
  138. return target;
  139. }
  140. /**
  141. * Masks a buffer using the given mask.
  142. *
  143. * @param {Buffer} source The buffer to mask
  144. * @param {Buffer} mask The mask to use
  145. * @param {Buffer} output The buffer where to store the result
  146. * @param {Number} offset The offset at which to start writing
  147. * @param {Number} length The number of bytes to mask.
  148. * @public
  149. */
  150. function _mask(source, mask, output, offset, length) {
  151. for (let i = 0; i < length; i++) {
  152. output[offset + i] = source[i] ^ mask[i & 3];
  153. }
  154. }
  155. /**
  156. * Unmasks a buffer using the given mask.
  157. *
  158. * @param {Buffer} buffer The buffer to unmask
  159. * @param {Buffer} mask The mask to use
  160. * @public
  161. */
  162. function _unmask(buffer, mask) {
  163. for (let i = 0; i < buffer.length; i++) {
  164. buffer[i] ^= mask[i & 3];
  165. }
  166. }
  167. /**
  168. * Converts a buffer to an `ArrayBuffer`.
  169. *
  170. * @param {Buffer} buf The buffer to convert
  171. * @return {ArrayBuffer} Converted buffer
  172. * @public
  173. */
  174. function toArrayBuffer$1(buf) {
  175. if (buf.byteLength === buf.buffer.byteLength) {
  176. return buf.buffer;
  177. }
  178. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
  179. }
  180. /**
  181. * Converts `data` to a `Buffer`.
  182. *
  183. * @param {*} data The data to convert
  184. * @return {Buffer} The buffer
  185. * @throws {TypeError}
  186. * @public
  187. */
  188. function toBuffer$2(data) {
  189. toBuffer$2.readOnly = true;
  190. if (Buffer.isBuffer(data)) return data;
  191. let buf;
  192. if (data instanceof ArrayBuffer) {
  193. buf = Buffer.from(data);
  194. } else if (ArrayBuffer.isView(data)) {
  195. buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  196. } else {
  197. buf = Buffer.from(data);
  198. toBuffer$2.readOnly = false;
  199. }
  200. return buf;
  201. }
  202. bufferUtil$1.exports = {
  203. concat: concat$1,
  204. mask: _mask,
  205. toArrayBuffer: toArrayBuffer$1,
  206. toBuffer: toBuffer$2,
  207. unmask: _unmask
  208. };
  209. /* istanbul ignore else */
  210. if (!process.env.WS_NO_BUFFER_UTIL) {
  211. try {
  212. const bufferUtil = require('bufferutil');
  213. mask = bufferUtil$1.exports.mask = function (source, mask, output, offset, length) {
  214. if (length < 48) _mask(source, mask, output, offset, length);
  215. else bufferUtil.mask(source, mask, output, offset, length);
  216. };
  217. unmask$1 = bufferUtil$1.exports.unmask = function (buffer, mask) {
  218. if (buffer.length < 32) _unmask(buffer, mask);
  219. else bufferUtil.unmask(buffer, mask);
  220. };
  221. } catch (e) {
  222. // Continue regardless of the error.
  223. }
  224. }
  225. const kDone = Symbol('kDone');
  226. const kRun = Symbol('kRun');
  227. /**
  228. * A very simple job queue with adjustable concurrency. Adapted from
  229. * https://github.com/STRML/async-limiter
  230. */
  231. class Limiter$1 {
  232. /**
  233. * Creates a new `Limiter`.
  234. *
  235. * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
  236. * to run concurrently
  237. */
  238. constructor(concurrency) {
  239. this[kDone] = () => {
  240. this.pending--;
  241. this[kRun]();
  242. };
  243. this.concurrency = concurrency || Infinity;
  244. this.jobs = [];
  245. this.pending = 0;
  246. }
  247. /**
  248. * Adds a job to the queue.
  249. *
  250. * @param {Function} job The job to run
  251. * @public
  252. */
  253. add(job) {
  254. this.jobs.push(job);
  255. this[kRun]();
  256. }
  257. /**
  258. * Removes a job from the queue and runs it if possible.
  259. *
  260. * @private
  261. */
  262. [kRun]() {
  263. if (this.pending === this.concurrency) return;
  264. if (this.jobs.length) {
  265. const job = this.jobs.shift();
  266. this.pending++;
  267. job(this[kDone]);
  268. }
  269. }
  270. }
  271. var limiter = Limiter$1;
  272. const zlib = require$$0;
  273. const bufferUtil = bufferUtil$1.exports;
  274. const Limiter = limiter;
  275. const { kStatusCode: kStatusCode$2 } = constants;
  276. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  277. const kPerMessageDeflate = Symbol('permessage-deflate');
  278. const kTotalLength = Symbol('total-length');
  279. const kCallback = Symbol('callback');
  280. const kBuffers = Symbol('buffers');
  281. const kError$1 = Symbol('error');
  282. //
  283. // We limit zlib concurrency, which prevents severe memory fragmentation
  284. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  285. // and https://github.com/websockets/ws/issues/1202
  286. //
  287. // Intentionally global; it's the global thread pool that's an issue.
  288. //
  289. let zlibLimiter;
  290. /**
  291. * permessage-deflate implementation.
  292. */
  293. class PerMessageDeflate$4 {
  294. /**
  295. * Creates a PerMessageDeflate instance.
  296. *
  297. * @param {Object} [options] Configuration options
  298. * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
  299. * for, or request, a custom client window size
  300. * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
  301. * acknowledge disabling of client context takeover
  302. * @param {Number} [options.concurrencyLimit=10] The number of concurrent
  303. * calls to zlib
  304. * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
  305. * use of a custom server window size
  306. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  307. * disabling of server context takeover
  308. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  309. * messages should not be compressed if context takeover is disabled
  310. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  311. * deflate
  312. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  313. * inflate
  314. * @param {Boolean} [isServer=false] Create the instance in either server or
  315. * client mode
  316. * @param {Number} [maxPayload=0] The maximum allowed message length
  317. */
  318. constructor(options, isServer, maxPayload) {
  319. this._maxPayload = maxPayload | 0;
  320. this._options = options || {};
  321. this._threshold =
  322. this._options.threshold !== undefined ? this._options.threshold : 1024;
  323. this._isServer = !!isServer;
  324. this._deflate = null;
  325. this._inflate = null;
  326. this.params = null;
  327. if (!zlibLimiter) {
  328. const concurrency =
  329. this._options.concurrencyLimit !== undefined
  330. ? this._options.concurrencyLimit
  331. : 10;
  332. zlibLimiter = new Limiter(concurrency);
  333. }
  334. }
  335. /**
  336. * @type {String}
  337. */
  338. static get extensionName() {
  339. return 'permessage-deflate';
  340. }
  341. /**
  342. * Create an extension negotiation offer.
  343. *
  344. * @return {Object} Extension parameters
  345. * @public
  346. */
  347. offer() {
  348. const params = {};
  349. if (this._options.serverNoContextTakeover) {
  350. params.server_no_context_takeover = true;
  351. }
  352. if (this._options.clientNoContextTakeover) {
  353. params.client_no_context_takeover = true;
  354. }
  355. if (this._options.serverMaxWindowBits) {
  356. params.server_max_window_bits = this._options.serverMaxWindowBits;
  357. }
  358. if (this._options.clientMaxWindowBits) {
  359. params.client_max_window_bits = this._options.clientMaxWindowBits;
  360. } else if (this._options.clientMaxWindowBits == null) {
  361. params.client_max_window_bits = true;
  362. }
  363. return params;
  364. }
  365. /**
  366. * Accept an extension negotiation offer/response.
  367. *
  368. * @param {Array} configurations The extension negotiation offers/reponse
  369. * @return {Object} Accepted configuration
  370. * @public
  371. */
  372. accept(configurations) {
  373. configurations = this.normalizeParams(configurations);
  374. this.params = this._isServer
  375. ? this.acceptAsServer(configurations)
  376. : this.acceptAsClient(configurations);
  377. return this.params;
  378. }
  379. /**
  380. * Releases all resources used by the extension.
  381. *
  382. * @public
  383. */
  384. cleanup() {
  385. if (this._inflate) {
  386. this._inflate.close();
  387. this._inflate = null;
  388. }
  389. if (this._deflate) {
  390. const callback = this._deflate[kCallback];
  391. this._deflate.close();
  392. this._deflate = null;
  393. if (callback) {
  394. callback(
  395. new Error(
  396. 'The deflate stream was closed while data was being processed'
  397. )
  398. );
  399. }
  400. }
  401. }
  402. /**
  403. * Accept an extension negotiation offer.
  404. *
  405. * @param {Array} offers The extension negotiation offers
  406. * @return {Object} Accepted configuration
  407. * @private
  408. */
  409. acceptAsServer(offers) {
  410. const opts = this._options;
  411. const accepted = offers.find((params) => {
  412. if (
  413. (opts.serverNoContextTakeover === false &&
  414. params.server_no_context_takeover) ||
  415. (params.server_max_window_bits &&
  416. (opts.serverMaxWindowBits === false ||
  417. (typeof opts.serverMaxWindowBits === 'number' &&
  418. opts.serverMaxWindowBits > params.server_max_window_bits))) ||
  419. (typeof opts.clientMaxWindowBits === 'number' &&
  420. !params.client_max_window_bits)
  421. ) {
  422. return false;
  423. }
  424. return true;
  425. });
  426. if (!accepted) {
  427. throw new Error('None of the extension offers can be accepted');
  428. }
  429. if (opts.serverNoContextTakeover) {
  430. accepted.server_no_context_takeover = true;
  431. }
  432. if (opts.clientNoContextTakeover) {
  433. accepted.client_no_context_takeover = true;
  434. }
  435. if (typeof opts.serverMaxWindowBits === 'number') {
  436. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  437. }
  438. if (typeof opts.clientMaxWindowBits === 'number') {
  439. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  440. } else if (
  441. accepted.client_max_window_bits === true ||
  442. opts.clientMaxWindowBits === false
  443. ) {
  444. delete accepted.client_max_window_bits;
  445. }
  446. return accepted;
  447. }
  448. /**
  449. * Accept the extension negotiation response.
  450. *
  451. * @param {Array} response The extension negotiation response
  452. * @return {Object} Accepted configuration
  453. * @private
  454. */
  455. acceptAsClient(response) {
  456. const params = response[0];
  457. if (
  458. this._options.clientNoContextTakeover === false &&
  459. params.client_no_context_takeover
  460. ) {
  461. throw new Error('Unexpected parameter "client_no_context_takeover"');
  462. }
  463. if (!params.client_max_window_bits) {
  464. if (typeof this._options.clientMaxWindowBits === 'number') {
  465. params.client_max_window_bits = this._options.clientMaxWindowBits;
  466. }
  467. } else if (
  468. this._options.clientMaxWindowBits === false ||
  469. (typeof this._options.clientMaxWindowBits === 'number' &&
  470. params.client_max_window_bits > this._options.clientMaxWindowBits)
  471. ) {
  472. throw new Error(
  473. 'Unexpected or invalid parameter "client_max_window_bits"'
  474. );
  475. }
  476. return params;
  477. }
  478. /**
  479. * Normalize parameters.
  480. *
  481. * @param {Array} configurations The extension negotiation offers/reponse
  482. * @return {Array} The offers/response with normalized parameters
  483. * @private
  484. */
  485. normalizeParams(configurations) {
  486. configurations.forEach((params) => {
  487. Object.keys(params).forEach((key) => {
  488. let value = params[key];
  489. if (value.length > 1) {
  490. throw new Error(`Parameter "${key}" must have only a single value`);
  491. }
  492. value = value[0];
  493. if (key === 'client_max_window_bits') {
  494. if (value !== true) {
  495. const num = +value;
  496. if (!Number.isInteger(num) || num < 8 || num > 15) {
  497. throw new TypeError(
  498. `Invalid value for parameter "${key}": ${value}`
  499. );
  500. }
  501. value = num;
  502. } else if (!this._isServer) {
  503. throw new TypeError(
  504. `Invalid value for parameter "${key}": ${value}`
  505. );
  506. }
  507. } else if (key === 'server_max_window_bits') {
  508. const num = +value;
  509. if (!Number.isInteger(num) || num < 8 || num > 15) {
  510. throw new TypeError(
  511. `Invalid value for parameter "${key}": ${value}`
  512. );
  513. }
  514. value = num;
  515. } else if (
  516. key === 'client_no_context_takeover' ||
  517. key === 'server_no_context_takeover'
  518. ) {
  519. if (value !== true) {
  520. throw new TypeError(
  521. `Invalid value for parameter "${key}": ${value}`
  522. );
  523. }
  524. } else {
  525. throw new Error(`Unknown parameter "${key}"`);
  526. }
  527. params[key] = value;
  528. });
  529. });
  530. return configurations;
  531. }
  532. /**
  533. * Decompress data. Concurrency limited.
  534. *
  535. * @param {Buffer} data Compressed data
  536. * @param {Boolean} fin Specifies whether or not this is the last fragment
  537. * @param {Function} callback Callback
  538. * @public
  539. */
  540. decompress(data, fin, callback) {
  541. zlibLimiter.add((done) => {
  542. this._decompress(data, fin, (err, result) => {
  543. done();
  544. callback(err, result);
  545. });
  546. });
  547. }
  548. /**
  549. * Compress data. Concurrency limited.
  550. *
  551. * @param {(Buffer|String)} data Data to compress
  552. * @param {Boolean} fin Specifies whether or not this is the last fragment
  553. * @param {Function} callback Callback
  554. * @public
  555. */
  556. compress(data, fin, callback) {
  557. zlibLimiter.add((done) => {
  558. this._compress(data, fin, (err, result) => {
  559. done();
  560. callback(err, result);
  561. });
  562. });
  563. }
  564. /**
  565. * Decompress data.
  566. *
  567. * @param {Buffer} data Compressed data
  568. * @param {Boolean} fin Specifies whether or not this is the last fragment
  569. * @param {Function} callback Callback
  570. * @private
  571. */
  572. _decompress(data, fin, callback) {
  573. const endpoint = this._isServer ? 'client' : 'server';
  574. if (!this._inflate) {
  575. const key = `${endpoint}_max_window_bits`;
  576. const windowBits =
  577. typeof this.params[key] !== 'number'
  578. ? zlib.Z_DEFAULT_WINDOWBITS
  579. : this.params[key];
  580. this._inflate = zlib.createInflateRaw({
  581. ...this._options.zlibInflateOptions,
  582. windowBits
  583. });
  584. this._inflate[kPerMessageDeflate] = this;
  585. this._inflate[kTotalLength] = 0;
  586. this._inflate[kBuffers] = [];
  587. this._inflate.on('error', inflateOnError);
  588. this._inflate.on('data', inflateOnData);
  589. }
  590. this._inflate[kCallback] = callback;
  591. this._inflate.write(data);
  592. if (fin) this._inflate.write(TRAILER);
  593. this._inflate.flush(() => {
  594. const err = this._inflate[kError$1];
  595. if (err) {
  596. this._inflate.close();
  597. this._inflate = null;
  598. callback(err);
  599. return;
  600. }
  601. const data = bufferUtil.concat(
  602. this._inflate[kBuffers],
  603. this._inflate[kTotalLength]
  604. );
  605. if (this._inflate._readableState.endEmitted) {
  606. this._inflate.close();
  607. this._inflate = null;
  608. } else {
  609. this._inflate[kTotalLength] = 0;
  610. this._inflate[kBuffers] = [];
  611. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  612. this._inflate.reset();
  613. }
  614. }
  615. callback(null, data);
  616. });
  617. }
  618. /**
  619. * Compress data.
  620. *
  621. * @param {(Buffer|String)} data Data to compress
  622. * @param {Boolean} fin Specifies whether or not this is the last fragment
  623. * @param {Function} callback Callback
  624. * @private
  625. */
  626. _compress(data, fin, callback) {
  627. const endpoint = this._isServer ? 'server' : 'client';
  628. if (!this._deflate) {
  629. const key = `${endpoint}_max_window_bits`;
  630. const windowBits =
  631. typeof this.params[key] !== 'number'
  632. ? zlib.Z_DEFAULT_WINDOWBITS
  633. : this.params[key];
  634. this._deflate = zlib.createDeflateRaw({
  635. ...this._options.zlibDeflateOptions,
  636. windowBits
  637. });
  638. this._deflate[kTotalLength] = 0;
  639. this._deflate[kBuffers] = [];
  640. this._deflate.on('data', deflateOnData);
  641. }
  642. this._deflate[kCallback] = callback;
  643. this._deflate.write(data);
  644. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  645. if (!this._deflate) {
  646. //
  647. // The deflate stream was closed while data was being processed.
  648. //
  649. return;
  650. }
  651. let data = bufferUtil.concat(
  652. this._deflate[kBuffers],
  653. this._deflate[kTotalLength]
  654. );
  655. if (fin) data = data.slice(0, data.length - 4);
  656. //
  657. // Ensure that the callback will not be called again in
  658. // `PerMessageDeflate#cleanup()`.
  659. //
  660. this._deflate[kCallback] = null;
  661. this._deflate[kTotalLength] = 0;
  662. this._deflate[kBuffers] = [];
  663. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  664. this._deflate.reset();
  665. }
  666. callback(null, data);
  667. });
  668. }
  669. }
  670. var permessageDeflate = PerMessageDeflate$4;
  671. /**
  672. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  673. *
  674. * @param {Buffer} chunk A chunk of data
  675. * @private
  676. */
  677. function deflateOnData(chunk) {
  678. this[kBuffers].push(chunk);
  679. this[kTotalLength] += chunk.length;
  680. }
  681. /**
  682. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  683. *
  684. * @param {Buffer} chunk A chunk of data
  685. * @private
  686. */
  687. function inflateOnData(chunk) {
  688. this[kTotalLength] += chunk.length;
  689. if (
  690. this[kPerMessageDeflate]._maxPayload < 1 ||
  691. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  692. ) {
  693. this[kBuffers].push(chunk);
  694. return;
  695. }
  696. this[kError$1] = new RangeError('Max payload size exceeded');
  697. this[kError$1].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  698. this[kError$1][kStatusCode$2] = 1009;
  699. this.removeListener('data', inflateOnData);
  700. this.reset();
  701. }
  702. /**
  703. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  704. *
  705. * @param {Error} err The emitted error
  706. * @private
  707. */
  708. function inflateOnError(err) {
  709. //
  710. // There is no need to call `Zlib#close()` as the handle is automatically
  711. // closed when an error is emitted.
  712. //
  713. this[kPerMessageDeflate]._inflate = null;
  714. err[kStatusCode$2] = 1007;
  715. this[kCallback](err);
  716. }
  717. var validation = {exports: {}};
  718. var isValidUTF8_1;
  719. //
  720. // Allowed token characters:
  721. //
  722. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  723. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  724. //
  725. // tokenChars[32] === 0 // ' '
  726. // tokenChars[33] === 1 // '!'
  727. // tokenChars[34] === 0 // '"'
  728. // ...
  729. //
  730. // prettier-ignore
  731. const tokenChars$2 = [
  732. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  733. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  734. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  735. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  736. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  737. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  738. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  739. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  740. ];
  741. /**
  742. * Checks if a status code is allowed in a close frame.
  743. *
  744. * @param {Number} code The status code
  745. * @return {Boolean} `true` if the status code is valid, else `false`
  746. * @public
  747. */
  748. function isValidStatusCode$2(code) {
  749. return (
  750. (code >= 1000 &&
  751. code <= 1014 &&
  752. code !== 1004 &&
  753. code !== 1005 &&
  754. code !== 1006) ||
  755. (code >= 3000 && code <= 4999)
  756. );
  757. }
  758. /**
  759. * Checks if a given buffer contains only correct UTF-8.
  760. * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
  761. * Markus Kuhn.
  762. *
  763. * @param {Buffer} buf The buffer to check
  764. * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
  765. * @public
  766. */
  767. function _isValidUTF8(buf) {
  768. const len = buf.length;
  769. let i = 0;
  770. while (i < len) {
  771. if ((buf[i] & 0x80) === 0) {
  772. // 0xxxxxxx
  773. i++;
  774. } else if ((buf[i] & 0xe0) === 0xc0) {
  775. // 110xxxxx 10xxxxxx
  776. if (
  777. i + 1 === len ||
  778. (buf[i + 1] & 0xc0) !== 0x80 ||
  779. (buf[i] & 0xfe) === 0xc0 // Overlong
  780. ) {
  781. return false;
  782. }
  783. i += 2;
  784. } else if ((buf[i] & 0xf0) === 0xe0) {
  785. // 1110xxxx 10xxxxxx 10xxxxxx
  786. if (
  787. i + 2 >= len ||
  788. (buf[i + 1] & 0xc0) !== 0x80 ||
  789. (buf[i + 2] & 0xc0) !== 0x80 ||
  790. (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
  791. (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
  792. ) {
  793. return false;
  794. }
  795. i += 3;
  796. } else if ((buf[i] & 0xf8) === 0xf0) {
  797. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  798. if (
  799. i + 3 >= len ||
  800. (buf[i + 1] & 0xc0) !== 0x80 ||
  801. (buf[i + 2] & 0xc0) !== 0x80 ||
  802. (buf[i + 3] & 0xc0) !== 0x80 ||
  803. (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
  804. (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
  805. buf[i] > 0xf4 // > U+10FFFF
  806. ) {
  807. return false;
  808. }
  809. i += 4;
  810. } else {
  811. return false;
  812. }
  813. }
  814. return true;
  815. }
  816. validation.exports = {
  817. isValidStatusCode: isValidStatusCode$2,
  818. isValidUTF8: _isValidUTF8,
  819. tokenChars: tokenChars$2
  820. };
  821. /* istanbul ignore else */
  822. if (!process.env.WS_NO_UTF_8_VALIDATE) {
  823. try {
  824. const isValidUTF8 = require('utf-8-validate');
  825. isValidUTF8_1 = validation.exports.isValidUTF8 = function (buf) {
  826. return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
  827. };
  828. } catch (e) {
  829. // Continue regardless of the error.
  830. }
  831. }
  832. const { Writable } = require$$0$1;
  833. const PerMessageDeflate$3 = permessageDeflate;
  834. const {
  835. BINARY_TYPES: BINARY_TYPES$1,
  836. EMPTY_BUFFER: EMPTY_BUFFER$2,
  837. kStatusCode: kStatusCode$1,
  838. kWebSocket: kWebSocket$2
  839. } = constants;
  840. const { concat, toArrayBuffer, unmask } = bufferUtil$1.exports;
  841. const { isValidStatusCode: isValidStatusCode$1, isValidUTF8 } = validation.exports;
  842. const GET_INFO = 0;
  843. const GET_PAYLOAD_LENGTH_16 = 1;
  844. const GET_PAYLOAD_LENGTH_64 = 2;
  845. const GET_MASK = 3;
  846. const GET_DATA = 4;
  847. const INFLATING = 5;
  848. /**
  849. * HyBi Receiver implementation.
  850. *
  851. * @extends Writable
  852. */
  853. class Receiver$1 extends Writable {
  854. /**
  855. * Creates a Receiver instance.
  856. *
  857. * @param {Object} [options] Options object
  858. * @param {String} [options.binaryType=nodebuffer] The type for binary data
  859. * @param {Object} [options.extensions] An object containing the negotiated
  860. * extensions
  861. * @param {Boolean} [options.isServer=false] Specifies whether to operate in
  862. * client or server mode
  863. * @param {Number} [options.maxPayload=0] The maximum allowed message length
  864. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  865. * not to skip UTF-8 validation for text and close messages
  866. */
  867. constructor(options = {}) {
  868. super();
  869. this._binaryType = options.binaryType || BINARY_TYPES$1[0];
  870. this._extensions = options.extensions || {};
  871. this._isServer = !!options.isServer;
  872. this._maxPayload = options.maxPayload | 0;
  873. this._skipUTF8Validation = !!options.skipUTF8Validation;
  874. this[kWebSocket$2] = undefined;
  875. this._bufferedBytes = 0;
  876. this._buffers = [];
  877. this._compressed = false;
  878. this._payloadLength = 0;
  879. this._mask = undefined;
  880. this._fragmented = 0;
  881. this._masked = false;
  882. this._fin = false;
  883. this._opcode = 0;
  884. this._totalPayloadLength = 0;
  885. this._messageLength = 0;
  886. this._fragments = [];
  887. this._state = GET_INFO;
  888. this._loop = false;
  889. }
  890. /**
  891. * Implements `Writable.prototype._write()`.
  892. *
  893. * @param {Buffer} chunk The chunk of data to write
  894. * @param {String} encoding The character encoding of `chunk`
  895. * @param {Function} cb Callback
  896. * @private
  897. */
  898. _write(chunk, encoding, cb) {
  899. if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
  900. this._bufferedBytes += chunk.length;
  901. this._buffers.push(chunk);
  902. this.startLoop(cb);
  903. }
  904. /**
  905. * Consumes `n` bytes from the buffered data.
  906. *
  907. * @param {Number} n The number of bytes to consume
  908. * @return {Buffer} The consumed bytes
  909. * @private
  910. */
  911. consume(n) {
  912. this._bufferedBytes -= n;
  913. if (n === this._buffers[0].length) return this._buffers.shift();
  914. if (n < this._buffers[0].length) {
  915. const buf = this._buffers[0];
  916. this._buffers[0] = buf.slice(n);
  917. return buf.slice(0, n);
  918. }
  919. const dst = Buffer.allocUnsafe(n);
  920. do {
  921. const buf = this._buffers[0];
  922. const offset = dst.length - n;
  923. if (n >= buf.length) {
  924. dst.set(this._buffers.shift(), offset);
  925. } else {
  926. dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
  927. this._buffers[0] = buf.slice(n);
  928. }
  929. n -= buf.length;
  930. } while (n > 0);
  931. return dst;
  932. }
  933. /**
  934. * Starts the parsing loop.
  935. *
  936. * @param {Function} cb Callback
  937. * @private
  938. */
  939. startLoop(cb) {
  940. let err;
  941. this._loop = true;
  942. do {
  943. switch (this._state) {
  944. case GET_INFO:
  945. err = this.getInfo();
  946. break;
  947. case GET_PAYLOAD_LENGTH_16:
  948. err = this.getPayloadLength16();
  949. break;
  950. case GET_PAYLOAD_LENGTH_64:
  951. err = this.getPayloadLength64();
  952. break;
  953. case GET_MASK:
  954. this.getMask();
  955. break;
  956. case GET_DATA:
  957. err = this.getData(cb);
  958. break;
  959. default:
  960. // `INFLATING`
  961. this._loop = false;
  962. return;
  963. }
  964. } while (this._loop);
  965. cb(err);
  966. }
  967. /**
  968. * Reads the first two bytes of a frame.
  969. *
  970. * @return {(RangeError|undefined)} A possible error
  971. * @private
  972. */
  973. getInfo() {
  974. if (this._bufferedBytes < 2) {
  975. this._loop = false;
  976. return;
  977. }
  978. const buf = this.consume(2);
  979. if ((buf[0] & 0x30) !== 0x00) {
  980. this._loop = false;
  981. return error(
  982. RangeError,
  983. 'RSV2 and RSV3 must be clear',
  984. true,
  985. 1002,
  986. 'WS_ERR_UNEXPECTED_RSV_2_3'
  987. );
  988. }
  989. const compressed = (buf[0] & 0x40) === 0x40;
  990. if (compressed && !this._extensions[PerMessageDeflate$3.extensionName]) {
  991. this._loop = false;
  992. return error(
  993. RangeError,
  994. 'RSV1 must be clear',
  995. true,
  996. 1002,
  997. 'WS_ERR_UNEXPECTED_RSV_1'
  998. );
  999. }
  1000. this._fin = (buf[0] & 0x80) === 0x80;
  1001. this._opcode = buf[0] & 0x0f;
  1002. this._payloadLength = buf[1] & 0x7f;
  1003. if (this._opcode === 0x00) {
  1004. if (compressed) {
  1005. this._loop = false;
  1006. return error(
  1007. RangeError,
  1008. 'RSV1 must be clear',
  1009. true,
  1010. 1002,
  1011. 'WS_ERR_UNEXPECTED_RSV_1'
  1012. );
  1013. }
  1014. if (!this._fragmented) {
  1015. this._loop = false;
  1016. return error(
  1017. RangeError,
  1018. 'invalid opcode 0',
  1019. true,
  1020. 1002,
  1021. 'WS_ERR_INVALID_OPCODE'
  1022. );
  1023. }
  1024. this._opcode = this._fragmented;
  1025. } else if (this._opcode === 0x01 || this._opcode === 0x02) {
  1026. if (this._fragmented) {
  1027. this._loop = false;
  1028. return error(
  1029. RangeError,
  1030. `invalid opcode ${this._opcode}`,
  1031. true,
  1032. 1002,
  1033. 'WS_ERR_INVALID_OPCODE'
  1034. );
  1035. }
  1036. this._compressed = compressed;
  1037. } else if (this._opcode > 0x07 && this._opcode < 0x0b) {
  1038. if (!this._fin) {
  1039. this._loop = false;
  1040. return error(
  1041. RangeError,
  1042. 'FIN must be set',
  1043. true,
  1044. 1002,
  1045. 'WS_ERR_EXPECTED_FIN'
  1046. );
  1047. }
  1048. if (compressed) {
  1049. this._loop = false;
  1050. return error(
  1051. RangeError,
  1052. 'RSV1 must be clear',
  1053. true,
  1054. 1002,
  1055. 'WS_ERR_UNEXPECTED_RSV_1'
  1056. );
  1057. }
  1058. if (this._payloadLength > 0x7d) {
  1059. this._loop = false;
  1060. return error(
  1061. RangeError,
  1062. `invalid payload length ${this._payloadLength}`,
  1063. true,
  1064. 1002,
  1065. 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
  1066. );
  1067. }
  1068. } else {
  1069. this._loop = false;
  1070. return error(
  1071. RangeError,
  1072. `invalid opcode ${this._opcode}`,
  1073. true,
  1074. 1002,
  1075. 'WS_ERR_INVALID_OPCODE'
  1076. );
  1077. }
  1078. if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
  1079. this._masked = (buf[1] & 0x80) === 0x80;
  1080. if (this._isServer) {
  1081. if (!this._masked) {
  1082. this._loop = false;
  1083. return error(
  1084. RangeError,
  1085. 'MASK must be set',
  1086. true,
  1087. 1002,
  1088. 'WS_ERR_EXPECTED_MASK'
  1089. );
  1090. }
  1091. } else if (this._masked) {
  1092. this._loop = false;
  1093. return error(
  1094. RangeError,
  1095. 'MASK must be clear',
  1096. true,
  1097. 1002,
  1098. 'WS_ERR_UNEXPECTED_MASK'
  1099. );
  1100. }
  1101. if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
  1102. else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
  1103. else return this.haveLength();
  1104. }
  1105. /**
  1106. * Gets extended payload length (7+16).
  1107. *
  1108. * @return {(RangeError|undefined)} A possible error
  1109. * @private
  1110. */
  1111. getPayloadLength16() {
  1112. if (this._bufferedBytes < 2) {
  1113. this._loop = false;
  1114. return;
  1115. }
  1116. this._payloadLength = this.consume(2).readUInt16BE(0);
  1117. return this.haveLength();
  1118. }
  1119. /**
  1120. * Gets extended payload length (7+64).
  1121. *
  1122. * @return {(RangeError|undefined)} A possible error
  1123. * @private
  1124. */
  1125. getPayloadLength64() {
  1126. if (this._bufferedBytes < 8) {
  1127. this._loop = false;
  1128. return;
  1129. }
  1130. const buf = this.consume(8);
  1131. const num = buf.readUInt32BE(0);
  1132. //
  1133. // The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
  1134. // if payload length is greater than this number.
  1135. //
  1136. if (num > Math.pow(2, 53 - 32) - 1) {
  1137. this._loop = false;
  1138. return error(
  1139. RangeError,
  1140. 'Unsupported WebSocket frame: payload length > 2^53 - 1',
  1141. false,
  1142. 1009,
  1143. 'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
  1144. );
  1145. }
  1146. this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
  1147. return this.haveLength();
  1148. }
  1149. /**
  1150. * Payload length has been read.
  1151. *
  1152. * @return {(RangeError|undefined)} A possible error
  1153. * @private
  1154. */
  1155. haveLength() {
  1156. if (this._payloadLength && this._opcode < 0x08) {
  1157. this._totalPayloadLength += this._payloadLength;
  1158. if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
  1159. this._loop = false;
  1160. return error(
  1161. RangeError,
  1162. 'Max payload size exceeded',
  1163. false,
  1164. 1009,
  1165. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  1166. );
  1167. }
  1168. }
  1169. if (this._masked) this._state = GET_MASK;
  1170. else this._state = GET_DATA;
  1171. }
  1172. /**
  1173. * Reads mask bytes.
  1174. *
  1175. * @private
  1176. */
  1177. getMask() {
  1178. if (this._bufferedBytes < 4) {
  1179. this._loop = false;
  1180. return;
  1181. }
  1182. this._mask = this.consume(4);
  1183. this._state = GET_DATA;
  1184. }
  1185. /**
  1186. * Reads data bytes.
  1187. *
  1188. * @param {Function} cb Callback
  1189. * @return {(Error|RangeError|undefined)} A possible error
  1190. * @private
  1191. */
  1192. getData(cb) {
  1193. let data = EMPTY_BUFFER$2;
  1194. if (this._payloadLength) {
  1195. if (this._bufferedBytes < this._payloadLength) {
  1196. this._loop = false;
  1197. return;
  1198. }
  1199. data = this.consume(this._payloadLength);
  1200. if (
  1201. this._masked &&
  1202. (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0
  1203. ) {
  1204. unmask(data, this._mask);
  1205. }
  1206. }
  1207. if (this._opcode > 0x07) return this.controlMessage(data);
  1208. if (this._compressed) {
  1209. this._state = INFLATING;
  1210. this.decompress(data, cb);
  1211. return;
  1212. }
  1213. if (data.length) {
  1214. //
  1215. // This message is not compressed so its length is the sum of the payload
  1216. // length of all fragments.
  1217. //
  1218. this._messageLength = this._totalPayloadLength;
  1219. this._fragments.push(data);
  1220. }
  1221. return this.dataMessage();
  1222. }
  1223. /**
  1224. * Decompresses data.
  1225. *
  1226. * @param {Buffer} data Compressed data
  1227. * @param {Function} cb Callback
  1228. * @private
  1229. */
  1230. decompress(data, cb) {
  1231. const perMessageDeflate = this._extensions[PerMessageDeflate$3.extensionName];
  1232. perMessageDeflate.decompress(data, this._fin, (err, buf) => {
  1233. if (err) return cb(err);
  1234. if (buf.length) {
  1235. this._messageLength += buf.length;
  1236. if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
  1237. return cb(
  1238. error(
  1239. RangeError,
  1240. 'Max payload size exceeded',
  1241. false,
  1242. 1009,
  1243. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  1244. )
  1245. );
  1246. }
  1247. this._fragments.push(buf);
  1248. }
  1249. const er = this.dataMessage();
  1250. if (er) return cb(er);
  1251. this.startLoop(cb);
  1252. });
  1253. }
  1254. /**
  1255. * Handles a data message.
  1256. *
  1257. * @return {(Error|undefined)} A possible error
  1258. * @private
  1259. */
  1260. dataMessage() {
  1261. if (this._fin) {
  1262. const messageLength = this._messageLength;
  1263. const fragments = this._fragments;
  1264. this._totalPayloadLength = 0;
  1265. this._messageLength = 0;
  1266. this._fragmented = 0;
  1267. this._fragments = [];
  1268. if (this._opcode === 2) {
  1269. let data;
  1270. if (this._binaryType === 'nodebuffer') {
  1271. data = concat(fragments, messageLength);
  1272. } else if (this._binaryType === 'arraybuffer') {
  1273. data = toArrayBuffer(concat(fragments, messageLength));
  1274. } else {
  1275. data = fragments;
  1276. }
  1277. this.emit('message', data, true);
  1278. } else {
  1279. const buf = concat(fragments, messageLength);
  1280. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  1281. this._loop = false;
  1282. return error(
  1283. Error,
  1284. 'invalid UTF-8 sequence',
  1285. true,
  1286. 1007,
  1287. 'WS_ERR_INVALID_UTF8'
  1288. );
  1289. }
  1290. this.emit('message', buf, false);
  1291. }
  1292. }
  1293. this._state = GET_INFO;
  1294. }
  1295. /**
  1296. * Handles a control message.
  1297. *
  1298. * @param {Buffer} data Data to handle
  1299. * @return {(Error|RangeError|undefined)} A possible error
  1300. * @private
  1301. */
  1302. controlMessage(data) {
  1303. if (this._opcode === 0x08) {
  1304. this._loop = false;
  1305. if (data.length === 0) {
  1306. this.emit('conclude', 1005, EMPTY_BUFFER$2);
  1307. this.end();
  1308. } else if (data.length === 1) {
  1309. return error(
  1310. RangeError,
  1311. 'invalid payload length 1',
  1312. true,
  1313. 1002,
  1314. 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
  1315. );
  1316. } else {
  1317. const code = data.readUInt16BE(0);
  1318. if (!isValidStatusCode$1(code)) {
  1319. return error(
  1320. RangeError,
  1321. `invalid status code ${code}`,
  1322. true,
  1323. 1002,
  1324. 'WS_ERR_INVALID_CLOSE_CODE'
  1325. );
  1326. }
  1327. const buf = data.slice(2);
  1328. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  1329. return error(
  1330. Error,
  1331. 'invalid UTF-8 sequence',
  1332. true,
  1333. 1007,
  1334. 'WS_ERR_INVALID_UTF8'
  1335. );
  1336. }
  1337. this.emit('conclude', code, buf);
  1338. this.end();
  1339. }
  1340. } else if (this._opcode === 0x09) {
  1341. this.emit('ping', data);
  1342. } else {
  1343. this.emit('pong', data);
  1344. }
  1345. this._state = GET_INFO;
  1346. }
  1347. }
  1348. var receiver = Receiver$1;
  1349. /**
  1350. * Builds an error object.
  1351. *
  1352. * @param {function(new:Error|RangeError)} ErrorCtor The error constructor
  1353. * @param {String} message The error message
  1354. * @param {Boolean} prefix Specifies whether or not to add a default prefix to
  1355. * `message`
  1356. * @param {Number} statusCode The status code
  1357. * @param {String} errorCode The exposed error code
  1358. * @return {(Error|RangeError)} The error
  1359. * @private
  1360. */
  1361. function error(ErrorCtor, message, prefix, statusCode, errorCode) {
  1362. const err = new ErrorCtor(
  1363. prefix ? `Invalid WebSocket frame: ${message}` : message
  1364. );
  1365. Error.captureStackTrace(err, error);
  1366. err.code = errorCode;
  1367. err[kStatusCode$1] = statusCode;
  1368. return err;
  1369. }
  1370. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
  1371. const { randomFillSync } = require$$5;
  1372. const PerMessageDeflate$2 = permessageDeflate;
  1373. const { EMPTY_BUFFER: EMPTY_BUFFER$1 } = constants;
  1374. const { isValidStatusCode } = validation.exports;
  1375. const { mask: applyMask, toBuffer: toBuffer$1 } = bufferUtil$1.exports;
  1376. const kByteLength = Symbol('kByteLength');
  1377. const maskBuffer = Buffer.alloc(4);
  1378. /**
  1379. * HyBi Sender implementation.
  1380. */
  1381. class Sender$1 {
  1382. /**
  1383. * Creates a Sender instance.
  1384. *
  1385. * @param {(net.Socket|tls.Socket)} socket The connection socket
  1386. * @param {Object} [extensions] An object containing the negotiated extensions
  1387. * @param {Function} [generateMask] The function used to generate the masking
  1388. * key
  1389. */
  1390. constructor(socket, extensions, generateMask) {
  1391. this._extensions = extensions || {};
  1392. if (generateMask) {
  1393. this._generateMask = generateMask;
  1394. this._maskBuffer = Buffer.alloc(4);
  1395. }
  1396. this._socket = socket;
  1397. this._firstFragment = true;
  1398. this._compress = false;
  1399. this._bufferedBytes = 0;
  1400. this._deflating = false;
  1401. this._queue = [];
  1402. }
  1403. /**
  1404. * Frames a piece of data according to the HyBi WebSocket protocol.
  1405. *
  1406. * @param {(Buffer|String)} data The data to frame
  1407. * @param {Object} options Options object
  1408. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  1409. * FIN bit
  1410. * @param {Function} [options.generateMask] The function used to generate the
  1411. * masking key
  1412. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  1413. * `data`
  1414. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  1415. * key
  1416. * @param {Number} options.opcode The opcode
  1417. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  1418. * modified
  1419. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  1420. * RSV1 bit
  1421. * @return {(Buffer|String)[]} The framed data
  1422. * @public
  1423. */
  1424. static frame(data, options) {
  1425. let mask;
  1426. let merge = false;
  1427. let offset = 2;
  1428. let skipMasking = false;
  1429. if (options.mask) {
  1430. mask = options.maskBuffer || maskBuffer;
  1431. if (options.generateMask) {
  1432. options.generateMask(mask);
  1433. } else {
  1434. randomFillSync(mask, 0, 4);
  1435. }
  1436. skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
  1437. offset = 6;
  1438. }
  1439. let dataLength;
  1440. if (typeof data === 'string') {
  1441. if (
  1442. (!options.mask || skipMasking) &&
  1443. options[kByteLength] !== undefined
  1444. ) {
  1445. dataLength = options[kByteLength];
  1446. } else {
  1447. data = Buffer.from(data);
  1448. dataLength = data.length;
  1449. }
  1450. } else {
  1451. dataLength = data.length;
  1452. merge = options.mask && options.readOnly && !skipMasking;
  1453. }
  1454. let payloadLength = dataLength;
  1455. if (dataLength >= 65536) {
  1456. offset += 8;
  1457. payloadLength = 127;
  1458. } else if (dataLength > 125) {
  1459. offset += 2;
  1460. payloadLength = 126;
  1461. }
  1462. const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
  1463. target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
  1464. if (options.rsv1) target[0] |= 0x40;
  1465. target[1] = payloadLength;
  1466. if (payloadLength === 126) {
  1467. target.writeUInt16BE(dataLength, 2);
  1468. } else if (payloadLength === 127) {
  1469. target[2] = target[3] = 0;
  1470. target.writeUIntBE(dataLength, 4, 6);
  1471. }
  1472. if (!options.mask) return [target, data];
  1473. target[1] |= 0x80;
  1474. target[offset - 4] = mask[0];
  1475. target[offset - 3] = mask[1];
  1476. target[offset - 2] = mask[2];
  1477. target[offset - 1] = mask[3];
  1478. if (skipMasking) return [target, data];
  1479. if (merge) {
  1480. applyMask(data, mask, target, offset, dataLength);
  1481. return [target];
  1482. }
  1483. applyMask(data, mask, data, 0, dataLength);
  1484. return [target, data];
  1485. }
  1486. /**
  1487. * Sends a close message to the other peer.
  1488. *
  1489. * @param {Number} [code] The status code component of the body
  1490. * @param {(String|Buffer)} [data] The message component of the body
  1491. * @param {Boolean} [mask=false] Specifies whether or not to mask the message
  1492. * @param {Function} [cb] Callback
  1493. * @public
  1494. */
  1495. close(code, data, mask, cb) {
  1496. let buf;
  1497. if (code === undefined) {
  1498. buf = EMPTY_BUFFER$1;
  1499. } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
  1500. throw new TypeError('First argument must be a valid error code number');
  1501. } else if (data === undefined || !data.length) {
  1502. buf = Buffer.allocUnsafe(2);
  1503. buf.writeUInt16BE(code, 0);
  1504. } else {
  1505. const length = Buffer.byteLength(data);
  1506. if (length > 123) {
  1507. throw new RangeError('The message must not be greater than 123 bytes');
  1508. }
  1509. buf = Buffer.allocUnsafe(2 + length);
  1510. buf.writeUInt16BE(code, 0);
  1511. if (typeof data === 'string') {
  1512. buf.write(data, 2);
  1513. } else {
  1514. buf.set(data, 2);
  1515. }
  1516. }
  1517. const options = {
  1518. [kByteLength]: buf.length,
  1519. fin: true,
  1520. generateMask: this._generateMask,
  1521. mask,
  1522. maskBuffer: this._maskBuffer,
  1523. opcode: 0x08,
  1524. readOnly: false,
  1525. rsv1: false
  1526. };
  1527. if (this._deflating) {
  1528. this.enqueue([this.dispatch, buf, false, options, cb]);
  1529. } else {
  1530. this.sendFrame(Sender$1.frame(buf, options), cb);
  1531. }
  1532. }
  1533. /**
  1534. * Sends a ping message to the other peer.
  1535. *
  1536. * @param {*} data The message to send
  1537. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  1538. * @param {Function} [cb] Callback
  1539. * @public
  1540. */
  1541. ping(data, mask, cb) {
  1542. let byteLength;
  1543. let readOnly;
  1544. if (typeof data === 'string') {
  1545. byteLength = Buffer.byteLength(data);
  1546. readOnly = false;
  1547. } else {
  1548. data = toBuffer$1(data);
  1549. byteLength = data.length;
  1550. readOnly = toBuffer$1.readOnly;
  1551. }
  1552. if (byteLength > 125) {
  1553. throw new RangeError('The data size must not be greater than 125 bytes');
  1554. }
  1555. const options = {
  1556. [kByteLength]: byteLength,
  1557. fin: true,
  1558. generateMask: this._generateMask,
  1559. mask,
  1560. maskBuffer: this._maskBuffer,
  1561. opcode: 0x09,
  1562. readOnly,
  1563. rsv1: false
  1564. };
  1565. if (this._deflating) {
  1566. this.enqueue([this.dispatch, data, false, options, cb]);
  1567. } else {
  1568. this.sendFrame(Sender$1.frame(data, options), cb);
  1569. }
  1570. }
  1571. /**
  1572. * Sends a pong message to the other peer.
  1573. *
  1574. * @param {*} data The message to send
  1575. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  1576. * @param {Function} [cb] Callback
  1577. * @public
  1578. */
  1579. pong(data, mask, cb) {
  1580. let byteLength;
  1581. let readOnly;
  1582. if (typeof data === 'string') {
  1583. byteLength = Buffer.byteLength(data);
  1584. readOnly = false;
  1585. } else {
  1586. data = toBuffer$1(data);
  1587. byteLength = data.length;
  1588. readOnly = toBuffer$1.readOnly;
  1589. }
  1590. if (byteLength > 125) {
  1591. throw new RangeError('The data size must not be greater than 125 bytes');
  1592. }
  1593. const options = {
  1594. [kByteLength]: byteLength,
  1595. fin: true,
  1596. generateMask: this._generateMask,
  1597. mask,
  1598. maskBuffer: this._maskBuffer,
  1599. opcode: 0x0a,
  1600. readOnly,
  1601. rsv1: false
  1602. };
  1603. if (this._deflating) {
  1604. this.enqueue([this.dispatch, data, false, options, cb]);
  1605. } else {
  1606. this.sendFrame(Sender$1.frame(data, options), cb);
  1607. }
  1608. }
  1609. /**
  1610. * Sends a data message to the other peer.
  1611. *
  1612. * @param {*} data The message to send
  1613. * @param {Object} options Options object
  1614. * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
  1615. * or text
  1616. * @param {Boolean} [options.compress=false] Specifies whether or not to
  1617. * compress `data`
  1618. * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
  1619. * last one
  1620. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  1621. * `data`
  1622. * @param {Function} [cb] Callback
  1623. * @public
  1624. */
  1625. send(data, options, cb) {
  1626. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  1627. let opcode = options.binary ? 2 : 1;
  1628. let rsv1 = options.compress;
  1629. let byteLength;
  1630. let readOnly;
  1631. if (typeof data === 'string') {
  1632. byteLength = Buffer.byteLength(data);
  1633. readOnly = false;
  1634. } else {
  1635. data = toBuffer$1(data);
  1636. byteLength = data.length;
  1637. readOnly = toBuffer$1.readOnly;
  1638. }
  1639. if (this._firstFragment) {
  1640. this._firstFragment = false;
  1641. if (
  1642. rsv1 &&
  1643. perMessageDeflate &&
  1644. perMessageDeflate.params[
  1645. perMessageDeflate._isServer
  1646. ? 'server_no_context_takeover'
  1647. : 'client_no_context_takeover'
  1648. ]
  1649. ) {
  1650. rsv1 = byteLength >= perMessageDeflate._threshold;
  1651. }
  1652. this._compress = rsv1;
  1653. } else {
  1654. rsv1 = false;
  1655. opcode = 0;
  1656. }
  1657. if (options.fin) this._firstFragment = true;
  1658. if (perMessageDeflate) {
  1659. const opts = {
  1660. [kByteLength]: byteLength,
  1661. fin: options.fin,
  1662. generateMask: this._generateMask,
  1663. mask: options.mask,
  1664. maskBuffer: this._maskBuffer,
  1665. opcode,
  1666. readOnly,
  1667. rsv1
  1668. };
  1669. if (this._deflating) {
  1670. this.enqueue([this.dispatch, data, this._compress, opts, cb]);
  1671. } else {
  1672. this.dispatch(data, this._compress, opts, cb);
  1673. }
  1674. } else {
  1675. this.sendFrame(
  1676. Sender$1.frame(data, {
  1677. [kByteLength]: byteLength,
  1678. fin: options.fin,
  1679. generateMask: this._generateMask,
  1680. mask: options.mask,
  1681. maskBuffer: this._maskBuffer,
  1682. opcode,
  1683. readOnly,
  1684. rsv1: false
  1685. }),
  1686. cb
  1687. );
  1688. }
  1689. }
  1690. /**
  1691. * Dispatches a message.
  1692. *
  1693. * @param {(Buffer|String)} data The message to send
  1694. * @param {Boolean} [compress=false] Specifies whether or not to compress
  1695. * `data`
  1696. * @param {Object} options Options object
  1697. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  1698. * FIN bit
  1699. * @param {Function} [options.generateMask] The function used to generate the
  1700. * masking key
  1701. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  1702. * `data`
  1703. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  1704. * key
  1705. * @param {Number} options.opcode The opcode
  1706. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  1707. * modified
  1708. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  1709. * RSV1 bit
  1710. * @param {Function} [cb] Callback
  1711. * @private
  1712. */
  1713. dispatch(data, compress, options, cb) {
  1714. if (!compress) {
  1715. this.sendFrame(Sender$1.frame(data, options), cb);
  1716. return;
  1717. }
  1718. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  1719. this._bufferedBytes += options[kByteLength];
  1720. this._deflating = true;
  1721. perMessageDeflate.compress(data, options.fin, (_, buf) => {
  1722. if (this._socket.destroyed) {
  1723. const err = new Error(
  1724. 'The socket was closed while data was being compressed'
  1725. );
  1726. if (typeof cb === 'function') cb(err);
  1727. for (let i = 0; i < this._queue.length; i++) {
  1728. const params = this._queue[i];
  1729. const callback = params[params.length - 1];
  1730. if (typeof callback === 'function') callback(err);
  1731. }
  1732. return;
  1733. }
  1734. this._bufferedBytes -= options[kByteLength];
  1735. this._deflating = false;
  1736. options.readOnly = false;
  1737. this.sendFrame(Sender$1.frame(buf, options), cb);
  1738. this.dequeue();
  1739. });
  1740. }
  1741. /**
  1742. * Executes queued send operations.
  1743. *
  1744. * @private
  1745. */
  1746. dequeue() {
  1747. while (!this._deflating && this._queue.length) {
  1748. const params = this._queue.shift();
  1749. this._bufferedBytes -= params[3][kByteLength];
  1750. Reflect.apply(params[0], this, params.slice(1));
  1751. }
  1752. }
  1753. /**
  1754. * Enqueues a send operation.
  1755. *
  1756. * @param {Array} params Send operation parameters.
  1757. * @private
  1758. */
  1759. enqueue(params) {
  1760. this._bufferedBytes += params[3][kByteLength];
  1761. this._queue.push(params);
  1762. }
  1763. /**
  1764. * Sends a frame.
  1765. *
  1766. * @param {Buffer[]} list The frame to send
  1767. * @param {Function} [cb] Callback
  1768. * @private
  1769. */
  1770. sendFrame(list, cb) {
  1771. if (list.length === 2) {
  1772. this._socket.cork();
  1773. this._socket.write(list[0]);
  1774. this._socket.write(list[1], cb);
  1775. this._socket.uncork();
  1776. } else {
  1777. this._socket.write(list[0], cb);
  1778. }
  1779. }
  1780. }
  1781. var sender = Sender$1;
  1782. const { kForOnEventAttribute: kForOnEventAttribute$1, kListener: kListener$1 } = constants;
  1783. const kCode = Symbol('kCode');
  1784. const kData = Symbol('kData');
  1785. const kError = Symbol('kError');
  1786. const kMessage = Symbol('kMessage');
  1787. const kReason = Symbol('kReason');
  1788. const kTarget = Symbol('kTarget');
  1789. const kType = Symbol('kType');
  1790. const kWasClean = Symbol('kWasClean');
  1791. /**
  1792. * Class representing an event.
  1793. */
  1794. class Event {
  1795. /**
  1796. * Create a new `Event`.
  1797. *
  1798. * @param {String} type The name of the event
  1799. * @throws {TypeError} If the `type` argument is not specified
  1800. */
  1801. constructor(type) {
  1802. this[kTarget] = null;
  1803. this[kType] = type;
  1804. }
  1805. /**
  1806. * @type {*}
  1807. */
  1808. get target() {
  1809. return this[kTarget];
  1810. }
  1811. /**
  1812. * @type {String}
  1813. */
  1814. get type() {
  1815. return this[kType];
  1816. }
  1817. }
  1818. Object.defineProperty(Event.prototype, 'target', { enumerable: true });
  1819. Object.defineProperty(Event.prototype, 'type', { enumerable: true });
  1820. /**
  1821. * Class representing a close event.
  1822. *
  1823. * @extends Event
  1824. */
  1825. class CloseEvent extends Event {
  1826. /**
  1827. * Create a new `CloseEvent`.
  1828. *
  1829. * @param {String} type The name of the event
  1830. * @param {Object} [options] A dictionary object that allows for setting
  1831. * attributes via object members of the same name
  1832. * @param {Number} [options.code=0] The status code explaining why the
  1833. * connection was closed
  1834. * @param {String} [options.reason=''] A human-readable string explaining why
  1835. * the connection was closed
  1836. * @param {Boolean} [options.wasClean=false] Indicates whether or not the
  1837. * connection was cleanly closed
  1838. */
  1839. constructor(type, options = {}) {
  1840. super(type);
  1841. this[kCode] = options.code === undefined ? 0 : options.code;
  1842. this[kReason] = options.reason === undefined ? '' : options.reason;
  1843. this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
  1844. }
  1845. /**
  1846. * @type {Number}
  1847. */
  1848. get code() {
  1849. return this[kCode];
  1850. }
  1851. /**
  1852. * @type {String}
  1853. */
  1854. get reason() {
  1855. return this[kReason];
  1856. }
  1857. /**
  1858. * @type {Boolean}
  1859. */
  1860. get wasClean() {
  1861. return this[kWasClean];
  1862. }
  1863. }
  1864. Object.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });
  1865. Object.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });
  1866. Object.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });
  1867. /**
  1868. * Class representing an error event.
  1869. *
  1870. * @extends Event
  1871. */
  1872. class ErrorEvent extends Event {
  1873. /**
  1874. * Create a new `ErrorEvent`.
  1875. *
  1876. * @param {String} type The name of the event
  1877. * @param {Object} [options] A dictionary object that allows for setting
  1878. * attributes via object members of the same name
  1879. * @param {*} [options.error=null] The error that generated this event
  1880. * @param {String} [options.message=''] The error message
  1881. */
  1882. constructor(type, options = {}) {
  1883. super(type);
  1884. this[kError] = options.error === undefined ? null : options.error;
  1885. this[kMessage] = options.message === undefined ? '' : options.message;
  1886. }
  1887. /**
  1888. * @type {*}
  1889. */
  1890. get error() {
  1891. return this[kError];
  1892. }
  1893. /**
  1894. * @type {String}
  1895. */
  1896. get message() {
  1897. return this[kMessage];
  1898. }
  1899. }
  1900. Object.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });
  1901. Object.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });
  1902. /**
  1903. * Class representing a message event.
  1904. *
  1905. * @extends Event
  1906. */
  1907. class MessageEvent extends Event {
  1908. /**
  1909. * Create a new `MessageEvent`.
  1910. *
  1911. * @param {String} type The name of the event
  1912. * @param {Object} [options] A dictionary object that allows for setting
  1913. * attributes via object members of the same name
  1914. * @param {*} [options.data=null] The message content
  1915. */
  1916. constructor(type, options = {}) {
  1917. super(type);
  1918. this[kData] = options.data === undefined ? null : options.data;
  1919. }
  1920. /**
  1921. * @type {*}
  1922. */
  1923. get data() {
  1924. return this[kData];
  1925. }
  1926. }
  1927. Object.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });
  1928. /**
  1929. * This provides methods for emulating the `EventTarget` interface. It's not
  1930. * meant to be used directly.
  1931. *
  1932. * @mixin
  1933. */
  1934. const EventTarget = {
  1935. /**
  1936. * Register an event listener.
  1937. *
  1938. * @param {String} type A string representing the event type to listen for
  1939. * @param {Function} listener The listener to add
  1940. * @param {Object} [options] An options object specifies characteristics about
  1941. * the event listener
  1942. * @param {Boolean} [options.once=false] A `Boolean` indicating that the
  1943. * listener should be invoked at most once after being added. If `true`,
  1944. * the listener would be automatically removed when invoked.
  1945. * @public
  1946. */
  1947. addEventListener(type, listener, options = {}) {
  1948. let wrapper;
  1949. if (type === 'message') {
  1950. wrapper = function onMessage(data, isBinary) {
  1951. const event = new MessageEvent('message', {
  1952. data: isBinary ? data : data.toString()
  1953. });
  1954. event[kTarget] = this;
  1955. listener.call(this, event);
  1956. };
  1957. } else if (type === 'close') {
  1958. wrapper = function onClose(code, message) {
  1959. const event = new CloseEvent('close', {
  1960. code,
  1961. reason: message.toString(),
  1962. wasClean: this._closeFrameReceived && this._closeFrameSent
  1963. });
  1964. event[kTarget] = this;
  1965. listener.call(this, event);
  1966. };
  1967. } else if (type === 'error') {
  1968. wrapper = function onError(error) {
  1969. const event = new ErrorEvent('error', {
  1970. error,
  1971. message: error.message
  1972. });
  1973. event[kTarget] = this;
  1974. listener.call(this, event);
  1975. };
  1976. } else if (type === 'open') {
  1977. wrapper = function onOpen() {
  1978. const event = new Event('open');
  1979. event[kTarget] = this;
  1980. listener.call(this, event);
  1981. };
  1982. } else {
  1983. return;
  1984. }
  1985. wrapper[kForOnEventAttribute$1] = !!options[kForOnEventAttribute$1];
  1986. wrapper[kListener$1] = listener;
  1987. if (options.once) {
  1988. this.once(type, wrapper);
  1989. } else {
  1990. this.on(type, wrapper);
  1991. }
  1992. },
  1993. /**
  1994. * Remove an event listener.
  1995. *
  1996. * @param {String} type A string representing the event type to remove
  1997. * @param {Function} handler The listener to remove
  1998. * @public
  1999. */
  2000. removeEventListener(type, handler) {
  2001. for (const listener of this.listeners(type)) {
  2002. if (listener[kListener$1] === handler && !listener[kForOnEventAttribute$1]) {
  2003. this.removeListener(type, listener);
  2004. break;
  2005. }
  2006. }
  2007. }
  2008. };
  2009. var eventTarget = {
  2010. CloseEvent,
  2011. ErrorEvent,
  2012. Event,
  2013. EventTarget,
  2014. MessageEvent
  2015. };
  2016. const { tokenChars: tokenChars$1 } = validation.exports;
  2017. /**
  2018. * Adds an offer to the map of extension offers or a parameter to the map of
  2019. * parameters.
  2020. *
  2021. * @param {Object} dest The map of extension offers or parameters
  2022. * @param {String} name The extension or parameter name
  2023. * @param {(Object|Boolean|String)} elem The extension parameters or the
  2024. * parameter value
  2025. * @private
  2026. */
  2027. function push(dest, name, elem) {
  2028. if (dest[name] === undefined) dest[name] = [elem];
  2029. else dest[name].push(elem);
  2030. }
  2031. /**
  2032. * Parses the `Sec-WebSocket-Extensions` header into an object.
  2033. *
  2034. * @param {String} header The field value of the header
  2035. * @return {Object} The parsed object
  2036. * @public
  2037. */
  2038. function parse$2(header) {
  2039. const offers = Object.create(null);
  2040. let params = Object.create(null);
  2041. let mustUnescape = false;
  2042. let isEscaping = false;
  2043. let inQuotes = false;
  2044. let extensionName;
  2045. let paramName;
  2046. let start = -1;
  2047. let code = -1;
  2048. let end = -1;
  2049. let i = 0;
  2050. for (; i < header.length; i++) {
  2051. code = header.charCodeAt(i);
  2052. if (extensionName === undefined) {
  2053. if (end === -1 && tokenChars$1[code] === 1) {
  2054. if (start === -1) start = i;
  2055. } else if (
  2056. i !== 0 &&
  2057. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  2058. ) {
  2059. if (end === -1 && start !== -1) end = i;
  2060. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  2061. if (start === -1) {
  2062. throw new SyntaxError(`Unexpected character at index ${i}`);
  2063. }
  2064. if (end === -1) end = i;
  2065. const name = header.slice(start, end);
  2066. if (code === 0x2c) {
  2067. push(offers, name, params);
  2068. params = Object.create(null);
  2069. } else {
  2070. extensionName = name;
  2071. }
  2072. start = end = -1;
  2073. } else {
  2074. throw new SyntaxError(`Unexpected character at index ${i}`);
  2075. }
  2076. } else if (paramName === undefined) {
  2077. if (end === -1 && tokenChars$1[code] === 1) {
  2078. if (start === -1) start = i;
  2079. } else if (code === 0x20 || code === 0x09) {
  2080. if (end === -1 && start !== -1) end = i;
  2081. } else if (code === 0x3b || code === 0x2c) {
  2082. if (start === -1) {
  2083. throw new SyntaxError(`Unexpected character at index ${i}`);
  2084. }
  2085. if (end === -1) end = i;
  2086. push(params, header.slice(start, end), true);
  2087. if (code === 0x2c) {
  2088. push(offers, extensionName, params);
  2089. params = Object.create(null);
  2090. extensionName = undefined;
  2091. }
  2092. start = end = -1;
  2093. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  2094. paramName = header.slice(start, i);
  2095. start = end = -1;
  2096. } else {
  2097. throw new SyntaxError(`Unexpected character at index ${i}`);
  2098. }
  2099. } else {
  2100. //
  2101. // The value of a quoted-string after unescaping must conform to the
  2102. // token ABNF, so only token characters are valid.
  2103. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  2104. //
  2105. if (isEscaping) {
  2106. if (tokenChars$1[code] !== 1) {
  2107. throw new SyntaxError(`Unexpected character at index ${i}`);
  2108. }
  2109. if (start === -1) start = i;
  2110. else if (!mustUnescape) mustUnescape = true;
  2111. isEscaping = false;
  2112. } else if (inQuotes) {
  2113. if (tokenChars$1[code] === 1) {
  2114. if (start === -1) start = i;
  2115. } else if (code === 0x22 /* '"' */ && start !== -1) {
  2116. inQuotes = false;
  2117. end = i;
  2118. } else if (code === 0x5c /* '\' */) {
  2119. isEscaping = true;
  2120. } else {
  2121. throw new SyntaxError(`Unexpected character at index ${i}`);
  2122. }
  2123. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  2124. inQuotes = true;
  2125. } else if (end === -1 && tokenChars$1[code] === 1) {
  2126. if (start === -1) start = i;
  2127. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  2128. if (end === -1) end = i;
  2129. } else if (code === 0x3b || code === 0x2c) {
  2130. if (start === -1) {
  2131. throw new SyntaxError(`Unexpected character at index ${i}`);
  2132. }
  2133. if (end === -1) end = i;
  2134. let value = header.slice(start, end);
  2135. if (mustUnescape) {
  2136. value = value.replace(/\\/g, '');
  2137. mustUnescape = false;
  2138. }
  2139. push(params, paramName, value);
  2140. if (code === 0x2c) {
  2141. push(offers, extensionName, params);
  2142. params = Object.create(null);
  2143. extensionName = undefined;
  2144. }
  2145. paramName = undefined;
  2146. start = end = -1;
  2147. } else {
  2148. throw new SyntaxError(`Unexpected character at index ${i}`);
  2149. }
  2150. }
  2151. }
  2152. if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {
  2153. throw new SyntaxError('Unexpected end of input');
  2154. }
  2155. if (end === -1) end = i;
  2156. const token = header.slice(start, end);
  2157. if (extensionName === undefined) {
  2158. push(offers, token, params);
  2159. } else {
  2160. if (paramName === undefined) {
  2161. push(params, token, true);
  2162. } else if (mustUnescape) {
  2163. push(params, paramName, token.replace(/\\/g, ''));
  2164. } else {
  2165. push(params, paramName, token);
  2166. }
  2167. push(offers, extensionName, params);
  2168. }
  2169. return offers;
  2170. }
  2171. /**
  2172. * Builds the `Sec-WebSocket-Extensions` header field value.
  2173. *
  2174. * @param {Object} extensions The map of extensions and parameters to format
  2175. * @return {String} A string representing the given object
  2176. * @public
  2177. */
  2178. function format$1(extensions) {
  2179. return Object.keys(extensions)
  2180. .map((extension) => {
  2181. let configurations = extensions[extension];
  2182. if (!Array.isArray(configurations)) configurations = [configurations];
  2183. return configurations
  2184. .map((params) => {
  2185. return [extension]
  2186. .concat(
  2187. Object.keys(params).map((k) => {
  2188. let values = params[k];
  2189. if (!Array.isArray(values)) values = [values];
  2190. return values
  2191. .map((v) => (v === true ? k : `${k}=${v}`))
  2192. .join('; ');
  2193. })
  2194. )
  2195. .join('; ');
  2196. })
  2197. .join(', ');
  2198. })
  2199. .join(', ');
  2200. }
  2201. var extension$1 = { format: format$1, parse: parse$2 };
  2202. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Readable$" }] */
  2203. const EventEmitter$1 = require$$2;
  2204. const https = require$$1;
  2205. const http$1 = require$$2$1;
  2206. const net = require$$3;
  2207. const tls = require$$4;
  2208. const { randomBytes, createHash: createHash$1 } = require$$5;
  2209. const { URL: URL$1 } = url;
  2210. const PerMessageDeflate$1 = permessageDeflate;
  2211. const Receiver = receiver;
  2212. const Sender = sender;
  2213. const {
  2214. BINARY_TYPES,
  2215. EMPTY_BUFFER,
  2216. GUID: GUID$1,
  2217. kForOnEventAttribute,
  2218. kListener,
  2219. kStatusCode,
  2220. kWebSocket: kWebSocket$1,
  2221. NOOP
  2222. } = constants;
  2223. const {
  2224. EventTarget: { addEventListener, removeEventListener }
  2225. } = eventTarget;
  2226. const { format, parse: parse$1 } = extension$1;
  2227. const { toBuffer } = bufferUtil$1.exports;
  2228. const closeTimeout = 30 * 1000;
  2229. const kAborted = Symbol('kAborted');
  2230. const protocolVersions = [8, 13];
  2231. const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
  2232. const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
  2233. /**
  2234. * Class representing a WebSocket.
  2235. *
  2236. * @extends EventEmitter
  2237. */
  2238. class WebSocket$1 extends EventEmitter$1 {
  2239. /**
  2240. * Create a new `WebSocket`.
  2241. *
  2242. * @param {(String|URL)} address The URL to which to connect
  2243. * @param {(String|String[])} [protocols] The subprotocols
  2244. * @param {Object} [options] Connection options
  2245. */
  2246. constructor(address, protocols, options) {
  2247. super();
  2248. this._binaryType = BINARY_TYPES[0];
  2249. this._closeCode = 1006;
  2250. this._closeFrameReceived = false;
  2251. this._closeFrameSent = false;
  2252. this._closeMessage = EMPTY_BUFFER;
  2253. this._closeTimer = null;
  2254. this._extensions = {};
  2255. this._paused = false;
  2256. this._protocol = '';
  2257. this._readyState = WebSocket$1.CONNECTING;
  2258. this._receiver = null;
  2259. this._sender = null;
  2260. this._socket = null;
  2261. if (address !== null) {
  2262. this._bufferedAmount = 0;
  2263. this._isServer = false;
  2264. this._redirects = 0;
  2265. if (protocols === undefined) {
  2266. protocols = [];
  2267. } else if (!Array.isArray(protocols)) {
  2268. if (typeof protocols === 'object' && protocols !== null) {
  2269. options = protocols;
  2270. protocols = [];
  2271. } else {
  2272. protocols = [protocols];
  2273. }
  2274. }
  2275. initAsClient(this, address, protocols, options);
  2276. } else {
  2277. this._isServer = true;
  2278. }
  2279. }
  2280. /**
  2281. * This deviates from the WHATWG interface since ws doesn't support the
  2282. * required default "blob" type (instead we define a custom "nodebuffer"
  2283. * type).
  2284. *
  2285. * @type {String}
  2286. */
  2287. get binaryType() {
  2288. return this._binaryType;
  2289. }
  2290. set binaryType(type) {
  2291. if (!BINARY_TYPES.includes(type)) return;
  2292. this._binaryType = type;
  2293. //
  2294. // Allow to change `binaryType` on the fly.
  2295. //
  2296. if (this._receiver) this._receiver._binaryType = type;
  2297. }
  2298. /**
  2299. * @type {Number}
  2300. */
  2301. get bufferedAmount() {
  2302. if (!this._socket) return this._bufferedAmount;
  2303. return this._socket._writableState.length + this._sender._bufferedBytes;
  2304. }
  2305. /**
  2306. * @type {String}
  2307. */
  2308. get extensions() {
  2309. return Object.keys(this._extensions).join();
  2310. }
  2311. /**
  2312. * @type {Boolean}
  2313. */
  2314. get isPaused() {
  2315. return this._paused;
  2316. }
  2317. /**
  2318. * @type {Function}
  2319. */
  2320. /* istanbul ignore next */
  2321. get onclose() {
  2322. return null;
  2323. }
  2324. /**
  2325. * @type {Function}
  2326. */
  2327. /* istanbul ignore next */
  2328. get onerror() {
  2329. return null;
  2330. }
  2331. /**
  2332. * @type {Function}
  2333. */
  2334. /* istanbul ignore next */
  2335. get onopen() {
  2336. return null;
  2337. }
  2338. /**
  2339. * @type {Function}
  2340. */
  2341. /* istanbul ignore next */
  2342. get onmessage() {
  2343. return null;
  2344. }
  2345. /**
  2346. * @type {String}
  2347. */
  2348. get protocol() {
  2349. return this._protocol;
  2350. }
  2351. /**
  2352. * @type {Number}
  2353. */
  2354. get readyState() {
  2355. return this._readyState;
  2356. }
  2357. /**
  2358. * @type {String}
  2359. */
  2360. get url() {
  2361. return this._url;
  2362. }
  2363. /**
  2364. * Set up the socket and the internal resources.
  2365. *
  2366. * @param {(net.Socket|tls.Socket)} socket The network socket between the
  2367. * server and client
  2368. * @param {Buffer} head The first packet of the upgraded stream
  2369. * @param {Object} options Options object
  2370. * @param {Function} [options.generateMask] The function used to generate the
  2371. * masking key
  2372. * @param {Number} [options.maxPayload=0] The maximum allowed message size
  2373. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  2374. * not to skip UTF-8 validation for text and close messages
  2375. * @private
  2376. */
  2377. setSocket(socket, head, options) {
  2378. const receiver = new Receiver({
  2379. binaryType: this.binaryType,
  2380. extensions: this._extensions,
  2381. isServer: this._isServer,
  2382. maxPayload: options.maxPayload,
  2383. skipUTF8Validation: options.skipUTF8Validation
  2384. });
  2385. this._sender = new Sender(socket, this._extensions, options.generateMask);
  2386. this._receiver = receiver;
  2387. this._socket = socket;
  2388. receiver[kWebSocket$1] = this;
  2389. socket[kWebSocket$1] = this;
  2390. receiver.on('conclude', receiverOnConclude);
  2391. receiver.on('drain', receiverOnDrain);
  2392. receiver.on('error', receiverOnError);
  2393. receiver.on('message', receiverOnMessage);
  2394. receiver.on('ping', receiverOnPing);
  2395. receiver.on('pong', receiverOnPong);
  2396. socket.setTimeout(0);
  2397. socket.setNoDelay();
  2398. if (head.length > 0) socket.unshift(head);
  2399. socket.on('close', socketOnClose);
  2400. socket.on('data', socketOnData);
  2401. socket.on('end', socketOnEnd);
  2402. socket.on('error', socketOnError$1);
  2403. this._readyState = WebSocket$1.OPEN;
  2404. this.emit('open');
  2405. }
  2406. /**
  2407. * Emit the `'close'` event.
  2408. *
  2409. * @private
  2410. */
  2411. emitClose() {
  2412. if (!this._socket) {
  2413. this._readyState = WebSocket$1.CLOSED;
  2414. this.emit('close', this._closeCode, this._closeMessage);
  2415. return;
  2416. }
  2417. if (this._extensions[PerMessageDeflate$1.extensionName]) {
  2418. this._extensions[PerMessageDeflate$1.extensionName].cleanup();
  2419. }
  2420. this._receiver.removeAllListeners();
  2421. this._readyState = WebSocket$1.CLOSED;
  2422. this.emit('close', this._closeCode, this._closeMessage);
  2423. }
  2424. /**
  2425. * Start a closing handshake.
  2426. *
  2427. * +----------+ +-----------+ +----------+
  2428. * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
  2429. * | +----------+ +-----------+ +----------+ |
  2430. * +----------+ +-----------+ |
  2431. * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
  2432. * +----------+ +-----------+ |
  2433. * | | | +---+ |
  2434. * +------------------------+-->|fin| - - - -
  2435. * | +---+ | +---+
  2436. * - - - - -|fin|<---------------------+
  2437. * +---+
  2438. *
  2439. * @param {Number} [code] Status code explaining why the connection is closing
  2440. * @param {(String|Buffer)} [data] The reason why the connection is
  2441. * closing
  2442. * @public
  2443. */
  2444. close(code, data) {
  2445. if (this.readyState === WebSocket$1.CLOSED) return;
  2446. if (this.readyState === WebSocket$1.CONNECTING) {
  2447. const msg = 'WebSocket was closed before the connection was established';
  2448. return abortHandshake$1(this, this._req, msg);
  2449. }
  2450. if (this.readyState === WebSocket$1.CLOSING) {
  2451. if (
  2452. this._closeFrameSent &&
  2453. (this._closeFrameReceived || this._receiver._writableState.errorEmitted)
  2454. ) {
  2455. this._socket.end();
  2456. }
  2457. return;
  2458. }
  2459. this._readyState = WebSocket$1.CLOSING;
  2460. this._sender.close(code, data, !this._isServer, (err) => {
  2461. //
  2462. // This error is handled by the `'error'` listener on the socket. We only
  2463. // want to know if the close frame has been sent here.
  2464. //
  2465. if (err) return;
  2466. this._closeFrameSent = true;
  2467. if (
  2468. this._closeFrameReceived ||
  2469. this._receiver._writableState.errorEmitted
  2470. ) {
  2471. this._socket.end();
  2472. }
  2473. });
  2474. //
  2475. // Specify a timeout for the closing handshake to complete.
  2476. //
  2477. this._closeTimer = setTimeout(
  2478. this._socket.destroy.bind(this._socket),
  2479. closeTimeout
  2480. );
  2481. }
  2482. /**
  2483. * Pause the socket.
  2484. *
  2485. * @public
  2486. */
  2487. pause() {
  2488. if (
  2489. this.readyState === WebSocket$1.CONNECTING ||
  2490. this.readyState === WebSocket$1.CLOSED
  2491. ) {
  2492. return;
  2493. }
  2494. this._paused = true;
  2495. this._socket.pause();
  2496. }
  2497. /**
  2498. * Send a ping.
  2499. *
  2500. * @param {*} [data] The data to send
  2501. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  2502. * @param {Function} [cb] Callback which is executed when the ping is sent
  2503. * @public
  2504. */
  2505. ping(data, mask, cb) {
  2506. if (this.readyState === WebSocket$1.CONNECTING) {
  2507. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  2508. }
  2509. if (typeof data === 'function') {
  2510. cb = data;
  2511. data = mask = undefined;
  2512. } else if (typeof mask === 'function') {
  2513. cb = mask;
  2514. mask = undefined;
  2515. }
  2516. if (typeof data === 'number') data = data.toString();
  2517. if (this.readyState !== WebSocket$1.OPEN) {
  2518. sendAfterClose(this, data, cb);
  2519. return;
  2520. }
  2521. if (mask === undefined) mask = !this._isServer;
  2522. this._sender.ping(data || EMPTY_BUFFER, mask, cb);
  2523. }
  2524. /**
  2525. * Send a pong.
  2526. *
  2527. * @param {*} [data] The data to send
  2528. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  2529. * @param {Function} [cb] Callback which is executed when the pong is sent
  2530. * @public
  2531. */
  2532. pong(data, mask, cb) {
  2533. if (this.readyState === WebSocket$1.CONNECTING) {
  2534. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  2535. }
  2536. if (typeof data === 'function') {
  2537. cb = data;
  2538. data = mask = undefined;
  2539. } else if (typeof mask === 'function') {
  2540. cb = mask;
  2541. mask = undefined;
  2542. }
  2543. if (typeof data === 'number') data = data.toString();
  2544. if (this.readyState !== WebSocket$1.OPEN) {
  2545. sendAfterClose(this, data, cb);
  2546. return;
  2547. }
  2548. if (mask === undefined) mask = !this._isServer;
  2549. this._sender.pong(data || EMPTY_BUFFER, mask, cb);
  2550. }
  2551. /**
  2552. * Resume the socket.
  2553. *
  2554. * @public
  2555. */
  2556. resume() {
  2557. if (
  2558. this.readyState === WebSocket$1.CONNECTING ||
  2559. this.readyState === WebSocket$1.CLOSED
  2560. ) {
  2561. return;
  2562. }
  2563. this._paused = false;
  2564. if (!this._receiver._writableState.needDrain) this._socket.resume();
  2565. }
  2566. /**
  2567. * Send a data message.
  2568. *
  2569. * @param {*} data The message to send
  2570. * @param {Object} [options] Options object
  2571. * @param {Boolean} [options.binary] Specifies whether `data` is binary or
  2572. * text
  2573. * @param {Boolean} [options.compress] Specifies whether or not to compress
  2574. * `data`
  2575. * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
  2576. * last one
  2577. * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
  2578. * @param {Function} [cb] Callback which is executed when data is written out
  2579. * @public
  2580. */
  2581. send(data, options, cb) {
  2582. if (this.readyState === WebSocket$1.CONNECTING) {
  2583. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  2584. }
  2585. if (typeof options === 'function') {
  2586. cb = options;
  2587. options = {};
  2588. }
  2589. if (typeof data === 'number') data = data.toString();
  2590. if (this.readyState !== WebSocket$1.OPEN) {
  2591. sendAfterClose(this, data, cb);
  2592. return;
  2593. }
  2594. const opts = {
  2595. binary: typeof data !== 'string',
  2596. mask: !this._isServer,
  2597. compress: true,
  2598. fin: true,
  2599. ...options
  2600. };
  2601. if (!this._extensions[PerMessageDeflate$1.extensionName]) {
  2602. opts.compress = false;
  2603. }
  2604. this._sender.send(data || EMPTY_BUFFER, opts, cb);
  2605. }
  2606. /**
  2607. * Forcibly close the connection.
  2608. *
  2609. * @public
  2610. */
  2611. terminate() {
  2612. if (this.readyState === WebSocket$1.CLOSED) return;
  2613. if (this.readyState === WebSocket$1.CONNECTING) {
  2614. const msg = 'WebSocket was closed before the connection was established';
  2615. return abortHandshake$1(this, this._req, msg);
  2616. }
  2617. if (this._socket) {
  2618. this._readyState = WebSocket$1.CLOSING;
  2619. this._socket.destroy();
  2620. }
  2621. }
  2622. }
  2623. /**
  2624. * @constant {Number} CONNECTING
  2625. * @memberof WebSocket
  2626. */
  2627. Object.defineProperty(WebSocket$1, 'CONNECTING', {
  2628. enumerable: true,
  2629. value: readyStates.indexOf('CONNECTING')
  2630. });
  2631. /**
  2632. * @constant {Number} CONNECTING
  2633. * @memberof WebSocket.prototype
  2634. */
  2635. Object.defineProperty(WebSocket$1.prototype, 'CONNECTING', {
  2636. enumerable: true,
  2637. value: readyStates.indexOf('CONNECTING')
  2638. });
  2639. /**
  2640. * @constant {Number} OPEN
  2641. * @memberof WebSocket
  2642. */
  2643. Object.defineProperty(WebSocket$1, 'OPEN', {
  2644. enumerable: true,
  2645. value: readyStates.indexOf('OPEN')
  2646. });
  2647. /**
  2648. * @constant {Number} OPEN
  2649. * @memberof WebSocket.prototype
  2650. */
  2651. Object.defineProperty(WebSocket$1.prototype, 'OPEN', {
  2652. enumerable: true,
  2653. value: readyStates.indexOf('OPEN')
  2654. });
  2655. /**
  2656. * @constant {Number} CLOSING
  2657. * @memberof WebSocket
  2658. */
  2659. Object.defineProperty(WebSocket$1, 'CLOSING', {
  2660. enumerable: true,
  2661. value: readyStates.indexOf('CLOSING')
  2662. });
  2663. /**
  2664. * @constant {Number} CLOSING
  2665. * @memberof WebSocket.prototype
  2666. */
  2667. Object.defineProperty(WebSocket$1.prototype, 'CLOSING', {
  2668. enumerable: true,
  2669. value: readyStates.indexOf('CLOSING')
  2670. });
  2671. /**
  2672. * @constant {Number} CLOSED
  2673. * @memberof WebSocket
  2674. */
  2675. Object.defineProperty(WebSocket$1, 'CLOSED', {
  2676. enumerable: true,
  2677. value: readyStates.indexOf('CLOSED')
  2678. });
  2679. /**
  2680. * @constant {Number} CLOSED
  2681. * @memberof WebSocket.prototype
  2682. */
  2683. Object.defineProperty(WebSocket$1.prototype, 'CLOSED', {
  2684. enumerable: true,
  2685. value: readyStates.indexOf('CLOSED')
  2686. });
  2687. [
  2688. 'binaryType',
  2689. 'bufferedAmount',
  2690. 'extensions',
  2691. 'isPaused',
  2692. 'protocol',
  2693. 'readyState',
  2694. 'url'
  2695. ].forEach((property) => {
  2696. Object.defineProperty(WebSocket$1.prototype, property, { enumerable: true });
  2697. });
  2698. //
  2699. // Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
  2700. // See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
  2701. //
  2702. ['open', 'error', 'close', 'message'].forEach((method) => {
  2703. Object.defineProperty(WebSocket$1.prototype, `on${method}`, {
  2704. enumerable: true,
  2705. get() {
  2706. for (const listener of this.listeners(method)) {
  2707. if (listener[kForOnEventAttribute]) return listener[kListener];
  2708. }
  2709. return null;
  2710. },
  2711. set(handler) {
  2712. for (const listener of this.listeners(method)) {
  2713. if (listener[kForOnEventAttribute]) {
  2714. this.removeListener(method, listener);
  2715. break;
  2716. }
  2717. }
  2718. if (typeof handler !== 'function') return;
  2719. this.addEventListener(method, handler, {
  2720. [kForOnEventAttribute]: true
  2721. });
  2722. }
  2723. });
  2724. });
  2725. WebSocket$1.prototype.addEventListener = addEventListener;
  2726. WebSocket$1.prototype.removeEventListener = removeEventListener;
  2727. var websocket = WebSocket$1;
  2728. /**
  2729. * Initialize a WebSocket client.
  2730. *
  2731. * @param {WebSocket} websocket The client to initialize
  2732. * @param {(String|URL)} address The URL to which to connect
  2733. * @param {Array} protocols The subprotocols
  2734. * @param {Object} [options] Connection options
  2735. * @param {Boolean} [options.followRedirects=false] Whether or not to follow
  2736. * redirects
  2737. * @param {Function} [options.generateMask] The function used to generate the
  2738. * masking key
  2739. * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
  2740. * handshake request
  2741. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  2742. * size
  2743. * @param {Number} [options.maxRedirects=10] The maximum number of redirects
  2744. * allowed
  2745. * @param {String} [options.origin] Value of the `Origin` or
  2746. * `Sec-WebSocket-Origin` header
  2747. * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
  2748. * permessage-deflate
  2749. * @param {Number} [options.protocolVersion=13] Value of the
  2750. * `Sec-WebSocket-Version` header
  2751. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  2752. * not to skip UTF-8 validation for text and close messages
  2753. * @private
  2754. */
  2755. function initAsClient(websocket, address, protocols, options) {
  2756. const opts = {
  2757. protocolVersion: protocolVersions[1],
  2758. maxPayload: 100 * 1024 * 1024,
  2759. skipUTF8Validation: false,
  2760. perMessageDeflate: true,
  2761. followRedirects: false,
  2762. maxRedirects: 10,
  2763. ...options,
  2764. createConnection: undefined,
  2765. socketPath: undefined,
  2766. hostname: undefined,
  2767. protocol: undefined,
  2768. timeout: undefined,
  2769. method: 'GET',
  2770. host: undefined,
  2771. path: undefined,
  2772. port: undefined
  2773. };
  2774. if (!protocolVersions.includes(opts.protocolVersion)) {
  2775. throw new RangeError(
  2776. `Unsupported protocol version: ${opts.protocolVersion} ` +
  2777. `(supported versions: ${protocolVersions.join(', ')})`
  2778. );
  2779. }
  2780. let parsedUrl;
  2781. if (address instanceof URL$1) {
  2782. parsedUrl = address;
  2783. websocket._url = address.href;
  2784. } else {
  2785. try {
  2786. parsedUrl = new URL$1(address);
  2787. } catch (e) {
  2788. throw new SyntaxError(`Invalid URL: ${address}`);
  2789. }
  2790. websocket._url = address;
  2791. }
  2792. const isSecure = parsedUrl.protocol === 'wss:';
  2793. const isUnixSocket = parsedUrl.protocol === 'ws+unix:';
  2794. let invalidURLMessage;
  2795. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isUnixSocket) {
  2796. invalidURLMessage =
  2797. 'The URL\'s protocol must be one of "ws:", "wss:", or "ws+unix:"';
  2798. } else if (isUnixSocket && !parsedUrl.pathname) {
  2799. invalidURLMessage = "The URL's pathname is empty";
  2800. } else if (parsedUrl.hash) {
  2801. invalidURLMessage = 'The URL contains a fragment identifier';
  2802. }
  2803. if (invalidURLMessage) {
  2804. const err = new SyntaxError(invalidURLMessage);
  2805. if (websocket._redirects === 0) {
  2806. throw err;
  2807. } else {
  2808. emitErrorAndClose(websocket, err);
  2809. return;
  2810. }
  2811. }
  2812. const defaultPort = isSecure ? 443 : 80;
  2813. const key = randomBytes(16).toString('base64');
  2814. const request = isSecure ? https.request : http$1.request;
  2815. const protocolSet = new Set();
  2816. let perMessageDeflate;
  2817. opts.createConnection = isSecure ? tlsConnect : netConnect;
  2818. opts.defaultPort = opts.defaultPort || defaultPort;
  2819. opts.port = parsedUrl.port || defaultPort;
  2820. opts.host = parsedUrl.hostname.startsWith('[')
  2821. ? parsedUrl.hostname.slice(1, -1)
  2822. : parsedUrl.hostname;
  2823. opts.headers = {
  2824. ...opts.headers,
  2825. 'Sec-WebSocket-Version': opts.protocolVersion,
  2826. 'Sec-WebSocket-Key': key,
  2827. Connection: 'Upgrade',
  2828. Upgrade: 'websocket'
  2829. };
  2830. opts.path = parsedUrl.pathname + parsedUrl.search;
  2831. opts.timeout = opts.handshakeTimeout;
  2832. if (opts.perMessageDeflate) {
  2833. perMessageDeflate = new PerMessageDeflate$1(
  2834. opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
  2835. false,
  2836. opts.maxPayload
  2837. );
  2838. opts.headers['Sec-WebSocket-Extensions'] = format({
  2839. [PerMessageDeflate$1.extensionName]: perMessageDeflate.offer()
  2840. });
  2841. }
  2842. if (protocols.length) {
  2843. for (const protocol of protocols) {
  2844. if (
  2845. typeof protocol !== 'string' ||
  2846. !subprotocolRegex.test(protocol) ||
  2847. protocolSet.has(protocol)
  2848. ) {
  2849. throw new SyntaxError(
  2850. 'An invalid or duplicated subprotocol was specified'
  2851. );
  2852. }
  2853. protocolSet.add(protocol);
  2854. }
  2855. opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
  2856. }
  2857. if (opts.origin) {
  2858. if (opts.protocolVersion < 13) {
  2859. opts.headers['Sec-WebSocket-Origin'] = opts.origin;
  2860. } else {
  2861. opts.headers.Origin = opts.origin;
  2862. }
  2863. }
  2864. if (parsedUrl.username || parsedUrl.password) {
  2865. opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
  2866. }
  2867. if (isUnixSocket) {
  2868. const parts = opts.path.split(':');
  2869. opts.socketPath = parts[0];
  2870. opts.path = parts[1];
  2871. }
  2872. let req;
  2873. if (opts.followRedirects) {
  2874. if (websocket._redirects === 0) {
  2875. websocket._originalUnixSocket = isUnixSocket;
  2876. websocket._originalSecure = isSecure;
  2877. websocket._originalHostOrSocketPath = isUnixSocket
  2878. ? opts.socketPath
  2879. : parsedUrl.host;
  2880. const headers = options && options.headers;
  2881. //
  2882. // Shallow copy the user provided options so that headers can be changed
  2883. // without mutating the original object.
  2884. //
  2885. options = { ...options, headers: {} };
  2886. if (headers) {
  2887. for (const [key, value] of Object.entries(headers)) {
  2888. options.headers[key.toLowerCase()] = value;
  2889. }
  2890. }
  2891. } else if (websocket.listenerCount('redirect') === 0) {
  2892. const isSameHost = isUnixSocket
  2893. ? websocket._originalUnixSocket
  2894. ? opts.socketPath === websocket._originalHostOrSocketPath
  2895. : false
  2896. : websocket._originalUnixSocket
  2897. ? false
  2898. : parsedUrl.host === websocket._originalHostOrSocketPath;
  2899. if (!isSameHost || (websocket._originalSecure && !isSecure)) {
  2900. //
  2901. // Match curl 7.77.0 behavior and drop the following headers. These
  2902. // headers are also dropped when following a redirect to a subdomain.
  2903. //
  2904. delete opts.headers.authorization;
  2905. delete opts.headers.cookie;
  2906. if (!isSameHost) delete opts.headers.host;
  2907. opts.auth = undefined;
  2908. }
  2909. }
  2910. //
  2911. // Match curl 7.77.0 behavior and make the first `Authorization` header win.
  2912. // If the `Authorization` header is set, then there is nothing to do as it
  2913. // will take precedence.
  2914. //
  2915. if (opts.auth && !options.headers.authorization) {
  2916. options.headers.authorization =
  2917. 'Basic ' + Buffer.from(opts.auth).toString('base64');
  2918. }
  2919. req = websocket._req = request(opts);
  2920. if (websocket._redirects) {
  2921. //
  2922. // Unlike what is done for the `'upgrade'` event, no early exit is
  2923. // triggered here if the user calls `websocket.close()` or
  2924. // `websocket.terminate()` from a listener of the `'redirect'` event. This
  2925. // is because the user can also call `request.destroy()` with an error
  2926. // before calling `websocket.close()` or `websocket.terminate()` and this
  2927. // would result in an error being emitted on the `request` object with no
  2928. // `'error'` event listeners attached.
  2929. //
  2930. websocket.emit('redirect', websocket.url, req);
  2931. }
  2932. } else {
  2933. req = websocket._req = request(opts);
  2934. }
  2935. if (opts.timeout) {
  2936. req.on('timeout', () => {
  2937. abortHandshake$1(websocket, req, 'Opening handshake has timed out');
  2938. });
  2939. }
  2940. req.on('error', (err) => {
  2941. if (req === null || req[kAborted]) return;
  2942. req = websocket._req = null;
  2943. emitErrorAndClose(websocket, err);
  2944. });
  2945. req.on('response', (res) => {
  2946. const location = res.headers.location;
  2947. const statusCode = res.statusCode;
  2948. if (
  2949. location &&
  2950. opts.followRedirects &&
  2951. statusCode >= 300 &&
  2952. statusCode < 400
  2953. ) {
  2954. if (++websocket._redirects > opts.maxRedirects) {
  2955. abortHandshake$1(websocket, req, 'Maximum redirects exceeded');
  2956. return;
  2957. }
  2958. req.abort();
  2959. let addr;
  2960. try {
  2961. addr = new URL$1(location, address);
  2962. } catch (e) {
  2963. const err = new SyntaxError(`Invalid URL: ${location}`);
  2964. emitErrorAndClose(websocket, err);
  2965. return;
  2966. }
  2967. initAsClient(websocket, addr, protocols, options);
  2968. } else if (!websocket.emit('unexpected-response', req, res)) {
  2969. abortHandshake$1(
  2970. websocket,
  2971. req,
  2972. `Unexpected server response: ${res.statusCode}`
  2973. );
  2974. }
  2975. });
  2976. req.on('upgrade', (res, socket, head) => {
  2977. websocket.emit('upgrade', res);
  2978. //
  2979. // The user may have closed the connection from a listener of the
  2980. // `'upgrade'` event.
  2981. //
  2982. if (websocket.readyState !== WebSocket$1.CONNECTING) return;
  2983. req = websocket._req = null;
  2984. if (res.headers.upgrade.toLowerCase() !== 'websocket') {
  2985. abortHandshake$1(websocket, socket, 'Invalid Upgrade header');
  2986. return;
  2987. }
  2988. const digest = createHash$1('sha1')
  2989. .update(key + GUID$1)
  2990. .digest('base64');
  2991. if (res.headers['sec-websocket-accept'] !== digest) {
  2992. abortHandshake$1(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  2993. return;
  2994. }
  2995. const serverProt = res.headers['sec-websocket-protocol'];
  2996. let protError;
  2997. if (serverProt !== undefined) {
  2998. if (!protocolSet.size) {
  2999. protError = 'Server sent a subprotocol but none was requested';
  3000. } else if (!protocolSet.has(serverProt)) {
  3001. protError = 'Server sent an invalid subprotocol';
  3002. }
  3003. } else if (protocolSet.size) {
  3004. protError = 'Server sent no subprotocol';
  3005. }
  3006. if (protError) {
  3007. abortHandshake$1(websocket, socket, protError);
  3008. return;
  3009. }
  3010. if (serverProt) websocket._protocol = serverProt;
  3011. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  3012. if (secWebSocketExtensions !== undefined) {
  3013. if (!perMessageDeflate) {
  3014. const message =
  3015. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  3016. 'was requested';
  3017. abortHandshake$1(websocket, socket, message);
  3018. return;
  3019. }
  3020. let extensions;
  3021. try {
  3022. extensions = parse$1(secWebSocketExtensions);
  3023. } catch (err) {
  3024. const message = 'Invalid Sec-WebSocket-Extensions header';
  3025. abortHandshake$1(websocket, socket, message);
  3026. return;
  3027. }
  3028. const extensionNames = Object.keys(extensions);
  3029. if (
  3030. extensionNames.length !== 1 ||
  3031. extensionNames[0] !== PerMessageDeflate$1.extensionName
  3032. ) {
  3033. const message = 'Server indicated an extension that was not requested';
  3034. abortHandshake$1(websocket, socket, message);
  3035. return;
  3036. }
  3037. try {
  3038. perMessageDeflate.accept(extensions[PerMessageDeflate$1.extensionName]);
  3039. } catch (err) {
  3040. const message = 'Invalid Sec-WebSocket-Extensions header';
  3041. abortHandshake$1(websocket, socket, message);
  3042. return;
  3043. }
  3044. websocket._extensions[PerMessageDeflate$1.extensionName] =
  3045. perMessageDeflate;
  3046. }
  3047. websocket.setSocket(socket, head, {
  3048. generateMask: opts.generateMask,
  3049. maxPayload: opts.maxPayload,
  3050. skipUTF8Validation: opts.skipUTF8Validation
  3051. });
  3052. });
  3053. req.end();
  3054. }
  3055. /**
  3056. * Emit the `'error'` and `'close'` events.
  3057. *
  3058. * @param {WebSocket} websocket The WebSocket instance
  3059. * @param {Error} The error to emit
  3060. * @private
  3061. */
  3062. function emitErrorAndClose(websocket, err) {
  3063. websocket._readyState = WebSocket$1.CLOSING;
  3064. websocket.emit('error', err);
  3065. websocket.emitClose();
  3066. }
  3067. /**
  3068. * Create a `net.Socket` and initiate a connection.
  3069. *
  3070. * @param {Object} options Connection options
  3071. * @return {net.Socket} The newly created socket used to start the connection
  3072. * @private
  3073. */
  3074. function netConnect(options) {
  3075. options.path = options.socketPath;
  3076. return net.connect(options);
  3077. }
  3078. /**
  3079. * Create a `tls.TLSSocket` and initiate a connection.
  3080. *
  3081. * @param {Object} options Connection options
  3082. * @return {tls.TLSSocket} The newly created socket used to start the connection
  3083. * @private
  3084. */
  3085. function tlsConnect(options) {
  3086. options.path = undefined;
  3087. if (!options.servername && options.servername !== '') {
  3088. options.servername = net.isIP(options.host) ? '' : options.host;
  3089. }
  3090. return tls.connect(options);
  3091. }
  3092. /**
  3093. * Abort the handshake and emit an error.
  3094. *
  3095. * @param {WebSocket} websocket The WebSocket instance
  3096. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  3097. * abort or the socket to destroy
  3098. * @param {String} message The error message
  3099. * @private
  3100. */
  3101. function abortHandshake$1(websocket, stream, message) {
  3102. websocket._readyState = WebSocket$1.CLOSING;
  3103. const err = new Error(message);
  3104. Error.captureStackTrace(err, abortHandshake$1);
  3105. if (stream.setHeader) {
  3106. stream[kAborted] = true;
  3107. stream.abort();
  3108. if (stream.socket && !stream.socket.destroyed) {
  3109. //
  3110. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  3111. // called after the request completed. See
  3112. // https://github.com/websockets/ws/issues/1869.
  3113. //
  3114. stream.socket.destroy();
  3115. }
  3116. process.nextTick(emitErrorAndClose, websocket, err);
  3117. } else {
  3118. stream.destroy(err);
  3119. stream.once('error', websocket.emit.bind(websocket, 'error'));
  3120. stream.once('close', websocket.emitClose.bind(websocket));
  3121. }
  3122. }
  3123. /**
  3124. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  3125. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  3126. *
  3127. * @param {WebSocket} websocket The WebSocket instance
  3128. * @param {*} [data] The data to send
  3129. * @param {Function} [cb] Callback
  3130. * @private
  3131. */
  3132. function sendAfterClose(websocket, data, cb) {
  3133. if (data) {
  3134. const length = toBuffer(data).length;
  3135. //
  3136. // The `_bufferedAmount` property is used only when the peer is a client and
  3137. // the opening handshake fails. Under these circumstances, in fact, the
  3138. // `setSocket()` method is not called, so the `_socket` and `_sender`
  3139. // properties are set to `null`.
  3140. //
  3141. if (websocket._socket) websocket._sender._bufferedBytes += length;
  3142. else websocket._bufferedAmount += length;
  3143. }
  3144. if (cb) {
  3145. const err = new Error(
  3146. `WebSocket is not open: readyState ${websocket.readyState} ` +
  3147. `(${readyStates[websocket.readyState]})`
  3148. );
  3149. cb(err);
  3150. }
  3151. }
  3152. /**
  3153. * The listener of the `Receiver` `'conclude'` event.
  3154. *
  3155. * @param {Number} code The status code
  3156. * @param {Buffer} reason The reason for closing
  3157. * @private
  3158. */
  3159. function receiverOnConclude(code, reason) {
  3160. const websocket = this[kWebSocket$1];
  3161. websocket._closeFrameReceived = true;
  3162. websocket._closeMessage = reason;
  3163. websocket._closeCode = code;
  3164. if (websocket._socket[kWebSocket$1] === undefined) return;
  3165. websocket._socket.removeListener('data', socketOnData);
  3166. process.nextTick(resume, websocket._socket);
  3167. if (code === 1005) websocket.close();
  3168. else websocket.close(code, reason);
  3169. }
  3170. /**
  3171. * The listener of the `Receiver` `'drain'` event.
  3172. *
  3173. * @private
  3174. */
  3175. function receiverOnDrain() {
  3176. const websocket = this[kWebSocket$1];
  3177. if (!websocket.isPaused) websocket._socket.resume();
  3178. }
  3179. /**
  3180. * The listener of the `Receiver` `'error'` event.
  3181. *
  3182. * @param {(RangeError|Error)} err The emitted error
  3183. * @private
  3184. */
  3185. function receiverOnError(err) {
  3186. const websocket = this[kWebSocket$1];
  3187. if (websocket._socket[kWebSocket$1] !== undefined) {
  3188. websocket._socket.removeListener('data', socketOnData);
  3189. //
  3190. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  3191. // https://github.com/websockets/ws/issues/1940.
  3192. //
  3193. process.nextTick(resume, websocket._socket);
  3194. websocket.close(err[kStatusCode]);
  3195. }
  3196. websocket.emit('error', err);
  3197. }
  3198. /**
  3199. * The listener of the `Receiver` `'finish'` event.
  3200. *
  3201. * @private
  3202. */
  3203. function receiverOnFinish() {
  3204. this[kWebSocket$1].emitClose();
  3205. }
  3206. /**
  3207. * The listener of the `Receiver` `'message'` event.
  3208. *
  3209. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  3210. * @param {Boolean} isBinary Specifies whether the message is binary or not
  3211. * @private
  3212. */
  3213. function receiverOnMessage(data, isBinary) {
  3214. this[kWebSocket$1].emit('message', data, isBinary);
  3215. }
  3216. /**
  3217. * The listener of the `Receiver` `'ping'` event.
  3218. *
  3219. * @param {Buffer} data The data included in the ping frame
  3220. * @private
  3221. */
  3222. function receiverOnPing(data) {
  3223. const websocket = this[kWebSocket$1];
  3224. websocket.pong(data, !websocket._isServer, NOOP);
  3225. websocket.emit('ping', data);
  3226. }
  3227. /**
  3228. * The listener of the `Receiver` `'pong'` event.
  3229. *
  3230. * @param {Buffer} data The data included in the pong frame
  3231. * @private
  3232. */
  3233. function receiverOnPong(data) {
  3234. this[kWebSocket$1].emit('pong', data);
  3235. }
  3236. /**
  3237. * Resume a readable stream
  3238. *
  3239. * @param {Readable} stream The readable stream
  3240. * @private
  3241. */
  3242. function resume(stream) {
  3243. stream.resume();
  3244. }
  3245. /**
  3246. * The listener of the `net.Socket` `'close'` event.
  3247. *
  3248. * @private
  3249. */
  3250. function socketOnClose() {
  3251. const websocket = this[kWebSocket$1];
  3252. this.removeListener('close', socketOnClose);
  3253. this.removeListener('data', socketOnData);
  3254. this.removeListener('end', socketOnEnd);
  3255. websocket._readyState = WebSocket$1.CLOSING;
  3256. let chunk;
  3257. //
  3258. // The close frame might not have been received or the `'end'` event emitted,
  3259. // for example, if the socket was destroyed due to an error. Ensure that the
  3260. // `receiver` stream is closed after writing any remaining buffered data to
  3261. // it. If the readable side of the socket is in flowing mode then there is no
  3262. // buffered data as everything has been already written and `readable.read()`
  3263. // will return `null`. If instead, the socket is paused, any possible buffered
  3264. // data will be read as a single chunk.
  3265. //
  3266. if (
  3267. !this._readableState.endEmitted &&
  3268. !websocket._closeFrameReceived &&
  3269. !websocket._receiver._writableState.errorEmitted &&
  3270. (chunk = websocket._socket.read()) !== null
  3271. ) {
  3272. websocket._receiver.write(chunk);
  3273. }
  3274. websocket._receiver.end();
  3275. this[kWebSocket$1] = undefined;
  3276. clearTimeout(websocket._closeTimer);
  3277. if (
  3278. websocket._receiver._writableState.finished ||
  3279. websocket._receiver._writableState.errorEmitted
  3280. ) {
  3281. websocket.emitClose();
  3282. } else {
  3283. websocket._receiver.on('error', receiverOnFinish);
  3284. websocket._receiver.on('finish', receiverOnFinish);
  3285. }
  3286. }
  3287. /**
  3288. * The listener of the `net.Socket` `'data'` event.
  3289. *
  3290. * @param {Buffer} chunk A chunk of data
  3291. * @private
  3292. */
  3293. function socketOnData(chunk) {
  3294. if (!this[kWebSocket$1]._receiver.write(chunk)) {
  3295. this.pause();
  3296. }
  3297. }
  3298. /**
  3299. * The listener of the `net.Socket` `'end'` event.
  3300. *
  3301. * @private
  3302. */
  3303. function socketOnEnd() {
  3304. const websocket = this[kWebSocket$1];
  3305. websocket._readyState = WebSocket$1.CLOSING;
  3306. websocket._receiver.end();
  3307. this.end();
  3308. }
  3309. /**
  3310. * The listener of the `net.Socket` `'error'` event.
  3311. *
  3312. * @private
  3313. */
  3314. function socketOnError$1() {
  3315. const websocket = this[kWebSocket$1];
  3316. this.removeListener('error', socketOnError$1);
  3317. this.on('error', NOOP);
  3318. if (websocket) {
  3319. websocket._readyState = WebSocket$1.CLOSING;
  3320. this.destroy();
  3321. }
  3322. }
  3323. const { tokenChars } = validation.exports;
  3324. /**
  3325. * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
  3326. *
  3327. * @param {String} header The field value of the header
  3328. * @return {Set} The subprotocol names
  3329. * @public
  3330. */
  3331. function parse(header) {
  3332. const protocols = new Set();
  3333. let start = -1;
  3334. let end = -1;
  3335. let i = 0;
  3336. for (i; i < header.length; i++) {
  3337. const code = header.charCodeAt(i);
  3338. if (end === -1 && tokenChars[code] === 1) {
  3339. if (start === -1) start = i;
  3340. } else if (
  3341. i !== 0 &&
  3342. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  3343. ) {
  3344. if (end === -1 && start !== -1) end = i;
  3345. } else if (code === 0x2c /* ',' */) {
  3346. if (start === -1) {
  3347. throw new SyntaxError(`Unexpected character at index ${i}`);
  3348. }
  3349. if (end === -1) end = i;
  3350. const protocol = header.slice(start, end);
  3351. if (protocols.has(protocol)) {
  3352. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  3353. }
  3354. protocols.add(protocol);
  3355. start = end = -1;
  3356. } else {
  3357. throw new SyntaxError(`Unexpected character at index ${i}`);
  3358. }
  3359. }
  3360. if (start === -1 || end !== -1) {
  3361. throw new SyntaxError('Unexpected end of input');
  3362. }
  3363. const protocol = header.slice(start, i);
  3364. if (protocols.has(protocol)) {
  3365. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  3366. }
  3367. protocols.add(protocol);
  3368. return protocols;
  3369. }
  3370. var subprotocol$1 = { parse };
  3371. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
  3372. const EventEmitter = require$$2;
  3373. const http = require$$2$1;
  3374. const { createHash } = require$$5;
  3375. const extension = extension$1;
  3376. const PerMessageDeflate = permessageDeflate;
  3377. const subprotocol = subprotocol$1;
  3378. const WebSocket = websocket;
  3379. const { GUID, kWebSocket } = constants;
  3380. const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
  3381. const RUNNING = 0;
  3382. const CLOSING = 1;
  3383. const CLOSED = 2;
  3384. /**
  3385. * Class representing a WebSocket server.
  3386. *
  3387. * @extends EventEmitter
  3388. */
  3389. class WebSocketServer extends EventEmitter {
  3390. /**
  3391. * Create a `WebSocketServer` instance.
  3392. *
  3393. * @param {Object} options Configuration options
  3394. * @param {Number} [options.backlog=511] The maximum length of the queue of
  3395. * pending connections
  3396. * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
  3397. * track clients
  3398. * @param {Function} [options.handleProtocols] A hook to handle protocols
  3399. * @param {String} [options.host] The hostname where to bind the server
  3400. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  3401. * size
  3402. * @param {Boolean} [options.noServer=false] Enable no server mode
  3403. * @param {String} [options.path] Accept only connections matching this path
  3404. * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
  3405. * permessage-deflate
  3406. * @param {Number} [options.port] The port where to bind the server
  3407. * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
  3408. * server to use
  3409. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  3410. * not to skip UTF-8 validation for text and close messages
  3411. * @param {Function} [options.verifyClient] A hook to reject connections
  3412. * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
  3413. * class to use. It must be the `WebSocket` class or class that extends it
  3414. * @param {Function} [callback] A listener for the `listening` event
  3415. */
  3416. constructor(options, callback) {
  3417. super();
  3418. options = {
  3419. maxPayload: 100 * 1024 * 1024,
  3420. skipUTF8Validation: false,
  3421. perMessageDeflate: false,
  3422. handleProtocols: null,
  3423. clientTracking: true,
  3424. verifyClient: null,
  3425. noServer: false,
  3426. backlog: null, // use default (511 as implemented in net.js)
  3427. server: null,
  3428. host: null,
  3429. path: null,
  3430. port: null,
  3431. WebSocket,
  3432. ...options
  3433. };
  3434. if (
  3435. (options.port == null && !options.server && !options.noServer) ||
  3436. (options.port != null && (options.server || options.noServer)) ||
  3437. (options.server && options.noServer)
  3438. ) {
  3439. throw new TypeError(
  3440. 'One and only one of the "port", "server", or "noServer" options ' +
  3441. 'must be specified'
  3442. );
  3443. }
  3444. if (options.port != null) {
  3445. this._server = http.createServer((req, res) => {
  3446. const body = http.STATUS_CODES[426];
  3447. res.writeHead(426, {
  3448. 'Content-Length': body.length,
  3449. 'Content-Type': 'text/plain'
  3450. });
  3451. res.end(body);
  3452. });
  3453. this._server.listen(
  3454. options.port,
  3455. options.host,
  3456. options.backlog,
  3457. callback
  3458. );
  3459. } else if (options.server) {
  3460. this._server = options.server;
  3461. }
  3462. if (this._server) {
  3463. const emitConnection = this.emit.bind(this, 'connection');
  3464. this._removeListeners = addListeners(this._server, {
  3465. listening: this.emit.bind(this, 'listening'),
  3466. error: this.emit.bind(this, 'error'),
  3467. upgrade: (req, socket, head) => {
  3468. this.handleUpgrade(req, socket, head, emitConnection);
  3469. }
  3470. });
  3471. }
  3472. if (options.perMessageDeflate === true) options.perMessageDeflate = {};
  3473. if (options.clientTracking) {
  3474. this.clients = new Set();
  3475. this._shouldEmitClose = false;
  3476. }
  3477. this.options = options;
  3478. this._state = RUNNING;
  3479. }
  3480. /**
  3481. * Returns the bound address, the address family name, and port of the server
  3482. * as reported by the operating system if listening on an IP socket.
  3483. * If the server is listening on a pipe or UNIX domain socket, the name is
  3484. * returned as a string.
  3485. *
  3486. * @return {(Object|String|null)} The address of the server
  3487. * @public
  3488. */
  3489. address() {
  3490. if (this.options.noServer) {
  3491. throw new Error('The server is operating in "noServer" mode');
  3492. }
  3493. if (!this._server) return null;
  3494. return this._server.address();
  3495. }
  3496. /**
  3497. * Stop the server from accepting new connections and emit the `'close'` event
  3498. * when all existing connections are closed.
  3499. *
  3500. * @param {Function} [cb] A one-time listener for the `'close'` event
  3501. * @public
  3502. */
  3503. close(cb) {
  3504. if (this._state === CLOSED) {
  3505. if (cb) {
  3506. this.once('close', () => {
  3507. cb(new Error('The server is not running'));
  3508. });
  3509. }
  3510. process.nextTick(emitClose, this);
  3511. return;
  3512. }
  3513. if (cb) this.once('close', cb);
  3514. if (this._state === CLOSING) return;
  3515. this._state = CLOSING;
  3516. if (this.options.noServer || this.options.server) {
  3517. if (this._server) {
  3518. this._removeListeners();
  3519. this._removeListeners = this._server = null;
  3520. }
  3521. if (this.clients) {
  3522. if (!this.clients.size) {
  3523. process.nextTick(emitClose, this);
  3524. } else {
  3525. this._shouldEmitClose = true;
  3526. }
  3527. } else {
  3528. process.nextTick(emitClose, this);
  3529. }
  3530. } else {
  3531. const server = this._server;
  3532. this._removeListeners();
  3533. this._removeListeners = this._server = null;
  3534. //
  3535. // The HTTP/S server was created internally. Close it, and rely on its
  3536. // `'close'` event.
  3537. //
  3538. server.close(() => {
  3539. emitClose(this);
  3540. });
  3541. }
  3542. }
  3543. /**
  3544. * See if a given request should be handled by this server instance.
  3545. *
  3546. * @param {http.IncomingMessage} req Request object to inspect
  3547. * @return {Boolean} `true` if the request is valid, else `false`
  3548. * @public
  3549. */
  3550. shouldHandle(req) {
  3551. if (this.options.path) {
  3552. const index = req.url.indexOf('?');
  3553. const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
  3554. if (pathname !== this.options.path) return false;
  3555. }
  3556. return true;
  3557. }
  3558. /**
  3559. * Handle a HTTP Upgrade request.
  3560. *
  3561. * @param {http.IncomingMessage} req The request object
  3562. * @param {(net.Socket|tls.Socket)} socket The network socket between the
  3563. * server and client
  3564. * @param {Buffer} head The first packet of the upgraded stream
  3565. * @param {Function} cb Callback
  3566. * @public
  3567. */
  3568. handleUpgrade(req, socket, head, cb) {
  3569. socket.on('error', socketOnError);
  3570. const key = req.headers['sec-websocket-key'];
  3571. const version = +req.headers['sec-websocket-version'];
  3572. if (req.method !== 'GET') {
  3573. const message = 'Invalid HTTP method';
  3574. abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
  3575. return;
  3576. }
  3577. if (req.headers.upgrade.toLowerCase() !== 'websocket') {
  3578. const message = 'Invalid Upgrade header';
  3579. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  3580. return;
  3581. }
  3582. if (!key || !keyRegex.test(key)) {
  3583. const message = 'Missing or invalid Sec-WebSocket-Key header';
  3584. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  3585. return;
  3586. }
  3587. if (version !== 8 && version !== 13) {
  3588. const message = 'Missing or invalid Sec-WebSocket-Version header';
  3589. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  3590. return;
  3591. }
  3592. if (!this.shouldHandle(req)) {
  3593. abortHandshake(socket, 400);
  3594. return;
  3595. }
  3596. const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
  3597. let protocols = new Set();
  3598. if (secWebSocketProtocol !== undefined) {
  3599. try {
  3600. protocols = subprotocol.parse(secWebSocketProtocol);
  3601. } catch (err) {
  3602. const message = 'Invalid Sec-WebSocket-Protocol header';
  3603. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  3604. return;
  3605. }
  3606. }
  3607. const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
  3608. const extensions = {};
  3609. if (
  3610. this.options.perMessageDeflate &&
  3611. secWebSocketExtensions !== undefined
  3612. ) {
  3613. const perMessageDeflate = new PerMessageDeflate(
  3614. this.options.perMessageDeflate,
  3615. true,
  3616. this.options.maxPayload
  3617. );
  3618. try {
  3619. const offers = extension.parse(secWebSocketExtensions);
  3620. if (offers[PerMessageDeflate.extensionName]) {
  3621. perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
  3622. extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
  3623. }
  3624. } catch (err) {
  3625. const message =
  3626. 'Invalid or unacceptable Sec-WebSocket-Extensions header';
  3627. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  3628. return;
  3629. }
  3630. }
  3631. //
  3632. // Optionally call external client verification handler.
  3633. //
  3634. if (this.options.verifyClient) {
  3635. const info = {
  3636. origin:
  3637. req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
  3638. secure: !!(req.socket.authorized || req.socket.encrypted),
  3639. req
  3640. };
  3641. if (this.options.verifyClient.length === 2) {
  3642. this.options.verifyClient(info, (verified, code, message, headers) => {
  3643. if (!verified) {
  3644. return abortHandshake(socket, code || 401, message, headers);
  3645. }
  3646. this.completeUpgrade(
  3647. extensions,
  3648. key,
  3649. protocols,
  3650. req,
  3651. socket,
  3652. head,
  3653. cb
  3654. );
  3655. });
  3656. return;
  3657. }
  3658. if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
  3659. }
  3660. this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
  3661. }
  3662. /**
  3663. * Upgrade the connection to WebSocket.
  3664. *
  3665. * @param {Object} extensions The accepted extensions
  3666. * @param {String} key The value of the `Sec-WebSocket-Key` header
  3667. * @param {Set} protocols The subprotocols
  3668. * @param {http.IncomingMessage} req The request object
  3669. * @param {(net.Socket|tls.Socket)} socket The network socket between the
  3670. * server and client
  3671. * @param {Buffer} head The first packet of the upgraded stream
  3672. * @param {Function} cb Callback
  3673. * @throws {Error} If called more than once with the same socket
  3674. * @private
  3675. */
  3676. completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
  3677. //
  3678. // Destroy the socket if the client has already sent a FIN packet.
  3679. //
  3680. if (!socket.readable || !socket.writable) return socket.destroy();
  3681. if (socket[kWebSocket]) {
  3682. throw new Error(
  3683. 'server.handleUpgrade() was called more than once with the same ' +
  3684. 'socket, possibly due to a misconfiguration'
  3685. );
  3686. }
  3687. if (this._state > RUNNING) return abortHandshake(socket, 503);
  3688. const digest = createHash('sha1')
  3689. .update(key + GUID)
  3690. .digest('base64');
  3691. const headers = [
  3692. 'HTTP/1.1 101 Switching Protocols',
  3693. 'Upgrade: websocket',
  3694. 'Connection: Upgrade',
  3695. `Sec-WebSocket-Accept: ${digest}`
  3696. ];
  3697. const ws = new this.options.WebSocket(null);
  3698. if (protocols.size) {
  3699. //
  3700. // Optionally call external protocol selection handler.
  3701. //
  3702. const protocol = this.options.handleProtocols
  3703. ? this.options.handleProtocols(protocols, req)
  3704. : protocols.values().next().value;
  3705. if (protocol) {
  3706. headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
  3707. ws._protocol = protocol;
  3708. }
  3709. }
  3710. if (extensions[PerMessageDeflate.extensionName]) {
  3711. const params = extensions[PerMessageDeflate.extensionName].params;
  3712. const value = extension.format({
  3713. [PerMessageDeflate.extensionName]: [params]
  3714. });
  3715. headers.push(`Sec-WebSocket-Extensions: ${value}`);
  3716. ws._extensions = extensions;
  3717. }
  3718. //
  3719. // Allow external modification/inspection of handshake headers.
  3720. //
  3721. this.emit('headers', headers, req);
  3722. socket.write(headers.concat('\r\n').join('\r\n'));
  3723. socket.removeListener('error', socketOnError);
  3724. ws.setSocket(socket, head, {
  3725. maxPayload: this.options.maxPayload,
  3726. skipUTF8Validation: this.options.skipUTF8Validation
  3727. });
  3728. if (this.clients) {
  3729. this.clients.add(ws);
  3730. ws.on('close', () => {
  3731. this.clients.delete(ws);
  3732. if (this._shouldEmitClose && !this.clients.size) {
  3733. process.nextTick(emitClose, this);
  3734. }
  3735. });
  3736. }
  3737. cb(ws, req);
  3738. }
  3739. }
  3740. var websocketServer = WebSocketServer;
  3741. /**
  3742. * Add event listeners on an `EventEmitter` using a map of <event, listener>
  3743. * pairs.
  3744. *
  3745. * @param {EventEmitter} server The event emitter
  3746. * @param {Object.<String, Function>} map The listeners to add
  3747. * @return {Function} A function that will remove the added listeners when
  3748. * called
  3749. * @private
  3750. */
  3751. function addListeners(server, map) {
  3752. for (const event of Object.keys(map)) server.on(event, map[event]);
  3753. return function removeListeners() {
  3754. for (const event of Object.keys(map)) {
  3755. server.removeListener(event, map[event]);
  3756. }
  3757. };
  3758. }
  3759. /**
  3760. * Emit a `'close'` event on an `EventEmitter`.
  3761. *
  3762. * @param {EventEmitter} server The event emitter
  3763. * @private
  3764. */
  3765. function emitClose(server) {
  3766. server._state = CLOSED;
  3767. server.emit('close');
  3768. }
  3769. /**
  3770. * Handle socket errors.
  3771. *
  3772. * @private
  3773. */
  3774. function socketOnError() {
  3775. this.destroy();
  3776. }
  3777. /**
  3778. * Close the connection when preconditions are not fulfilled.
  3779. *
  3780. * @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
  3781. * @param {Number} code The HTTP response status code
  3782. * @param {String} [message] The HTTP response body
  3783. * @param {Object} [headers] Additional HTTP response headers
  3784. * @private
  3785. */
  3786. function abortHandshake(socket, code, message, headers) {
  3787. //
  3788. // The socket is writable unless the user destroyed or ended it before calling
  3789. // `server.handleUpgrade()` or in the `verifyClient` function, which is a user
  3790. // error. Handling this does not make much sense as the worst that can happen
  3791. // is that some of the data written by the user might be discarded due to the
  3792. // call to `socket.end()` below, which triggers an `'error'` event that in
  3793. // turn causes the socket to be destroyed.
  3794. //
  3795. message = message || http.STATUS_CODES[code];
  3796. headers = {
  3797. Connection: 'close',
  3798. 'Content-Type': 'text/html',
  3799. 'Content-Length': Buffer.byteLength(message),
  3800. ...headers
  3801. };
  3802. socket.once('finish', socket.destroy);
  3803. socket.end(
  3804. `HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
  3805. Object.keys(headers)
  3806. .map((h) => `${h}: ${headers[h]}`)
  3807. .join('\r\n') +
  3808. '\r\n\r\n' +
  3809. message
  3810. );
  3811. }
  3812. /**
  3813. * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
  3814. * one listener for it, otherwise call `abortHandshake()`.
  3815. *
  3816. * @param {WebSocketServer} server The WebSocket server
  3817. * @param {http.IncomingMessage} req The request object
  3818. * @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
  3819. * @param {Number} code The HTTP response status code
  3820. * @param {String} message The HTTP response body
  3821. * @private
  3822. */
  3823. function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
  3824. if (server.listenerCount('wsClientError')) {
  3825. const err = new Error(message);
  3826. Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
  3827. server.emit('wsClientError', err, socket, req);
  3828. } else {
  3829. abortHandshake(socket, code, message);
  3830. }
  3831. }
  3832. function setup(ctx) {
  3833. var _a;
  3834. const wss = new websocketServer({ noServer: true });
  3835. const clients = /* @__PURE__ */ new Map();
  3836. (_a = ctx.server.httpServer) == null ? void 0 : _a.on("upgrade", (request, socket, head) => {
  3837. if (!request.url)
  3838. return;
  3839. const { pathname } = new URL(request.url, "http://localhost");
  3840. if (pathname !== API_PATH)
  3841. return;
  3842. wss.handleUpgrade(request, socket, head, (ws) => {
  3843. wss.emit("connection", ws, request);
  3844. setupClient(ws);
  3845. });
  3846. });
  3847. function setupClient(ws) {
  3848. const rpc = createBirpc(
  3849. {
  3850. async onWatcherStart() {
  3851. await ctx.report("onWatcherStart");
  3852. },
  3853. async onFinished() {
  3854. await ctx.report("onFinished");
  3855. },
  3856. async onCollected(files) {
  3857. ctx.state.collectFiles(files);
  3858. await ctx.report("onCollected", files);
  3859. },
  3860. async onTaskUpdate(packs) {
  3861. ctx.state.updateTasks(packs);
  3862. await ctx.report("onTaskUpdate", packs);
  3863. },
  3864. getFiles() {
  3865. return ctx.state.getFiles();
  3866. },
  3867. async getPaths() {
  3868. return await ctx.state.getPaths();
  3869. },
  3870. readFile(id) {
  3871. return promises.readFile(id, "utf-8");
  3872. },
  3873. writeFile(id, content) {
  3874. return promises.writeFile(id, content, "utf-8");
  3875. },
  3876. async rerun(files) {
  3877. await ctx.rerunFiles(files);
  3878. },
  3879. getConfig() {
  3880. return ctx.config;
  3881. },
  3882. async getTransformResult(id) {
  3883. const result = await ctx.vitenode.transformRequest(id);
  3884. if (result) {
  3885. try {
  3886. result.source = result.source || await promises.readFile(id, "utf-8");
  3887. } catch {
  3888. }
  3889. return result;
  3890. }
  3891. },
  3892. async getModuleGraph(id) {
  3893. const graph = {};
  3894. const externalized = /* @__PURE__ */ new Set();
  3895. const inlined = /* @__PURE__ */ new Set();
  3896. function clearId(id2) {
  3897. return (id2 == null ? void 0 : id2.replace(/\?v=\w+$/, "")) || "";
  3898. }
  3899. async function get(mod, seen = /* @__PURE__ */ new Map()) {
  3900. if (!mod || !mod.id)
  3901. return;
  3902. if (seen.has(mod))
  3903. return seen.get(mod);
  3904. let id2 = clearId(mod.id);
  3905. seen.set(mod, id2);
  3906. const rewrote = await ctx.vitenode.shouldExternalize(id2);
  3907. if (rewrote) {
  3908. id2 = rewrote;
  3909. externalized.add(id2);
  3910. seen.set(mod, id2);
  3911. } else {
  3912. inlined.add(id2);
  3913. }
  3914. const mods = Array.from(mod.importedModules).filter((i) => i.id && !i.id.includes("/vitest/dist/"));
  3915. graph[id2] = (await Promise.all(mods.map((m) => get(m, seen)))).filter(Boolean);
  3916. return id2;
  3917. }
  3918. await get(ctx.server.moduleGraph.getModuleById(id));
  3919. return {
  3920. graph,
  3921. externalized: Array.from(externalized),
  3922. inlined: Array.from(inlined)
  3923. };
  3924. },
  3925. updateSnapshot(file) {
  3926. if (!file)
  3927. return ctx.updateSnapshot();
  3928. return ctx.updateSnapshot([file.filepath]);
  3929. }
  3930. },
  3931. {
  3932. post: (msg) => ws.send(msg),
  3933. on: (fn) => ws.on("message", fn),
  3934. eventNames: ["onUserConsoleLog", "onFinished", "onCollected"],
  3935. serialize: stringify,
  3936. deserialize: parse$3
  3937. }
  3938. );
  3939. clients.set(ws, rpc);
  3940. ws.on("close", () => {
  3941. clients.delete(ws);
  3942. });
  3943. }
  3944. ctx.reporters.push(new WebSocketReporter(ctx, wss, clients));
  3945. }
  3946. class WebSocketReporter {
  3947. constructor(ctx, wss, clients) {
  3948. this.ctx = ctx;
  3949. this.wss = wss;
  3950. this.clients = clients;
  3951. }
  3952. onCollected(files) {
  3953. if (this.clients.size === 0)
  3954. return;
  3955. this.clients.forEach((client) => {
  3956. var _a;
  3957. (_a = client.onCollected) == null ? void 0 : _a.call(client, files);
  3958. });
  3959. }
  3960. async onTaskUpdate(packs) {
  3961. if (this.clients.size === 0)
  3962. return;
  3963. await Promise.all(packs.map(async (i) => {
  3964. var _a;
  3965. if ((_a = i[1]) == null ? void 0 : _a.error)
  3966. await interpretSourcePos(parseStacktrace(i[1].error), this.ctx);
  3967. }));
  3968. this.clients.forEach((client) => {
  3969. var _a;
  3970. (_a = client.onTaskUpdate) == null ? void 0 : _a.call(client, packs);
  3971. });
  3972. }
  3973. onFinished(files) {
  3974. this.clients.forEach((client) => {
  3975. var _a;
  3976. (_a = client.onFinished) == null ? void 0 : _a.call(client, files);
  3977. });
  3978. }
  3979. onUserConsoleLog(log) {
  3980. this.clients.forEach((client) => {
  3981. var _a;
  3982. (_a = client.onUserConsoleLog) == null ? void 0 : _a.call(client, log);
  3983. });
  3984. }
  3985. }
  3986. export { setup };