websocket.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$" }] */
  2. 'use strict';
  3. const EventEmitter = require('events');
  4. const https = require('https');
  5. const http = require('http');
  6. const net = require('net');
  7. const tls = require('tls');
  8. const { randomBytes, createHash } = require('crypto');
  9. const { Duplex, Readable } = require('stream');
  10. const { URL } = require('url');
  11. const PerMessageDeflate = require('./permessage-deflate');
  12. const Receiver = require('./receiver');
  13. const Sender = require('./sender');
  14. const {
  15. BINARY_TYPES,
  16. EMPTY_BUFFER,
  17. GUID,
  18. kForOnEventAttribute,
  19. kListener,
  20. kStatusCode,
  21. kWebSocket,
  22. NOOP
  23. } = require('./constants');
  24. const {
  25. EventTarget: { addEventListener, removeEventListener }
  26. } = require('./event-target');
  27. const { format, parse } = require('./extension');
  28. const { toBuffer } = require('./buffer-util');
  29. const closeTimeout = 30 * 1000;
  30. const kAborted = Symbol('kAborted');
  31. const protocolVersions = [8, 13];
  32. const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
  33. const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
  34. /**
  35. * Class representing a WebSocket.
  36. *
  37. * @extends EventEmitter
  38. */
  39. class WebSocket extends EventEmitter {
  40. /**
  41. * Create a new `WebSocket`.
  42. *
  43. * @param {(String|URL)} address The URL to which to connect
  44. * @param {(String|String[])} [protocols] The subprotocols
  45. * @param {Object} [options] Connection options
  46. */
  47. constructor(address, protocols, options) {
  48. super();
  49. this._binaryType = BINARY_TYPES[0];
  50. this._closeCode = 1006;
  51. this._closeFrameReceived = false;
  52. this._closeFrameSent = false;
  53. this._closeMessage = EMPTY_BUFFER;
  54. this._closeTimer = null;
  55. this._extensions = {};
  56. this._paused = false;
  57. this._protocol = '';
  58. this._readyState = WebSocket.CONNECTING;
  59. this._receiver = null;
  60. this._sender = null;
  61. this._socket = null;
  62. if (address !== null) {
  63. this._bufferedAmount = 0;
  64. this._isServer = false;
  65. this._redirects = 0;
  66. if (protocols === undefined) {
  67. protocols = [];
  68. } else if (!Array.isArray(protocols)) {
  69. if (typeof protocols === 'object' && protocols !== null) {
  70. options = protocols;
  71. protocols = [];
  72. } else {
  73. protocols = [protocols];
  74. }
  75. }
  76. initAsClient(this, address, protocols, options);
  77. } else {
  78. this._autoPong = options.autoPong;
  79. this._isServer = true;
  80. }
  81. }
  82. /**
  83. * This deviates from the WHATWG interface since ws doesn't support the
  84. * required default "blob" type (instead we define a custom "nodebuffer"
  85. * type).
  86. *
  87. * @type {String}
  88. */
  89. get binaryType() {
  90. return this._binaryType;
  91. }
  92. set binaryType(type) {
  93. if (!BINARY_TYPES.includes(type)) return;
  94. this._binaryType = type;
  95. //
  96. // Allow to change `binaryType` on the fly.
  97. //
  98. if (this._receiver) this._receiver._binaryType = type;
  99. }
  100. /**
  101. * @type {Number}
  102. */
  103. get bufferedAmount() {
  104. if (!this._socket) return this._bufferedAmount;
  105. return this._socket._writableState.length + this._sender._bufferedBytes;
  106. }
  107. /**
  108. * @type {String}
  109. */
  110. get extensions() {
  111. return Object.keys(this._extensions).join();
  112. }
  113. /**
  114. * @type {Boolean}
  115. */
  116. get isPaused() {
  117. return this._paused;
  118. }
  119. /**
  120. * @type {Function}
  121. */
  122. /* istanbul ignore next */
  123. get onclose() {
  124. return null;
  125. }
  126. /**
  127. * @type {Function}
  128. */
  129. /* istanbul ignore next */
  130. get onerror() {
  131. return null;
  132. }
  133. /**
  134. * @type {Function}
  135. */
  136. /* istanbul ignore next */
  137. get onopen() {
  138. return null;
  139. }
  140. /**
  141. * @type {Function}
  142. */
  143. /* istanbul ignore next */
  144. get onmessage() {
  145. return null;
  146. }
  147. /**
  148. * @type {String}
  149. */
  150. get protocol() {
  151. return this._protocol;
  152. }
  153. /**
  154. * @type {Number}
  155. */
  156. get readyState() {
  157. return this._readyState;
  158. }
  159. /**
  160. * @type {String}
  161. */
  162. get url() {
  163. return this._url;
  164. }
  165. /**
  166. * Set up the socket and the internal resources.
  167. *
  168. * @param {Duplex} socket The network socket between the server and client
  169. * @param {Buffer} head The first packet of the upgraded stream
  170. * @param {Object} options Options object
  171. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  172. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  173. * multiple times in the same tick
  174. * @param {Function} [options.generateMask] The function used to generate the
  175. * masking key
  176. * @param {Number} [options.maxPayload=0] The maximum allowed message size
  177. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  178. * not to skip UTF-8 validation for text and close messages
  179. * @private
  180. */
  181. setSocket(socket, head, options) {
  182. const receiver = new Receiver({
  183. allowSynchronousEvents: options.allowSynchronousEvents,
  184. binaryType: this.binaryType,
  185. extensions: this._extensions,
  186. isServer: this._isServer,
  187. maxPayload: options.maxPayload,
  188. skipUTF8Validation: options.skipUTF8Validation
  189. });
  190. this._sender = new Sender(socket, this._extensions, options.generateMask);
  191. this._receiver = receiver;
  192. this._socket = socket;
  193. receiver[kWebSocket] = this;
  194. socket[kWebSocket] = this;
  195. receiver.on('conclude', receiverOnConclude);
  196. receiver.on('drain', receiverOnDrain);
  197. receiver.on('error', receiverOnError);
  198. receiver.on('message', receiverOnMessage);
  199. receiver.on('ping', receiverOnPing);
  200. receiver.on('pong', receiverOnPong);
  201. //
  202. // These methods may not be available if `socket` is just a `Duplex`.
  203. //
  204. if (socket.setTimeout) socket.setTimeout(0);
  205. if (socket.setNoDelay) socket.setNoDelay();
  206. if (head.length > 0) socket.unshift(head);
  207. socket.on('close', socketOnClose);
  208. socket.on('data', socketOnData);
  209. socket.on('end', socketOnEnd);
  210. socket.on('error', socketOnError);
  211. this._readyState = WebSocket.OPEN;
  212. this.emit('open');
  213. }
  214. /**
  215. * Emit the `'close'` event.
  216. *
  217. * @private
  218. */
  219. emitClose() {
  220. if (!this._socket) {
  221. this._readyState = WebSocket.CLOSED;
  222. this.emit('close', this._closeCode, this._closeMessage);
  223. return;
  224. }
  225. if (this._extensions[PerMessageDeflate.extensionName]) {
  226. this._extensions[PerMessageDeflate.extensionName].cleanup();
  227. }
  228. this._receiver.removeAllListeners();
  229. this._readyState = WebSocket.CLOSED;
  230. this.emit('close', this._closeCode, this._closeMessage);
  231. }
  232. /**
  233. * Start a closing handshake.
  234. *
  235. * +----------+ +-----------+ +----------+
  236. * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
  237. * | +----------+ +-----------+ +----------+ |
  238. * +----------+ +-----------+ |
  239. * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
  240. * +----------+ +-----------+ |
  241. * | | | +---+ |
  242. * +------------------------+-->|fin| - - - -
  243. * | +---+ | +---+
  244. * - - - - -|fin|<---------------------+
  245. * +---+
  246. *
  247. * @param {Number} [code] Status code explaining why the connection is closing
  248. * @param {(String|Buffer)} [data] The reason why the connection is
  249. * closing
  250. * @public
  251. */
  252. close(code, data) {
  253. if (this.readyState === WebSocket.CLOSED) return;
  254. if (this.readyState === WebSocket.CONNECTING) {
  255. const msg = 'WebSocket was closed before the connection was established';
  256. abortHandshake(this, this._req, msg);
  257. return;
  258. }
  259. if (this.readyState === WebSocket.CLOSING) {
  260. if (
  261. this._closeFrameSent &&
  262. (this._closeFrameReceived || this._receiver._writableState.errorEmitted)
  263. ) {
  264. this._socket.end();
  265. }
  266. return;
  267. }
  268. this._readyState = WebSocket.CLOSING;
  269. this._sender.close(code, data, !this._isServer, (err) => {
  270. //
  271. // This error is handled by the `'error'` listener on the socket. We only
  272. // want to know if the close frame has been sent here.
  273. //
  274. if (err) return;
  275. this._closeFrameSent = true;
  276. if (
  277. this._closeFrameReceived ||
  278. this._receiver._writableState.errorEmitted
  279. ) {
  280. this._socket.end();
  281. }
  282. });
  283. //
  284. // Specify a timeout for the closing handshake to complete.
  285. //
  286. this._closeTimer = setTimeout(
  287. this._socket.destroy.bind(this._socket),
  288. closeTimeout
  289. );
  290. }
  291. /**
  292. * Pause the socket.
  293. *
  294. * @public
  295. */
  296. pause() {
  297. if (
  298. this.readyState === WebSocket.CONNECTING ||
  299. this.readyState === WebSocket.CLOSED
  300. ) {
  301. return;
  302. }
  303. this._paused = true;
  304. this._socket.pause();
  305. }
  306. /**
  307. * Send a ping.
  308. *
  309. * @param {*} [data] The data to send
  310. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  311. * @param {Function} [cb] Callback which is executed when the ping is sent
  312. * @public
  313. */
  314. ping(data, mask, cb) {
  315. if (this.readyState === WebSocket.CONNECTING) {
  316. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  317. }
  318. if (typeof data === 'function') {
  319. cb = data;
  320. data = mask = undefined;
  321. } else if (typeof mask === 'function') {
  322. cb = mask;
  323. mask = undefined;
  324. }
  325. if (typeof data === 'number') data = data.toString();
  326. if (this.readyState !== WebSocket.OPEN) {
  327. sendAfterClose(this, data, cb);
  328. return;
  329. }
  330. if (mask === undefined) mask = !this._isServer;
  331. this._sender.ping(data || EMPTY_BUFFER, mask, cb);
  332. }
  333. /**
  334. * Send a pong.
  335. *
  336. * @param {*} [data] The data to send
  337. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  338. * @param {Function} [cb] Callback which is executed when the pong is sent
  339. * @public
  340. */
  341. pong(data, mask, cb) {
  342. if (this.readyState === WebSocket.CONNECTING) {
  343. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  344. }
  345. if (typeof data === 'function') {
  346. cb = data;
  347. data = mask = undefined;
  348. } else if (typeof mask === 'function') {
  349. cb = mask;
  350. mask = undefined;
  351. }
  352. if (typeof data === 'number') data = data.toString();
  353. if (this.readyState !== WebSocket.OPEN) {
  354. sendAfterClose(this, data, cb);
  355. return;
  356. }
  357. if (mask === undefined) mask = !this._isServer;
  358. this._sender.pong(data || EMPTY_BUFFER, mask, cb);
  359. }
  360. /**
  361. * Resume the socket.
  362. *
  363. * @public
  364. */
  365. resume() {
  366. if (
  367. this.readyState === WebSocket.CONNECTING ||
  368. this.readyState === WebSocket.CLOSED
  369. ) {
  370. return;
  371. }
  372. this._paused = false;
  373. if (!this._receiver._writableState.needDrain) this._socket.resume();
  374. }
  375. /**
  376. * Send a data message.
  377. *
  378. * @param {*} data The message to send
  379. * @param {Object} [options] Options object
  380. * @param {Boolean} [options.binary] Specifies whether `data` is binary or
  381. * text
  382. * @param {Boolean} [options.compress] Specifies whether or not to compress
  383. * `data`
  384. * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
  385. * last one
  386. * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
  387. * @param {Function} [cb] Callback which is executed when data is written out
  388. * @public
  389. */
  390. send(data, options, cb) {
  391. if (this.readyState === WebSocket.CONNECTING) {
  392. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  393. }
  394. if (typeof options === 'function') {
  395. cb = options;
  396. options = {};
  397. }
  398. if (typeof data === 'number') data = data.toString();
  399. if (this.readyState !== WebSocket.OPEN) {
  400. sendAfterClose(this, data, cb);
  401. return;
  402. }
  403. const opts = {
  404. binary: typeof data !== 'string',
  405. mask: !this._isServer,
  406. compress: true,
  407. fin: true,
  408. ...options
  409. };
  410. if (!this._extensions[PerMessageDeflate.extensionName]) {
  411. opts.compress = false;
  412. }
  413. this._sender.send(data || EMPTY_BUFFER, opts, cb);
  414. }
  415. /**
  416. * Forcibly close the connection.
  417. *
  418. * @public
  419. */
  420. terminate() {
  421. if (this.readyState === WebSocket.CLOSED) return;
  422. if (this.readyState === WebSocket.CONNECTING) {
  423. const msg = 'WebSocket was closed before the connection was established';
  424. abortHandshake(this, this._req, msg);
  425. return;
  426. }
  427. if (this._socket) {
  428. this._readyState = WebSocket.CLOSING;
  429. this._socket.destroy();
  430. }
  431. }
  432. }
  433. /**
  434. * @constant {Number} CONNECTING
  435. * @memberof WebSocket
  436. */
  437. Object.defineProperty(WebSocket, 'CONNECTING', {
  438. enumerable: true,
  439. value: readyStates.indexOf('CONNECTING')
  440. });
  441. /**
  442. * @constant {Number} CONNECTING
  443. * @memberof WebSocket.prototype
  444. */
  445. Object.defineProperty(WebSocket.prototype, 'CONNECTING', {
  446. enumerable: true,
  447. value: readyStates.indexOf('CONNECTING')
  448. });
  449. /**
  450. * @constant {Number} OPEN
  451. * @memberof WebSocket
  452. */
  453. Object.defineProperty(WebSocket, 'OPEN', {
  454. enumerable: true,
  455. value: readyStates.indexOf('OPEN')
  456. });
  457. /**
  458. * @constant {Number} OPEN
  459. * @memberof WebSocket.prototype
  460. */
  461. Object.defineProperty(WebSocket.prototype, 'OPEN', {
  462. enumerable: true,
  463. value: readyStates.indexOf('OPEN')
  464. });
  465. /**
  466. * @constant {Number} CLOSING
  467. * @memberof WebSocket
  468. */
  469. Object.defineProperty(WebSocket, 'CLOSING', {
  470. enumerable: true,
  471. value: readyStates.indexOf('CLOSING')
  472. });
  473. /**
  474. * @constant {Number} CLOSING
  475. * @memberof WebSocket.prototype
  476. */
  477. Object.defineProperty(WebSocket.prototype, 'CLOSING', {
  478. enumerable: true,
  479. value: readyStates.indexOf('CLOSING')
  480. });
  481. /**
  482. * @constant {Number} CLOSED
  483. * @memberof WebSocket
  484. */
  485. Object.defineProperty(WebSocket, 'CLOSED', {
  486. enumerable: true,
  487. value: readyStates.indexOf('CLOSED')
  488. });
  489. /**
  490. * @constant {Number} CLOSED
  491. * @memberof WebSocket.prototype
  492. */
  493. Object.defineProperty(WebSocket.prototype, 'CLOSED', {
  494. enumerable: true,
  495. value: readyStates.indexOf('CLOSED')
  496. });
  497. [
  498. 'binaryType',
  499. 'bufferedAmount',
  500. 'extensions',
  501. 'isPaused',
  502. 'protocol',
  503. 'readyState',
  504. 'url'
  505. ].forEach((property) => {
  506. Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
  507. });
  508. //
  509. // Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
  510. // See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
  511. //
  512. ['open', 'error', 'close', 'message'].forEach((method) => {
  513. Object.defineProperty(WebSocket.prototype, `on${method}`, {
  514. enumerable: true,
  515. get() {
  516. for (const listener of this.listeners(method)) {
  517. if (listener[kForOnEventAttribute]) return listener[kListener];
  518. }
  519. return null;
  520. },
  521. set(handler) {
  522. for (const listener of this.listeners(method)) {
  523. if (listener[kForOnEventAttribute]) {
  524. this.removeListener(method, listener);
  525. break;
  526. }
  527. }
  528. if (typeof handler !== 'function') return;
  529. this.addEventListener(method, handler, {
  530. [kForOnEventAttribute]: true
  531. });
  532. }
  533. });
  534. });
  535. WebSocket.prototype.addEventListener = addEventListener;
  536. WebSocket.prototype.removeEventListener = removeEventListener;
  537. module.exports = WebSocket;
  538. /**
  539. * Initialize a WebSocket client.
  540. *
  541. * @param {WebSocket} websocket The client to initialize
  542. * @param {(String|URL)} address The URL to which to connect
  543. * @param {Array} protocols The subprotocols
  544. * @param {Object} [options] Connection options
  545. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether any
  546. * of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
  547. * times in the same tick
  548. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  549. * automatically send a pong in response to a ping
  550. * @param {Function} [options.finishRequest] A function which can be used to
  551. * customize the headers of each http request before it is sent
  552. * @param {Boolean} [options.followRedirects=false] Whether or not to follow
  553. * redirects
  554. * @param {Function} [options.generateMask] The function used to generate the
  555. * masking key
  556. * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
  557. * handshake request
  558. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  559. * size
  560. * @param {Number} [options.maxRedirects=10] The maximum number of redirects
  561. * allowed
  562. * @param {String} [options.origin] Value of the `Origin` or
  563. * `Sec-WebSocket-Origin` header
  564. * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
  565. * permessage-deflate
  566. * @param {Number} [options.protocolVersion=13] Value of the
  567. * `Sec-WebSocket-Version` header
  568. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  569. * not to skip UTF-8 validation for text and close messages
  570. * @private
  571. */
  572. function initAsClient(websocket, address, protocols, options) {
  573. const opts = {
  574. allowSynchronousEvents: false,
  575. autoPong: true,
  576. protocolVersion: protocolVersions[1],
  577. maxPayload: 100 * 1024 * 1024,
  578. skipUTF8Validation: false,
  579. perMessageDeflate: true,
  580. followRedirects: false,
  581. maxRedirects: 10,
  582. ...options,
  583. createConnection: undefined,
  584. socketPath: undefined,
  585. hostname: undefined,
  586. protocol: undefined,
  587. timeout: undefined,
  588. method: 'GET',
  589. host: undefined,
  590. path: undefined,
  591. port: undefined
  592. };
  593. websocket._autoPong = opts.autoPong;
  594. if (!protocolVersions.includes(opts.protocolVersion)) {
  595. throw new RangeError(
  596. `Unsupported protocol version: ${opts.protocolVersion} ` +
  597. `(supported versions: ${protocolVersions.join(', ')})`
  598. );
  599. }
  600. let parsedUrl;
  601. if (address instanceof URL) {
  602. parsedUrl = address;
  603. } else {
  604. try {
  605. parsedUrl = new URL(address);
  606. } catch (e) {
  607. throw new SyntaxError(`Invalid URL: ${address}`);
  608. }
  609. }
  610. if (parsedUrl.protocol === 'http:') {
  611. parsedUrl.protocol = 'ws:';
  612. } else if (parsedUrl.protocol === 'https:') {
  613. parsedUrl.protocol = 'wss:';
  614. }
  615. websocket._url = parsedUrl.href;
  616. const isSecure = parsedUrl.protocol === 'wss:';
  617. const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
  618. let invalidUrlMessage;
  619. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {
  620. invalidUrlMessage =
  621. 'The URL\'s protocol must be one of "ws:", "wss:", ' +
  622. '"http:", "https", or "ws+unix:"';
  623. } else if (isIpcUrl && !parsedUrl.pathname) {
  624. invalidUrlMessage = "The URL's pathname is empty";
  625. } else if (parsedUrl.hash) {
  626. invalidUrlMessage = 'The URL contains a fragment identifier';
  627. }
  628. if (invalidUrlMessage) {
  629. const err = new SyntaxError(invalidUrlMessage);
  630. if (websocket._redirects === 0) {
  631. throw err;
  632. } else {
  633. emitErrorAndClose(websocket, err);
  634. return;
  635. }
  636. }
  637. const defaultPort = isSecure ? 443 : 80;
  638. const key = randomBytes(16).toString('base64');
  639. const request = isSecure ? https.request : http.request;
  640. const protocolSet = new Set();
  641. let perMessageDeflate;
  642. opts.createConnection = isSecure ? tlsConnect : netConnect;
  643. opts.defaultPort = opts.defaultPort || defaultPort;
  644. opts.port = parsedUrl.port || defaultPort;
  645. opts.host = parsedUrl.hostname.startsWith('[')
  646. ? parsedUrl.hostname.slice(1, -1)
  647. : parsedUrl.hostname;
  648. opts.headers = {
  649. ...opts.headers,
  650. 'Sec-WebSocket-Version': opts.protocolVersion,
  651. 'Sec-WebSocket-Key': key,
  652. Connection: 'Upgrade',
  653. Upgrade: 'websocket'
  654. };
  655. opts.path = parsedUrl.pathname + parsedUrl.search;
  656. opts.timeout = opts.handshakeTimeout;
  657. if (opts.perMessageDeflate) {
  658. perMessageDeflate = new PerMessageDeflate(
  659. opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
  660. false,
  661. opts.maxPayload
  662. );
  663. opts.headers['Sec-WebSocket-Extensions'] = format({
  664. [PerMessageDeflate.extensionName]: perMessageDeflate.offer()
  665. });
  666. }
  667. if (protocols.length) {
  668. for (const protocol of protocols) {
  669. if (
  670. typeof protocol !== 'string' ||
  671. !subprotocolRegex.test(protocol) ||
  672. protocolSet.has(protocol)
  673. ) {
  674. throw new SyntaxError(
  675. 'An invalid or duplicated subprotocol was specified'
  676. );
  677. }
  678. protocolSet.add(protocol);
  679. }
  680. opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
  681. }
  682. if (opts.origin) {
  683. if (opts.protocolVersion < 13) {
  684. opts.headers['Sec-WebSocket-Origin'] = opts.origin;
  685. } else {
  686. opts.headers.Origin = opts.origin;
  687. }
  688. }
  689. if (parsedUrl.username || parsedUrl.password) {
  690. opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
  691. }
  692. if (isIpcUrl) {
  693. const parts = opts.path.split(':');
  694. opts.socketPath = parts[0];
  695. opts.path = parts[1];
  696. }
  697. let req;
  698. if (opts.followRedirects) {
  699. if (websocket._redirects === 0) {
  700. websocket._originalIpc = isIpcUrl;
  701. websocket._originalSecure = isSecure;
  702. websocket._originalHostOrSocketPath = isIpcUrl
  703. ? opts.socketPath
  704. : parsedUrl.host;
  705. const headers = options && options.headers;
  706. //
  707. // Shallow copy the user provided options so that headers can be changed
  708. // without mutating the original object.
  709. //
  710. options = { ...options, headers: {} };
  711. if (headers) {
  712. for (const [key, value] of Object.entries(headers)) {
  713. options.headers[key.toLowerCase()] = value;
  714. }
  715. }
  716. } else if (websocket.listenerCount('redirect') === 0) {
  717. const isSameHost = isIpcUrl
  718. ? websocket._originalIpc
  719. ? opts.socketPath === websocket._originalHostOrSocketPath
  720. : false
  721. : websocket._originalIpc
  722. ? false
  723. : parsedUrl.host === websocket._originalHostOrSocketPath;
  724. if (!isSameHost || (websocket._originalSecure && !isSecure)) {
  725. //
  726. // Match curl 7.77.0 behavior and drop the following headers. These
  727. // headers are also dropped when following a redirect to a subdomain.
  728. //
  729. delete opts.headers.authorization;
  730. delete opts.headers.cookie;
  731. if (!isSameHost) delete opts.headers.host;
  732. opts.auth = undefined;
  733. }
  734. }
  735. //
  736. // Match curl 7.77.0 behavior and make the first `Authorization` header win.
  737. // If the `Authorization` header is set, then there is nothing to do as it
  738. // will take precedence.
  739. //
  740. if (opts.auth && !options.headers.authorization) {
  741. options.headers.authorization =
  742. 'Basic ' + Buffer.from(opts.auth).toString('base64');
  743. }
  744. req = websocket._req = request(opts);
  745. if (websocket._redirects) {
  746. //
  747. // Unlike what is done for the `'upgrade'` event, no early exit is
  748. // triggered here if the user calls `websocket.close()` or
  749. // `websocket.terminate()` from a listener of the `'redirect'` event. This
  750. // is because the user can also call `request.destroy()` with an error
  751. // before calling `websocket.close()` or `websocket.terminate()` and this
  752. // would result in an error being emitted on the `request` object with no
  753. // `'error'` event listeners attached.
  754. //
  755. websocket.emit('redirect', websocket.url, req);
  756. }
  757. } else {
  758. req = websocket._req = request(opts);
  759. }
  760. if (opts.timeout) {
  761. req.on('timeout', () => {
  762. abortHandshake(websocket, req, 'Opening handshake has timed out');
  763. });
  764. }
  765. req.on('error', (err) => {
  766. if (req === null || req[kAborted]) return;
  767. req = websocket._req = null;
  768. emitErrorAndClose(websocket, err);
  769. });
  770. req.on('response', (res) => {
  771. const location = res.headers.location;
  772. const statusCode = res.statusCode;
  773. if (
  774. location &&
  775. opts.followRedirects &&
  776. statusCode >= 300 &&
  777. statusCode < 400
  778. ) {
  779. if (++websocket._redirects > opts.maxRedirects) {
  780. abortHandshake(websocket, req, 'Maximum redirects exceeded');
  781. return;
  782. }
  783. req.abort();
  784. let addr;
  785. try {
  786. addr = new URL(location, address);
  787. } catch (e) {
  788. const err = new SyntaxError(`Invalid URL: ${location}`);
  789. emitErrorAndClose(websocket, err);
  790. return;
  791. }
  792. initAsClient(websocket, addr, protocols, options);
  793. } else if (!websocket.emit('unexpected-response', req, res)) {
  794. abortHandshake(
  795. websocket,
  796. req,
  797. `Unexpected server response: ${res.statusCode}`
  798. );
  799. }
  800. });
  801. req.on('upgrade', (res, socket, head) => {
  802. websocket.emit('upgrade', res);
  803. //
  804. // The user may have closed the connection from a listener of the
  805. // `'upgrade'` event.
  806. //
  807. if (websocket.readyState !== WebSocket.CONNECTING) return;
  808. req = websocket._req = null;
  809. if (res.headers.upgrade.toLowerCase() !== 'websocket') {
  810. abortHandshake(websocket, socket, 'Invalid Upgrade header');
  811. return;
  812. }
  813. const digest = createHash('sha1')
  814. .update(key + GUID)
  815. .digest('base64');
  816. if (res.headers['sec-websocket-accept'] !== digest) {
  817. abortHandshake(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  818. return;
  819. }
  820. const serverProt = res.headers['sec-websocket-protocol'];
  821. let protError;
  822. if (serverProt !== undefined) {
  823. if (!protocolSet.size) {
  824. protError = 'Server sent a subprotocol but none was requested';
  825. } else if (!protocolSet.has(serverProt)) {
  826. protError = 'Server sent an invalid subprotocol';
  827. }
  828. } else if (protocolSet.size) {
  829. protError = 'Server sent no subprotocol';
  830. }
  831. if (protError) {
  832. abortHandshake(websocket, socket, protError);
  833. return;
  834. }
  835. if (serverProt) websocket._protocol = serverProt;
  836. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  837. if (secWebSocketExtensions !== undefined) {
  838. if (!perMessageDeflate) {
  839. const message =
  840. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  841. 'was requested';
  842. abortHandshake(websocket, socket, message);
  843. return;
  844. }
  845. let extensions;
  846. try {
  847. extensions = parse(secWebSocketExtensions);
  848. } catch (err) {
  849. const message = 'Invalid Sec-WebSocket-Extensions header';
  850. abortHandshake(websocket, socket, message);
  851. return;
  852. }
  853. const extensionNames = Object.keys(extensions);
  854. if (
  855. extensionNames.length !== 1 ||
  856. extensionNames[0] !== PerMessageDeflate.extensionName
  857. ) {
  858. const message = 'Server indicated an extension that was not requested';
  859. abortHandshake(websocket, socket, message);
  860. return;
  861. }
  862. try {
  863. perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
  864. } catch (err) {
  865. const message = 'Invalid Sec-WebSocket-Extensions header';
  866. abortHandshake(websocket, socket, message);
  867. return;
  868. }
  869. websocket._extensions[PerMessageDeflate.extensionName] =
  870. perMessageDeflate;
  871. }
  872. websocket.setSocket(socket, head, {
  873. allowSynchronousEvents: opts.allowSynchronousEvents,
  874. generateMask: opts.generateMask,
  875. maxPayload: opts.maxPayload,
  876. skipUTF8Validation: opts.skipUTF8Validation
  877. });
  878. });
  879. if (opts.finishRequest) {
  880. opts.finishRequest(req, websocket);
  881. } else {
  882. req.end();
  883. }
  884. }
  885. /**
  886. * Emit the `'error'` and `'close'` events.
  887. *
  888. * @param {WebSocket} websocket The WebSocket instance
  889. * @param {Error} The error to emit
  890. * @private
  891. */
  892. function emitErrorAndClose(websocket, err) {
  893. websocket._readyState = WebSocket.CLOSING;
  894. websocket.emit('error', err);
  895. websocket.emitClose();
  896. }
  897. /**
  898. * Create a `net.Socket` and initiate a connection.
  899. *
  900. * @param {Object} options Connection options
  901. * @return {net.Socket} The newly created socket used to start the connection
  902. * @private
  903. */
  904. function netConnect(options) {
  905. options.path = options.socketPath;
  906. return net.connect(options);
  907. }
  908. /**
  909. * Create a `tls.TLSSocket` and initiate a connection.
  910. *
  911. * @param {Object} options Connection options
  912. * @return {tls.TLSSocket} The newly created socket used to start the connection
  913. * @private
  914. */
  915. function tlsConnect(options) {
  916. options.path = undefined;
  917. if (!options.servername && options.servername !== '') {
  918. options.servername = net.isIP(options.host) ? '' : options.host;
  919. }
  920. return tls.connect(options);
  921. }
  922. /**
  923. * Abort the handshake and emit an error.
  924. *
  925. * @param {WebSocket} websocket The WebSocket instance
  926. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  927. * abort or the socket to destroy
  928. * @param {String} message The error message
  929. * @private
  930. */
  931. function abortHandshake(websocket, stream, message) {
  932. websocket._readyState = WebSocket.CLOSING;
  933. const err = new Error(message);
  934. Error.captureStackTrace(err, abortHandshake);
  935. if (stream.setHeader) {
  936. stream[kAborted] = true;
  937. stream.abort();
  938. if (stream.socket && !stream.socket.destroyed) {
  939. //
  940. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  941. // called after the request completed. See
  942. // https://github.com/websockets/ws/issues/1869.
  943. //
  944. stream.socket.destroy();
  945. }
  946. process.nextTick(emitErrorAndClose, websocket, err);
  947. } else {
  948. stream.destroy(err);
  949. stream.once('error', websocket.emit.bind(websocket, 'error'));
  950. stream.once('close', websocket.emitClose.bind(websocket));
  951. }
  952. }
  953. /**
  954. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  955. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  956. *
  957. * @param {WebSocket} websocket The WebSocket instance
  958. * @param {*} [data] The data to send
  959. * @param {Function} [cb] Callback
  960. * @private
  961. */
  962. function sendAfterClose(websocket, data, cb) {
  963. if (data) {
  964. const length = toBuffer(data).length;
  965. //
  966. // The `_bufferedAmount` property is used only when the peer is a client and
  967. // the opening handshake fails. Under these circumstances, in fact, the
  968. // `setSocket()` method is not called, so the `_socket` and `_sender`
  969. // properties are set to `null`.
  970. //
  971. if (websocket._socket) websocket._sender._bufferedBytes += length;
  972. else websocket._bufferedAmount += length;
  973. }
  974. if (cb) {
  975. const err = new Error(
  976. `WebSocket is not open: readyState ${websocket.readyState} ` +
  977. `(${readyStates[websocket.readyState]})`
  978. );
  979. process.nextTick(cb, err);
  980. }
  981. }
  982. /**
  983. * The listener of the `Receiver` `'conclude'` event.
  984. *
  985. * @param {Number} code The status code
  986. * @param {Buffer} reason The reason for closing
  987. * @private
  988. */
  989. function receiverOnConclude(code, reason) {
  990. const websocket = this[kWebSocket];
  991. websocket._closeFrameReceived = true;
  992. websocket._closeMessage = reason;
  993. websocket._closeCode = code;
  994. if (websocket._socket[kWebSocket] === undefined) return;
  995. websocket._socket.removeListener('data', socketOnData);
  996. process.nextTick(resume, websocket._socket);
  997. if (code === 1005) websocket.close();
  998. else websocket.close(code, reason);
  999. }
  1000. /**
  1001. * The listener of the `Receiver` `'drain'` event.
  1002. *
  1003. * @private
  1004. */
  1005. function receiverOnDrain() {
  1006. const websocket = this[kWebSocket];
  1007. if (!websocket.isPaused) websocket._socket.resume();
  1008. }
  1009. /**
  1010. * The listener of the `Receiver` `'error'` event.
  1011. *
  1012. * @param {(RangeError|Error)} err The emitted error
  1013. * @private
  1014. */
  1015. function receiverOnError(err) {
  1016. const websocket = this[kWebSocket];
  1017. if (websocket._socket[kWebSocket] !== undefined) {
  1018. websocket._socket.removeListener('data', socketOnData);
  1019. //
  1020. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  1021. // https://github.com/websockets/ws/issues/1940.
  1022. //
  1023. process.nextTick(resume, websocket._socket);
  1024. websocket.close(err[kStatusCode]);
  1025. }
  1026. websocket.emit('error', err);
  1027. }
  1028. /**
  1029. * The listener of the `Receiver` `'finish'` event.
  1030. *
  1031. * @private
  1032. */
  1033. function receiverOnFinish() {
  1034. this[kWebSocket].emitClose();
  1035. }
  1036. /**
  1037. * The listener of the `Receiver` `'message'` event.
  1038. *
  1039. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  1040. * @param {Boolean} isBinary Specifies whether the message is binary or not
  1041. * @private
  1042. */
  1043. function receiverOnMessage(data, isBinary) {
  1044. this[kWebSocket].emit('message', data, isBinary);
  1045. }
  1046. /**
  1047. * The listener of the `Receiver` `'ping'` event.
  1048. *
  1049. * @param {Buffer} data The data included in the ping frame
  1050. * @private
  1051. */
  1052. function receiverOnPing(data) {
  1053. const websocket = this[kWebSocket];
  1054. if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);
  1055. websocket.emit('ping', data);
  1056. }
  1057. /**
  1058. * The listener of the `Receiver` `'pong'` event.
  1059. *
  1060. * @param {Buffer} data The data included in the pong frame
  1061. * @private
  1062. */
  1063. function receiverOnPong(data) {
  1064. this[kWebSocket].emit('pong', data);
  1065. }
  1066. /**
  1067. * Resume a readable stream
  1068. *
  1069. * @param {Readable} stream The readable stream
  1070. * @private
  1071. */
  1072. function resume(stream) {
  1073. stream.resume();
  1074. }
  1075. /**
  1076. * The listener of the socket `'close'` event.
  1077. *
  1078. * @private
  1079. */
  1080. function socketOnClose() {
  1081. const websocket = this[kWebSocket];
  1082. this.removeListener('close', socketOnClose);
  1083. this.removeListener('data', socketOnData);
  1084. this.removeListener('end', socketOnEnd);
  1085. websocket._readyState = WebSocket.CLOSING;
  1086. let chunk;
  1087. //
  1088. // The close frame might not have been received or the `'end'` event emitted,
  1089. // for example, if the socket was destroyed due to an error. Ensure that the
  1090. // `receiver` stream is closed after writing any remaining buffered data to
  1091. // it. If the readable side of the socket is in flowing mode then there is no
  1092. // buffered data as everything has been already written and `readable.read()`
  1093. // will return `null`. If instead, the socket is paused, any possible buffered
  1094. // data will be read as a single chunk.
  1095. //
  1096. if (
  1097. !this._readableState.endEmitted &&
  1098. !websocket._closeFrameReceived &&
  1099. !websocket._receiver._writableState.errorEmitted &&
  1100. (chunk = websocket._socket.read()) !== null
  1101. ) {
  1102. websocket._receiver.write(chunk);
  1103. }
  1104. websocket._receiver.end();
  1105. this[kWebSocket] = undefined;
  1106. clearTimeout(websocket._closeTimer);
  1107. if (
  1108. websocket._receiver._writableState.finished ||
  1109. websocket._receiver._writableState.errorEmitted
  1110. ) {
  1111. websocket.emitClose();
  1112. } else {
  1113. websocket._receiver.on('error', receiverOnFinish);
  1114. websocket._receiver.on('finish', receiverOnFinish);
  1115. }
  1116. }
  1117. /**
  1118. * The listener of the socket `'data'` event.
  1119. *
  1120. * @param {Buffer} chunk A chunk of data
  1121. * @private
  1122. */
  1123. function socketOnData(chunk) {
  1124. if (!this[kWebSocket]._receiver.write(chunk)) {
  1125. this.pause();
  1126. }
  1127. }
  1128. /**
  1129. * The listener of the socket `'end'` event.
  1130. *
  1131. * @private
  1132. */
  1133. function socketOnEnd() {
  1134. const websocket = this[kWebSocket];
  1135. websocket._readyState = WebSocket.CLOSING;
  1136. websocket._receiver.end();
  1137. this.end();
  1138. }
  1139. /**
  1140. * The listener of the socket `'error'` event.
  1141. *
  1142. * @private
  1143. */
  1144. function socketOnError() {
  1145. const websocket = this[kWebSocket];
  1146. this.removeListener('error', socketOnError);
  1147. this.on('error', NOOP);
  1148. if (websocket) {
  1149. websocket._readyState = WebSocket.CLOSING;
  1150. this.destroy();
  1151. }
  1152. }