websocket.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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 {
  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=true] 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: true,
  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. socketPath: undefined,
  584. hostname: undefined,
  585. protocol: undefined,
  586. timeout: undefined,
  587. method: 'GET',
  588. host: undefined,
  589. path: undefined,
  590. port: undefined
  591. };
  592. websocket._autoPong = opts.autoPong;
  593. if (!protocolVersions.includes(opts.protocolVersion)) {
  594. throw new RangeError(
  595. `Unsupported protocol version: ${opts.protocolVersion} ` +
  596. `(supported versions: ${protocolVersions.join(', ')})`
  597. );
  598. }
  599. let parsedUrl;
  600. if (address instanceof URL) {
  601. parsedUrl = address;
  602. } else {
  603. try {
  604. parsedUrl = new URL(address);
  605. } catch (e) {
  606. throw new SyntaxError(`Invalid URL: ${address}`);
  607. }
  608. }
  609. if (parsedUrl.protocol === 'http:') {
  610. parsedUrl.protocol = 'ws:';
  611. } else if (parsedUrl.protocol === 'https:') {
  612. parsedUrl.protocol = 'wss:';
  613. }
  614. websocket._url = parsedUrl.href;
  615. const isSecure = parsedUrl.protocol === 'wss:';
  616. const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
  617. let invalidUrlMessage;
  618. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {
  619. invalidUrlMessage =
  620. 'The URL\'s protocol must be one of "ws:", "wss:", ' +
  621. '"http:", "https", or "ws+unix:"';
  622. } else if (isIpcUrl && !parsedUrl.pathname) {
  623. invalidUrlMessage = "The URL's pathname is empty";
  624. } else if (parsedUrl.hash) {
  625. invalidUrlMessage = 'The URL contains a fragment identifier';
  626. }
  627. if (invalidUrlMessage) {
  628. const err = new SyntaxError(invalidUrlMessage);
  629. if (websocket._redirects === 0) {
  630. throw err;
  631. } else {
  632. emitErrorAndClose(websocket, err);
  633. return;
  634. }
  635. }
  636. const defaultPort = isSecure ? 443 : 80;
  637. const key = randomBytes(16).toString('base64');
  638. const request = isSecure ? https.request : http.request;
  639. const protocolSet = new Set();
  640. let perMessageDeflate;
  641. opts.createConnection =
  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. const upgrade = res.headers.upgrade;
  810. if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {
  811. abortHandshake(websocket, socket, 'Invalid Upgrade header');
  812. return;
  813. }
  814. const digest = createHash('sha1')
  815. .update(key + GUID)
  816. .digest('base64');
  817. if (res.headers['sec-websocket-accept'] !== digest) {
  818. abortHandshake(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  819. return;
  820. }
  821. const serverProt = res.headers['sec-websocket-protocol'];
  822. let protError;
  823. if (serverProt !== undefined) {
  824. if (!protocolSet.size) {
  825. protError = 'Server sent a subprotocol but none was requested';
  826. } else if (!protocolSet.has(serverProt)) {
  827. protError = 'Server sent an invalid subprotocol';
  828. }
  829. } else if (protocolSet.size) {
  830. protError = 'Server sent no subprotocol';
  831. }
  832. if (protError) {
  833. abortHandshake(websocket, socket, protError);
  834. return;
  835. }
  836. if (serverProt) websocket._protocol = serverProt;
  837. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  838. if (secWebSocketExtensions !== undefined) {
  839. if (!perMessageDeflate) {
  840. const message =
  841. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  842. 'was requested';
  843. abortHandshake(websocket, socket, message);
  844. return;
  845. }
  846. let extensions;
  847. try {
  848. extensions = parse(secWebSocketExtensions);
  849. } catch (err) {
  850. const message = 'Invalid Sec-WebSocket-Extensions header';
  851. abortHandshake(websocket, socket, message);
  852. return;
  853. }
  854. const extensionNames = Object.keys(extensions);
  855. if (
  856. extensionNames.length !== 1 ||
  857. extensionNames[0] !== PerMessageDeflate.extensionName
  858. ) {
  859. const message = 'Server indicated an extension that was not requested';
  860. abortHandshake(websocket, socket, message);
  861. return;
  862. }
  863. try {
  864. perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
  865. } catch (err) {
  866. const message = 'Invalid Sec-WebSocket-Extensions header';
  867. abortHandshake(websocket, socket, message);
  868. return;
  869. }
  870. websocket._extensions[PerMessageDeflate.extensionName] =
  871. perMessageDeflate;
  872. }
  873. websocket.setSocket(socket, head, {
  874. allowSynchronousEvents: opts.allowSynchronousEvents,
  875. generateMask: opts.generateMask,
  876. maxPayload: opts.maxPayload,
  877. skipUTF8Validation: opts.skipUTF8Validation
  878. });
  879. });
  880. if (opts.finishRequest) {
  881. opts.finishRequest(req, websocket);
  882. } else {
  883. req.end();
  884. }
  885. }
  886. /**
  887. * Emit the `'error'` and `'close'` events.
  888. *
  889. * @param {WebSocket} websocket The WebSocket instance
  890. * @param {Error} The error to emit
  891. * @private
  892. */
  893. function emitErrorAndClose(websocket, err) {
  894. websocket._readyState = WebSocket.CLOSING;
  895. websocket.emit('error', err);
  896. websocket.emitClose();
  897. }
  898. /**
  899. * Create a `net.Socket` and initiate a connection.
  900. *
  901. * @param {Object} options Connection options
  902. * @return {net.Socket} The newly created socket used to start the connection
  903. * @private
  904. */
  905. function netConnect(options) {
  906. options.path = options.socketPath;
  907. return net.connect(options);
  908. }
  909. /**
  910. * Create a `tls.TLSSocket` and initiate a connection.
  911. *
  912. * @param {Object} options Connection options
  913. * @return {tls.TLSSocket} The newly created socket used to start the connection
  914. * @private
  915. */
  916. function tlsConnect(options) {
  917. options.path = undefined;
  918. if (!options.servername && options.servername !== '') {
  919. options.servername = net.isIP(options.host) ? '' : options.host;
  920. }
  921. return tls.connect(options);
  922. }
  923. /**
  924. * Abort the handshake and emit an error.
  925. *
  926. * @param {WebSocket} websocket The WebSocket instance
  927. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  928. * abort or the socket to destroy
  929. * @param {String} message The error message
  930. * @private
  931. */
  932. function abortHandshake(websocket, stream, message) {
  933. websocket._readyState = WebSocket.CLOSING;
  934. const err = new Error(message);
  935. Error.captureStackTrace(err, abortHandshake);
  936. if (stream.setHeader) {
  937. stream[kAborted] = true;
  938. stream.abort();
  939. if (stream.socket && !stream.socket.destroyed) {
  940. //
  941. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  942. // called after the request completed. See
  943. // https://github.com/websockets/ws/issues/1869.
  944. //
  945. stream.socket.destroy();
  946. }
  947. process.nextTick(emitErrorAndClose, websocket, err);
  948. } else {
  949. stream.destroy(err);
  950. stream.once('error', websocket.emit.bind(websocket, 'error'));
  951. stream.once('close', websocket.emitClose.bind(websocket));
  952. }
  953. }
  954. /**
  955. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  956. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  957. *
  958. * @param {WebSocket} websocket The WebSocket instance
  959. * @param {*} [data] The data to send
  960. * @param {Function} [cb] Callback
  961. * @private
  962. */
  963. function sendAfterClose(websocket, data, cb) {
  964. if (data) {
  965. const length = toBuffer(data).length;
  966. //
  967. // The `_bufferedAmount` property is used only when the peer is a client and
  968. // the opening handshake fails. Under these circumstances, in fact, the
  969. // `setSocket()` method is not called, so the `_socket` and `_sender`
  970. // properties are set to `null`.
  971. //
  972. if (websocket._socket) websocket._sender._bufferedBytes += length;
  973. else websocket._bufferedAmount += length;
  974. }
  975. if (cb) {
  976. const err = new Error(
  977. `WebSocket is not open: readyState ${websocket.readyState} ` +
  978. `(${readyStates[websocket.readyState]})`
  979. );
  980. process.nextTick(cb, err);
  981. }
  982. }
  983. /**
  984. * The listener of the `Receiver` `'conclude'` event.
  985. *
  986. * @param {Number} code The status code
  987. * @param {Buffer} reason The reason for closing
  988. * @private
  989. */
  990. function receiverOnConclude(code, reason) {
  991. const websocket = this[kWebSocket];
  992. websocket._closeFrameReceived = true;
  993. websocket._closeMessage = reason;
  994. websocket._closeCode = code;
  995. if (websocket._socket[kWebSocket] === undefined) return;
  996. websocket._socket.removeListener('data', socketOnData);
  997. process.nextTick(resume, websocket._socket);
  998. if (code === 1005) websocket.close();
  999. else websocket.close(code, reason);
  1000. }
  1001. /**
  1002. * The listener of the `Receiver` `'drain'` event.
  1003. *
  1004. * @private
  1005. */
  1006. function receiverOnDrain() {
  1007. const websocket = this[kWebSocket];
  1008. if (!websocket.isPaused) websocket._socket.resume();
  1009. }
  1010. /**
  1011. * The listener of the `Receiver` `'error'` event.
  1012. *
  1013. * @param {(RangeError|Error)} err The emitted error
  1014. * @private
  1015. */
  1016. function receiverOnError(err) {
  1017. const websocket = this[kWebSocket];
  1018. if (websocket._socket[kWebSocket] !== undefined) {
  1019. websocket._socket.removeListener('data', socketOnData);
  1020. //
  1021. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  1022. // https://github.com/websockets/ws/issues/1940.
  1023. //
  1024. process.nextTick(resume, websocket._socket);
  1025. websocket.close(err[kStatusCode]);
  1026. }
  1027. websocket.emit('error', err);
  1028. }
  1029. /**
  1030. * The listener of the `Receiver` `'finish'` event.
  1031. *
  1032. * @private
  1033. */
  1034. function receiverOnFinish() {
  1035. this[kWebSocket].emitClose();
  1036. }
  1037. /**
  1038. * The listener of the `Receiver` `'message'` event.
  1039. *
  1040. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  1041. * @param {Boolean} isBinary Specifies whether the message is binary or not
  1042. * @private
  1043. */
  1044. function receiverOnMessage(data, isBinary) {
  1045. this[kWebSocket].emit('message', data, isBinary);
  1046. }
  1047. /**
  1048. * The listener of the `Receiver` `'ping'` event.
  1049. *
  1050. * @param {Buffer} data The data included in the ping frame
  1051. * @private
  1052. */
  1053. function receiverOnPing(data) {
  1054. const websocket = this[kWebSocket];
  1055. if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP);
  1056. websocket.emit('ping', data);
  1057. }
  1058. /**
  1059. * The listener of the `Receiver` `'pong'` event.
  1060. *
  1061. * @param {Buffer} data The data included in the pong frame
  1062. * @private
  1063. */
  1064. function receiverOnPong(data) {
  1065. this[kWebSocket].emit('pong', data);
  1066. }
  1067. /**
  1068. * Resume a readable stream
  1069. *
  1070. * @param {Readable} stream The readable stream
  1071. * @private
  1072. */
  1073. function resume(stream) {
  1074. stream.resume();
  1075. }
  1076. /**
  1077. * The listener of the socket `'close'` event.
  1078. *
  1079. * @private
  1080. */
  1081. function socketOnClose() {
  1082. const websocket = this[kWebSocket];
  1083. this.removeListener('close', socketOnClose);
  1084. this.removeListener('data', socketOnData);
  1085. this.removeListener('end', socketOnEnd);
  1086. websocket._readyState = WebSocket.CLOSING;
  1087. let chunk;
  1088. //
  1089. // The close frame might not have been received or the `'end'` event emitted,
  1090. // for example, if the socket was destroyed due to an error. Ensure that the
  1091. // `receiver` stream is closed after writing any remaining buffered data to
  1092. // it. If the readable side of the socket is in flowing mode then there is no
  1093. // buffered data as everything has been already written and `readable.read()`
  1094. // will return `null`. If instead, the socket is paused, any possible buffered
  1095. // data will be read as a single chunk.
  1096. //
  1097. if (
  1098. !this._readableState.endEmitted &&
  1099. !websocket._closeFrameReceived &&
  1100. !websocket._receiver._writableState.errorEmitted &&
  1101. (chunk = websocket._socket.read()) !== null
  1102. ) {
  1103. websocket._receiver.write(chunk);
  1104. }
  1105. websocket._receiver.end();
  1106. this[kWebSocket] = undefined;
  1107. clearTimeout(websocket._closeTimer);
  1108. if (
  1109. websocket._receiver._writableState.finished ||
  1110. websocket._receiver._writableState.errorEmitted
  1111. ) {
  1112. websocket.emitClose();
  1113. } else {
  1114. websocket._receiver.on('error', receiverOnFinish);
  1115. websocket._receiver.on('finish', receiverOnFinish);
  1116. }
  1117. }
  1118. /**
  1119. * The listener of the socket `'data'` event.
  1120. *
  1121. * @param {Buffer} chunk A chunk of data
  1122. * @private
  1123. */
  1124. function socketOnData(chunk) {
  1125. if (!this[kWebSocket]._receiver.write(chunk)) {
  1126. this.pause();
  1127. }
  1128. }
  1129. /**
  1130. * The listener of the socket `'end'` event.
  1131. *
  1132. * @private
  1133. */
  1134. function socketOnEnd() {
  1135. const websocket = this[kWebSocket];
  1136. websocket._readyState = WebSocket.CLOSING;
  1137. websocket._receiver.end();
  1138. this.end();
  1139. }
  1140. /**
  1141. * The listener of the socket `'error'` event.
  1142. *
  1143. * @private
  1144. */
  1145. function socketOnError() {
  1146. const websocket = this[kWebSocket];
  1147. this.removeListener('error', socketOnError);
  1148. this.on('error', NOOP);
  1149. if (websocket) {
  1150. websocket._readyState = WebSocket.CLOSING;
  1151. this.destroy();
  1152. }
  1153. }