| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 | "use strict";Object.defineProperty(exports, "__esModule", {  value: true});exports.default = void 0;var _CoreManager = _interopRequireDefault(require("./CoreManager"));var _EventEmitter = _interopRequireDefault(require("./EventEmitter"));var _ParseObject = _interopRequireDefault(require("./ParseObject"));var _LiveQuerySubscription = _interopRequireDefault(require("./LiveQuerySubscription"));var _promiseUtils = require("./promiseUtils");var _ParseError = _interopRequireDefault(require("./ParseError"));function _interopRequireDefault(obj) {  return obj && obj.__esModule ? obj : {    default: obj  };}/* global WebSocket */// The LiveQuery client inner stateconst CLIENT_STATE = {  INITIALIZED: 'initialized',  CONNECTING: 'connecting',  CONNECTED: 'connected',  CLOSED: 'closed',  RECONNECTING: 'reconnecting',  DISCONNECTED: 'disconnected'};// The event type the LiveQuery client should sent to serverconst OP_TYPES = {  CONNECT: 'connect',  SUBSCRIBE: 'subscribe',  UNSUBSCRIBE: 'unsubscribe',  ERROR: 'error'};// The event we get back from LiveQuery serverconst OP_EVENTS = {  CONNECTED: 'connected',  SUBSCRIBED: 'subscribed',  UNSUBSCRIBED: 'unsubscribed',  ERROR: 'error',  CREATE: 'create',  UPDATE: 'update',  ENTER: 'enter',  LEAVE: 'leave',  DELETE: 'delete'};// The event the LiveQuery client should emitconst CLIENT_EMMITER_TYPES = {  CLOSE: 'close',  ERROR: 'error',  OPEN: 'open'};// The event the LiveQuery subscription should emitconst SUBSCRIPTION_EMMITER_TYPES = {  OPEN: 'open',  CLOSE: 'close',  ERROR: 'error',  CREATE: 'create',  UPDATE: 'update',  ENTER: 'enter',  LEAVE: 'leave',  DELETE: 'delete'};const generateInterval = k => {  return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;};/** * Creates a new LiveQueryClient. * Extends events.EventEmitter * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>. * * A wrapper of a standard WebSocket client. We add several useful methods to * help you connect/disconnect to LiveQueryServer, subscribe/unsubscribe a ParseQuery easily. * * javascriptKey and masterKey are used for verifying the LiveQueryClient when it tries * to connect to the LiveQuery server * * We expose three events to help you monitor the status of the LiveQueryClient. * * <pre> * let Parse = require('parse/node'); * let LiveQueryClient = Parse.LiveQueryClient; * let client = new LiveQueryClient({ *   applicationId: '', *   serverURL: '', *   javascriptKey: '', *   masterKey: '' *  }); * </pre> * * Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event. * <pre> * client.on('open', () => { * * });</pre> * * Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event. * <pre> * client.on('close', () => { * * });</pre> * * Error - When some network error or LiveQuery server error happens, you'll get this event. * <pre> * client.on('error', (error) => { * * });</pre> * * @alias Parse.LiveQueryClient */class LiveQueryClient extends _EventEmitter.default {  /*:: attempts: number;*/  /*:: id: number;*/  /*:: requestId: number;*/  /*:: applicationId: string;*/  /*:: serverURL: string;*/  /*:: javascriptKey: ?string;*/  /*:: masterKey: ?string;*/  /*:: sessionToken: ?string;*/  /*:: installationId: ?string;*/  /*:: additionalProperties: boolean;*/  /*:: connectPromise: Promise;*/  /*:: subscriptions: Map;*/  /*:: socket: any;*/  /*:: state: string;*/  /**   * @param {object} options   * @param {string} options.applicationId - applicationId of your Parse app   * @param {string} options.serverURL - <b>the URL of your LiveQuery server</b>   * @param {string} options.javascriptKey (optional)   * @param {string} options.masterKey (optional) Your Parse Master Key. (Node.js only!)   * @param {string} options.sessionToken (optional)   * @param {string} options.installationId (optional)   */  constructor({    applicationId,    serverURL,    javascriptKey,    masterKey,    sessionToken,    installationId  }) {    super();    if (!serverURL || serverURL.indexOf('ws') !== 0) {      throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');    }    this.reconnectHandle = null;    this.attempts = 1;    this.id = 0;    this.requestId = 1;    this.serverURL = serverURL;    this.applicationId = applicationId;    this.javascriptKey = javascriptKey || undefined;    this.masterKey = masterKey || undefined;    this.sessionToken = sessionToken || undefined;    this.installationId = installationId || undefined;    this.additionalProperties = true;    this.connectPromise = (0, _promiseUtils.resolvingPromise)();    this.subscriptions = new Map();    this.state = CLIENT_STATE.INITIALIZED;    // adding listener so process does not crash    // best practice is for developer to register their own listener    this.on('error', () => {});  }  shouldOpen() /*: any*/{    return this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED;  }  /**   * Subscribes to a ParseQuery   *   * If you provide the sessionToken, when the LiveQuery server gets ParseObject's   * updates from parse server, it'll try to check whether the sessionToken fulfills   * the ParseObject's ACL. The LiveQuery server will only send updates to clients whose   * sessionToken is fit for the ParseObject's ACL. You can check the LiveQuery protocol   * <a href="https://github.com/parse-community/parse-server/wiki/Parse-LiveQuery-Protocol-Specification">here</a> for more details. The subscription you get is the same subscription you get   * from our Standard API.   *   * @param {object} query - the ParseQuery you want to subscribe to   * @param {string} sessionToken (optional)   * @returns {LiveQuerySubscription | undefined}   */  subscribe(query /*: Object*/, sessionToken /*: ?string*/) /*: LiveQuerySubscription*/{    var _queryJSON$keys, _queryJSON$watch;    if (!query) {      return;    }    const className = query.className;    const queryJSON = query.toJSON();    const where = queryJSON.where;    const fields = (_queryJSON$keys = queryJSON.keys) === null || _queryJSON$keys === void 0 ? void 0 : _queryJSON$keys.split(',');    const watch = (_queryJSON$watch = queryJSON.watch) === null || _queryJSON$watch === void 0 ? void 0 : _queryJSON$watch.split(',');    const subscribeRequest = {      op: OP_TYPES.SUBSCRIBE,      requestId: this.requestId,      query: {        className,        where,        fields,        watch      }    };    if (sessionToken) {      subscribeRequest.sessionToken = sessionToken;    }    const subscription = new _LiveQuerySubscription.default(this.requestId, query, sessionToken);    this.subscriptions.set(this.requestId, subscription);    this.requestId += 1;    this.connectPromise.then(() => {      this.socket.send(JSON.stringify(subscribeRequest));    }).catch(error => {      subscription.subscribePromise.reject(error);    });    return subscription;  }  /**   * After calling unsubscribe you'll stop receiving events from the subscription object.   *   * @param {object} subscription - subscription you would like to unsubscribe from.   * @returns {Promise | undefined}   */  unsubscribe(subscription /*: Object*/) /*: ?Promise*/{    if (!subscription) {      return;    }    const unsubscribeRequest = {      op: OP_TYPES.UNSUBSCRIBE,      requestId: subscription.id    };    return this.connectPromise.then(() => {      return this.socket.send(JSON.stringify(unsubscribeRequest));    }).then(() => {      return subscription.unsubscribePromise;    });  }  /**   * After open is called, the LiveQueryClient will try to send a connect request   * to the LiveQuery server.   *   */  open() {    const WebSocketImplementation = _CoreManager.default.getWebSocketController();    if (!WebSocketImplementation) {      this.emit(CLIENT_EMMITER_TYPES.ERROR, 'Can not find WebSocket implementation');      return;    }    if (this.state !== CLIENT_STATE.RECONNECTING) {      this.state = CLIENT_STATE.CONNECTING;    }    this.socket = new WebSocketImplementation(this.serverURL);    this.socket.closingPromise = (0, _promiseUtils.resolvingPromise)();    // Bind WebSocket callbacks    this.socket.onopen = () => {      this._handleWebSocketOpen();    };    this.socket.onmessage = event => {      this._handleWebSocketMessage(event);    };    this.socket.onclose = event => {      this.socket.closingPromise.resolve(event);      this._handleWebSocketClose();    };    this.socket.onerror = error => {      this._handleWebSocketError(error);    };  }  resubscribe() {    this.subscriptions.forEach((subscription, requestId) => {      const query = subscription.query;      const queryJSON = query.toJSON();      const where = queryJSON.where;      const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;      const className = query.className;      const sessionToken = subscription.sessionToken;      const subscribeRequest = {        op: OP_TYPES.SUBSCRIBE,        requestId,        query: {          className,          where,          fields        }      };      if (sessionToken) {        subscribeRequest.sessionToken = sessionToken;      }      this.connectPromise.then(() => {        this.socket.send(JSON.stringify(subscribeRequest));      });    });  }  /**   * This method will close the WebSocket connection to this LiveQueryClient,   * cancel the auto reconnect and unsubscribe all subscriptions based on it.   *   * @returns {Promise | undefined} CloseEvent {@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event}   */  close() /*: ?Promise*/{    var _this$socket, _this$socket2;    if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) {      return;    }    this.state = CLIENT_STATE.DISCONNECTED;    (_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.close();    // Notify each subscription about the close    for (const subscription of this.subscriptions.values()) {      subscription.subscribed = false;      subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);    }    this._handleReset();    this.emit(CLIENT_EMMITER_TYPES.CLOSE);    return (_this$socket2 = this.socket) === null || _this$socket2 === void 0 ? void 0 : _this$socket2.closingPromise;  }  // ensure we start with valid state if connect is called again after close  _handleReset() {    this.attempts = 1;    this.id = 0;    this.requestId = 1;    this.connectPromise = (0, _promiseUtils.resolvingPromise)();    this.subscriptions = new Map();  }  _handleWebSocketOpen() {    this.attempts = 1;    const connectRequest = {      op: OP_TYPES.CONNECT,      applicationId: this.applicationId,      javascriptKey: this.javascriptKey,      masterKey: this.masterKey,      sessionToken: this.sessionToken    };    if (this.additionalProperties) {      connectRequest.installationId = this.installationId;    }    this.socket.send(JSON.stringify(connectRequest));  }  _handleWebSocketMessage(event /*: any*/) {    let data = event.data;    if (typeof data === 'string') {      data = JSON.parse(data);    }    let subscription = null;    if (data.requestId) {      subscription = this.subscriptions.get(data.requestId);    }    const response = {      clientId: data.clientId,      installationId: data.installationId    };    switch (data.op) {      case OP_EVENTS.CONNECTED:        if (this.state === CLIENT_STATE.RECONNECTING) {          this.resubscribe();        }        this.emit(CLIENT_EMMITER_TYPES.OPEN);        this.id = data.clientId;        this.connectPromise.resolve();        this.state = CLIENT_STATE.CONNECTED;        break;      case OP_EVENTS.SUBSCRIBED:        if (subscription) {          subscription.subscribed = true;          subscription.subscribePromise.resolve();          setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN, response), 200);        }        break;      case OP_EVENTS.ERROR:        {          const parseError = new _ParseError.default(data.code, data.error);          if (!this.id) {            this.connectPromise.reject(parseError);            this.state = CLIENT_STATE.DISCONNECTED;          }          if (data.requestId) {            if (subscription) {              subscription.subscribePromise.reject(parseError);              setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, data.error), 200);            }          } else {            this.emit(CLIENT_EMMITER_TYPES.ERROR, data.error);          }          if (data.error === 'Additional properties not allowed') {            this.additionalProperties = false;          }          if (data.reconnect) {            this._handleReconnect();          }          break;        }      case OP_EVENTS.UNSUBSCRIBED:        {          if (subscription) {            this.subscriptions.delete(data.requestId);            subscription.subscribed = false;            subscription.unsubscribePromise.resolve();          }          break;        }      default:        {          // create, update, enter, leave, delete cases          if (!subscription) {            break;          }          let override = false;          if (data.original) {            override = true;            delete data.original.__type;            // Check for removed fields            for (const field in data.original) {              if (!(field in data.object)) {                data.object[field] = undefined;              }            }            data.original = _ParseObject.default.fromJSON(data.original, false);          }          delete data.object.__type;          const parseObject = _ParseObject.default.fromJSON(data.object, !(subscription.query && subscription.query._select) ? override : false);          if (data.original) {            subscription.emit(data.op, parseObject, data.original, response);          } else {            subscription.emit(data.op, parseObject, response);          }          const localDatastore = _CoreManager.default.getLocalDatastore();          if (override && localDatastore.isEnabled) {            localDatastore._updateObjectIfPinned(parseObject).then(() => {});          }        }    }  }  _handleWebSocketClose() {    if (this.state === CLIENT_STATE.DISCONNECTED) {      return;    }    this.state = CLIENT_STATE.CLOSED;    this.emit(CLIENT_EMMITER_TYPES.CLOSE);    // Notify each subscription about the close    for (const subscription of this.subscriptions.values()) {      subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);    }    this._handleReconnect();  }  _handleWebSocketError(error /*: any*/) {    this.emit(CLIENT_EMMITER_TYPES.ERROR, error);    for (const subscription of this.subscriptions.values()) {      subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, error);    }    this._handleReconnect();  }  _handleReconnect() {    // if closed or currently reconnecting we stop attempting to reconnect    if (this.state === CLIENT_STATE.DISCONNECTED) {      return;    }    this.state = CLIENT_STATE.RECONNECTING;    const time = generateInterval(this.attempts);    // handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.    // we're unable to distinguish different between close/error when we're unable to reconnect therefore    // we try to reconnect in both cases    // server side ws and browser WebSocket behave differently in when close/error get triggered    if (this.reconnectHandle) {      clearTimeout(this.reconnectHandle);    }    this.reconnectHandle = setTimeout((() => {      this.attempts++;      this.connectPromise = (0, _promiseUtils.resolvingPromise)();      this.open();    }).bind(this), time);  }}_CoreManager.default.setWebSocketController(require('ws'));var _default = LiveQueryClient;exports.default = _default;
 |