Ignore:
Timestamp:
11/25/21 22:08:24 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
8d391a1
Parents:
59329aa
Message:

primeNG components

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner-front/node_modules/ws/lib/websocket.js

    r59329aa re29cc2e  
    22
    33const EventEmitter = require('events');
     4const crypto = require('crypto');
    45const https = require('https');
    56const http = require('http');
    67const net = require('net');
    78const tls = require('tls');
    8 const { randomBytes, createHash } = require('crypto');
    9 const { URL } = require('url');
     9const url = require('url');
    1010
    1111const PerMessageDeflate = require('./permessage-deflate');
     12const EventTarget = require('./event-target');
     13const extension = require('./extension');
    1214const Receiver = require('./receiver');
    1315const Sender = require('./sender');
     
    2022  NOOP
    2123} = require('./constants');
    22 const { addEventListener, removeEventListener } = require('./event-target');
    23 const { format, parse } = require('./extension');
    24 const { toBuffer } = require('./buffer-util');
    2524
    2625const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
     
    3736   * Create a new `WebSocket`.
    3837   *
    39    * @param {(String|url.URL)} address The URL to which to connect
    40    * @param {(String|String[])} [protocols] The subprotocols
    41    * @param {Object} [options] Connection options
     38   * @param {(String|url.Url|url.URL)} address The URL to which to connect
     39   * @param {(String|String[])} protocols The subprotocols
     40   * @param {Object} options Connection options
    4241   */
    4342  constructor(address, protocols, options) {
    4443    super();
    4544
     45    this.readyState = WebSocket.CONNECTING;
     46    this.protocol = '';
     47
    4648    this._binaryType = BINARY_TYPES[0];
    47     this._closeCode = 1006;
    4849    this._closeFrameReceived = false;
    4950    this._closeFrameSent = false;
    5051    this._closeMessage = '';
    5152    this._closeTimer = null;
     53    this._closeCode = 1006;
    5254    this._extensions = {};
    53     this._protocol = '';
    54     this._readyState = WebSocket.CONNECTING;
    5555    this._receiver = null;
    5656    this._sender = null;
     
    5858
    5959    if (address !== null) {
    60       this._bufferedAmount = 0;
    6160      this._isServer = false;
    6261      this._redirects = 0;
     
    7574  }
    7675
     76  get CONNECTING() {
     77    return WebSocket.CONNECTING;
     78  }
     79  get CLOSING() {
     80    return WebSocket.CLOSING;
     81  }
     82  get CLOSED() {
     83    return WebSocket.CLOSED;
     84  }
     85  get OPEN() {
     86    return WebSocket.OPEN;
     87  }
     88
    7789  /**
    7890   * This deviates from the WHATWG interface since ws doesn't support the
     
    101113   */
    102114  get bufferedAmount() {
    103     if (!this._socket) return this._bufferedAmount;
    104 
    105     return this._socket._writableState.length + this._sender._bufferedBytes;
     115    if (!this._socket) return 0;
     116
     117    //
     118    // `socket.bufferSize` is `undefined` if the socket is closed.
     119    //
     120    return (this._socket.bufferSize || 0) + this._sender._bufferedBytes;
    106121  }
    107122
     
    114129
    115130  /**
    116    * @type {String}
    117    */
    118   get protocol() {
    119     return this._protocol;
    120   }
    121 
    122   /**
    123    * @type {Number}
    124    */
    125   get readyState() {
    126     return this._readyState;
    127   }
    128 
    129   /**
    130    * @type {String}
    131    */
    132   get url() {
    133     return this._url;
    134   }
    135 
    136   /**
    137131   * Set up the socket and the internal resources.
    138132   *
    139133   * @param {net.Socket} socket The network socket between the server and client
    140134   * @param {Buffer} head The first packet of the upgraded stream
    141    * @param {Number} [maxPayload=0] The maximum allowed message size
     135   * @param {Number} maxPayload The maximum allowed message size
    142136   * @private
    143137   */
    144138  setSocket(socket, head, maxPayload) {
    145139    const receiver = new Receiver(
    146       this.binaryType,
     140      this._binaryType,
    147141      this._extensions,
    148       this._isServer,
    149142      maxPayload
    150143    );
     
    174167    socket.on('error', socketOnError);
    175168
    176     this._readyState = WebSocket.OPEN;
     169    this.readyState = WebSocket.OPEN;
    177170    this.emit('open');
    178171  }
     
    184177   */
    185178  emitClose() {
     179    this.readyState = WebSocket.CLOSED;
     180
    186181    if (!this._socket) {
    187       this._readyState = WebSocket.CLOSED;
    188182      this.emit('close', this._closeCode, this._closeMessage);
    189183      return;
     
    195189
    196190    this._receiver.removeAllListeners();
    197     this._readyState = WebSocket.CLOSED;
    198191    this.emit('close', this._closeCode, this._closeMessage);
    199192  }
     
    214207   *              +---+
    215208   *
    216    * @param {Number} [code] Status code explaining why the connection is closing
    217    * @param {String} [data] A string explaining why the connection is closing
     209   * @param {Number} code Status code explaining why the connection is closing
     210   * @param {String} data A string explaining why the connection is closing
    218211   * @public
    219212   */
     
    230223    }
    231224
    232     this._readyState = WebSocket.CLOSING;
     225    this.readyState = WebSocket.CLOSING;
    233226    this._sender.close(code, data, !this._isServer, (err) => {
    234227      //
     
    254247   * Send a ping.
    255248   *
    256    * @param {*} [data] The data to send
    257    * @param {Boolean} [mask] Indicates whether or not to mask `data`
    258    * @param {Function} [cb] Callback which is executed when the ping is sent
     249   * @param {*} data The data to send
     250   * @param {Boolean} mask Indicates whether or not to mask `data`
     251   * @param {Function} cb Callback which is executed when the ping is sent
    259252   * @public
    260253   */
    261254  ping(data, mask, cb) {
    262     if (this.readyState === WebSocket.CONNECTING) {
    263       throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
    264     }
    265 
    266255    if (typeof data === 'function') {
    267256      cb = data;
     
    272261    }
    273262
     263    if (this.readyState !== WebSocket.OPEN) {
     264      const err = new Error(
     265        `WebSocket is not open: readyState ${this.readyState} ` +
     266          `(${readyStates[this.readyState]})`
     267      );
     268
     269      if (cb) return cb(err);
     270      throw err;
     271    }
     272
    274273    if (typeof data === 'number') data = data.toString();
    275 
    276     if (this.readyState !== WebSocket.OPEN) {
    277       sendAfterClose(this, data, cb);
    278       return;
    279     }
    280 
    281274    if (mask === undefined) mask = !this._isServer;
    282275    this._sender.ping(data || EMPTY_BUFFER, mask, cb);
     
    286279   * Send a pong.
    287280   *
    288    * @param {*} [data] The data to send
    289    * @param {Boolean} [mask] Indicates whether or not to mask `data`
    290    * @param {Function} [cb] Callback which is executed when the pong is sent
     281   * @param {*} data The data to send
     282   * @param {Boolean} mask Indicates whether or not to mask `data`
     283   * @param {Function} cb Callback which is executed when the pong is sent
    291284   * @public
    292285   */
    293286  pong(data, mask, cb) {
    294     if (this.readyState === WebSocket.CONNECTING) {
    295       throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
    296     }
    297 
    298287    if (typeof data === 'function') {
    299288      cb = data;
     
    304293    }
    305294
     295    if (this.readyState !== WebSocket.OPEN) {
     296      const err = new Error(
     297        `WebSocket is not open: readyState ${this.readyState} ` +
     298          `(${readyStates[this.readyState]})`
     299      );
     300
     301      if (cb) return cb(err);
     302      throw err;
     303    }
     304
    306305    if (typeof data === 'number') data = data.toString();
    307 
    308     if (this.readyState !== WebSocket.OPEN) {
    309       sendAfterClose(this, data, cb);
    310       return;
    311     }
    312 
    313306    if (mask === undefined) mask = !this._isServer;
    314307    this._sender.pong(data || EMPTY_BUFFER, mask, cb);
     
    319312   *
    320313   * @param {*} data The message to send
    321    * @param {Object} [options] Options object
    322    * @param {Boolean} [options.compress] Specifies whether or not to compress
    323    *     `data`
    324    * @param {Boolean} [options.binary] Specifies whether `data` is binary or
    325    *     text
    326    * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
    327    *     last one
    328    * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
    329    * @param {Function} [cb] Callback which is executed when data is written out
     314   * @param {Object} options Options object
     315   * @param {Boolean} options.compress Specifies whether or not to compress `data`
     316   * @param {Boolean} options.binary Specifies whether `data` is binary or text
     317   * @param {Boolean} options.fin Specifies whether the fragment is the last one
     318   * @param {Boolean} options.mask Specifies whether or not to mask `data`
     319   * @param {Function} cb Callback which is executed when data is written out
    330320   * @public
    331321   */
    332322  send(data, options, cb) {
    333     if (this.readyState === WebSocket.CONNECTING) {
    334       throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
    335     }
    336 
    337323    if (typeof options === 'function') {
    338324      cb = options;
     
    340326    }
    341327
     328    if (this.readyState !== WebSocket.OPEN) {
     329      const err = new Error(
     330        `WebSocket is not open: readyState ${this.readyState} ` +
     331          `(${readyStates[this.readyState]})`
     332      );
     333
     334      if (cb) return cb(err);
     335      throw err;
     336    }
     337
    342338    if (typeof data === 'number') data = data.toString();
    343339
    344     if (this.readyState !== WebSocket.OPEN) {
    345       sendAfterClose(this, data, cb);
    346       return;
    347     }
    348 
    349     const opts = {
    350       binary: typeof data !== 'string',
    351       mask: !this._isServer,
    352       compress: true,
    353       fin: true,
    354       ...options
    355     };
     340    const opts = Object.assign(
     341      {
     342        binary: typeof data !== 'string',
     343        mask: !this._isServer,
     344        compress: true,
     345        fin: true
     346      },
     347      options
     348    );
    356349
    357350    if (!this._extensions[PerMessageDeflate.extensionName]) {
     
    375368
    376369    if (this._socket) {
    377       this._readyState = WebSocket.CLOSING;
     370      this.readyState = WebSocket.CLOSING;
    378371      this._socket.destroy();
    379372    }
     
    382375
    383376readyStates.forEach((readyState, i) => {
    384   const descriptor = { enumerable: true, value: i };
    385 
    386   Object.defineProperty(WebSocket.prototype, readyState, descriptor);
    387   Object.defineProperty(WebSocket, readyState, descriptor);
    388 });
    389 
    390 [
    391   'binaryType',
    392   'bufferedAmount',
    393   'extensions',
    394   'protocol',
    395   'readyState',
    396   'url'
    397 ].forEach((property) => {
    398   Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
     377  WebSocket[readyState] = i;
    399378});
    400379
     
    405384['open', 'error', 'close', 'message'].forEach((method) => {
    406385  Object.defineProperty(WebSocket.prototype, `on${method}`, {
    407     configurable: true,
    408     enumerable: true,
    409386    /**
    410387     * Return the listener of the event.
     
    415392    get() {
    416393      const listeners = this.listeners(method);
    417       for (let i = 0; i < listeners.length; i++) {
     394      for (var i = 0; i < listeners.length; i++) {
    418395        if (listeners[i]._listener) return listeners[i]._listener;
    419396      }
     
    429406    set(listener) {
    430407      const listeners = this.listeners(method);
    431       for (let i = 0; i < listeners.length; i++) {
     408      for (var i = 0; i < listeners.length; i++) {
    432409        //
    433410        // Remove only the listeners added via `addEventListener`.
     
    440417});
    441418
    442 WebSocket.prototype.addEventListener = addEventListener;
    443 WebSocket.prototype.removeEventListener = removeEventListener;
     419WebSocket.prototype.addEventListener = EventTarget.addEventListener;
     420WebSocket.prototype.removeEventListener = EventTarget.removeEventListener;
    444421
    445422module.exports = WebSocket;
     
    449426 *
    450427 * @param {WebSocket} websocket The client to initialize
    451  * @param {(String|url.URL)} address The URL to which to connect
    452  * @param {String} [protocols] The subprotocols
    453  * @param {Object} [options] Connection options
    454  * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
     428 * @param {(String|url.Url|url.URL)} address The URL to which to connect
     429 * @param {String} protocols The subprotocols
     430 * @param {Object} options Connection options
     431 * @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
    455432 *     permessage-deflate
    456  * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
     433 * @param {Number} options.handshakeTimeout Timeout in milliseconds for the
    457434 *     handshake request
    458  * @param {Number} [options.protocolVersion=13] Value of the
    459  *     `Sec-WebSocket-Version` header
    460  * @param {String} [options.origin] Value of the `Origin` or
     435 * @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version`
     436 *     header
     437 * @param {String} options.origin Value of the `Origin` or
    461438 *     `Sec-WebSocket-Origin` header
    462  * @param {Number} [options.maxPayload=104857600] The maximum allowed message
    463  *     size
    464  * @param {Boolean} [options.followRedirects=false] Whether or not to follow
    465  *     redirects
    466  * @param {Number} [options.maxRedirects=10] The maximum number of redirects
    467  *     allowed
     439 * @param {Number} options.maxPayload The maximum allowed message size
     440 * @param {Boolean} options.followRedirects Whether or not to follow redirects
     441 * @param {Number} options.maxRedirects The maximum number of redirects allowed
    468442 * @private
    469443 */
    470444function initAsClient(websocket, address, protocols, options) {
    471   const opts = {
    472     protocolVersion: protocolVersions[1],
    473     maxPayload: 100 * 1024 * 1024,
    474     perMessageDeflate: true,
    475     followRedirects: false,
    476     maxRedirects: 10,
    477     ...options,
    478     createConnection: undefined,
    479     socketPath: undefined,
    480     hostname: undefined,
    481     protocol: undefined,
    482     timeout: undefined,
    483     method: undefined,
    484     host: undefined,
    485     path: undefined,
    486     port: undefined
    487   };
     445  const opts = Object.assign(
     446    {
     447      protocolVersion: protocolVersions[1],
     448      maxPayload: 100 * 1024 * 1024,
     449      perMessageDeflate: true,
     450      followRedirects: false,
     451      maxRedirects: 10
     452    },
     453    options,
     454    {
     455      createConnection: undefined,
     456      socketPath: undefined,
     457      hostname: undefined,
     458      protocol: undefined,
     459      timeout: undefined,
     460      method: undefined,
     461      auth: undefined,
     462      host: undefined,
     463      path: undefined,
     464      port: undefined
     465    }
     466  );
    488467
    489468  if (!protocolVersions.includes(opts.protocolVersion)) {
     
    494473  }
    495474
    496   let parsedUrl;
    497 
    498   if (address instanceof URL) {
     475  var parsedUrl;
     476
     477  if (typeof address === 'object' && address.href !== undefined) {
    499478    parsedUrl = address;
    500     websocket._url = address.href;
     479    websocket.url = address.href;
    501480  } else {
    502     parsedUrl = new URL(address);
    503     websocket._url = address;
     481    //
     482    // The WHATWG URL constructor is not available on Node.js < 6.13.0
     483    //
     484    parsedUrl = url.URL ? new url.URL(address) : url.parse(address);
     485    websocket.url = address;
    504486  }
    505487
     
    513495    parsedUrl.protocol === 'wss:' || parsedUrl.protocol === 'https:';
    514496  const defaultPort = isSecure ? 443 : 80;
    515   const key = randomBytes(16).toString('base64');
     497  const key = crypto.randomBytes(16).toString('base64');
    516498  const get = isSecure ? https.get : http.get;
    517   let perMessageDeflate;
     499  const path = parsedUrl.search
     500    ? `${parsedUrl.pathname || '/'}${parsedUrl.search}`
     501    : parsedUrl.pathname || '/';
     502  var perMessageDeflate;
    518503
    519504  opts.createConnection = isSecure ? tlsConnect : netConnect;
     
    523508    ? parsedUrl.hostname.slice(1, -1)
    524509    : parsedUrl.hostname;
    525   opts.headers = {
    526     'Sec-WebSocket-Version': opts.protocolVersion,
    527     'Sec-WebSocket-Key': key,
    528     Connection: 'Upgrade',
    529     Upgrade: 'websocket',
    530     ...opts.headers
    531   };
    532   opts.path = parsedUrl.pathname + parsedUrl.search;
     510  opts.headers = Object.assign(
     511    {
     512      'Sec-WebSocket-Version': opts.protocolVersion,
     513      'Sec-WebSocket-Key': key,
     514      Connection: 'Upgrade',
     515      Upgrade: 'websocket'
     516    },
     517    opts.headers
     518  );
     519  opts.path = path;
    533520  opts.timeout = opts.handshakeTimeout;
    534521
     
    539526      opts.maxPayload
    540527    );
    541     opts.headers['Sec-WebSocket-Extensions'] = format({
     528    opts.headers['Sec-WebSocket-Extensions'] = extension.format({
    542529      [PerMessageDeflate.extensionName]: perMessageDeflate.offer()
    543530    });
     
    553540    }
    554541  }
    555   if (parsedUrl.username || parsedUrl.password) {
     542  if (parsedUrl.auth) {
     543    opts.auth = parsedUrl.auth;
     544  } else if (parsedUrl.username || parsedUrl.password) {
    556545    opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
    557546  }
    558547
    559548  if (isUnixSocket) {
    560     const parts = opts.path.split(':');
     549    const parts = path.split(':');
    561550
    562551    opts.socketPath = parts[0];
     
    564553  }
    565554
    566   let req = (websocket._req = get(opts));
     555  var req = (websocket._req = get(opts));
    567556
    568557  if (opts.timeout) {
     
    573562
    574563  req.on('error', (err) => {
    575     if (req === null || req.aborted) return;
     564    if (websocket._req.aborted) return;
    576565
    577566    req = websocket._req = null;
    578     websocket._readyState = WebSocket.CLOSING;
     567    websocket.readyState = WebSocket.CLOSING;
    579568    websocket.emit('error', err);
    580569    websocket.emitClose();
     
    598587      req.abort();
    599588
    600       const addr = new URL(location, address);
     589      const addr = url.URL
     590        ? new url.URL(location, address)
     591        : url.resolve(address, location);
    601592
    602593      initAsClient(websocket, addr, protocols, options);
     
    621612    req = websocket._req = null;
    622613
    623     const digest = createHash('sha1')
     614    const digest = crypto
     615      .createHash('sha1')
    624616      .update(key + GUID)
    625617      .digest('base64');
     
    632624    const serverProt = res.headers['sec-websocket-protocol'];
    633625    const protList = (protocols || '').split(/, */);
    634     let protError;
     626    var protError;
    635627
    636628    if (!protocols && serverProt) {
     
    647639    }
    648640
    649     if (serverProt) websocket._protocol = serverProt;
     641    if (serverProt) websocket.protocol = serverProt;
    650642
    651643    if (perMessageDeflate) {
    652644      try {
    653         const extensions = parse(res.headers['sec-websocket-extensions']);
     645        const extensions = extension.parse(
     646          res.headers['sec-websocket-extensions']
     647        );
    654648
    655649        if (extensions[PerMessageDeflate.extensionName]) {
    656650          perMessageDeflate.accept(extensions[PerMessageDeflate.extensionName]);
    657           websocket._extensions[PerMessageDeflate.extensionName] =
    658             perMessageDeflate;
     651          websocket._extensions[
     652            PerMessageDeflate.extensionName
     653          ] = perMessageDeflate;
    659654        }
    660655      } catch (err) {
     
    680675 */
    681676function netConnect(options) {
    682   options.path = options.socketPath;
     677  //
     678  // Override `options.path` only if `options` is a copy of the original options
     679  // object. This is always true on Node.js >= 8 but not on Node.js 6 where
     680  // `options.socketPath` might be `undefined` even if the `socketPath` option
     681  // was originally set.
     682  //
     683  if (options.protocolVersion) options.path = options.socketPath;
    683684  return net.connect(options);
    684685}
     
    693694function tlsConnect(options) {
    694695  options.path = undefined;
    695 
    696   if (!options.servername && options.servername !== '') {
    697     options.servername = net.isIP(options.host) ? '' : options.host;
    698   }
    699 
     696  options.servername = options.servername || options.host;
    700697  return tls.connect(options);
    701698}
     
    711708 */
    712709function abortHandshake(websocket, stream, message) {
    713   websocket._readyState = WebSocket.CLOSING;
     710  websocket.readyState = WebSocket.CLOSING;
    714711
    715712  const err = new Error(message);
     
    718715  if (stream.setHeader) {
    719716    stream.abort();
    720 
    721     if (stream.socket && !stream.socket.destroyed) {
    722       //
    723       // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
    724       // called after the request completed. See
    725       // https://github.com/websockets/ws/issues/1869.
    726       //
    727       stream.socket.destroy();
    728     }
    729 
    730717    stream.once('abort', websocket.emitClose.bind(websocket));
    731718    websocket.emit('error', err);
     
    738725
    739726/**
    740  * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
    741  * when the `readyState` attribute is `CLOSING` or `CLOSED`.
    742  *
    743  * @param {WebSocket} websocket The WebSocket instance
    744  * @param {*} [data] The data to send
    745  * @param {Function} [cb] Callback
    746  * @private
    747  */
    748 function sendAfterClose(websocket, data, cb) {
    749   if (data) {
    750     const length = toBuffer(data).length;
    751 
    752     //
    753     // The `_bufferedAmount` property is used only when the peer is a client and
    754     // the opening handshake fails. Under these circumstances, in fact, the
    755     // `setSocket()` method is not called, so the `_socket` and `_sender`
    756     // properties are set to `null`.
    757     //
    758     if (websocket._socket) websocket._sender._bufferedBytes += length;
    759     else websocket._bufferedAmount += length;
    760   }
    761 
    762   if (cb) {
    763     const err = new Error(
    764       `WebSocket is not open: readyState ${websocket.readyState} ` +
    765         `(${readyStates[websocket.readyState]})`
    766     );
    767     cb(err);
    768   }
    769 }
    770 
    771 /**
    772727 * The listener of the `Receiver` `'conclude'` event.
    773728 *
     
    810765  websocket._socket.removeListener('data', socketOnData);
    811766
    812   websocket._readyState = WebSocket.CLOSING;
     767  websocket.readyState = WebSocket.CLOSING;
    813768  websocket._closeCode = err[kStatusCode];
    814769  websocket.emit('error', err);
     
    869824  this.removeListener('end', socketOnEnd);
    870825
    871   websocket._readyState = WebSocket.CLOSING;
     826  websocket.readyState = WebSocket.CLOSING;
    872827
    873828  //
     
    920875  const websocket = this[kWebSocket];
    921876
    922   websocket._readyState = WebSocket.CLOSING;
     877  websocket.readyState = WebSocket.CLOSING;
    923878  websocket._receiver.end();
    924879  this.end();
     
    936891  this.on('error', NOOP);
    937892
    938   if (websocket) {
    939     websocket._readyState = WebSocket.CLOSING;
    940     this.destroy();
    941   }
    942 }
     893  websocket.readyState = WebSocket.CLOSING;
     894  this.destroy();
     895}
Note: See TracChangeset for help on using the changeset viewer.