websocket.js 36 KB

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