LiveQueryClient.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _EventEmitter = _interopRequireDefault(require("./EventEmitter"));
  8. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  9. var _LiveQuerySubscription = _interopRequireDefault(require("./LiveQuerySubscription"));
  10. var _promiseUtils = require("./promiseUtils");
  11. function _interopRequireDefault(obj) {
  12. return obj && obj.__esModule ? obj : {
  13. default: obj
  14. };
  15. }
  16. /**
  17. * Copyright (c) 2015-present, Parse, LLC.
  18. * All rights reserved.
  19. *
  20. * This source code is licensed under the BSD-style license found in the
  21. * LICENSE file in the root directory of this source tree. An additional grant
  22. * of patent rights can be found in the PATENTS file in the same directory.
  23. *
  24. */
  25. /* global WebSocket */
  26. // The LiveQuery client inner state
  27. const CLIENT_STATE = {
  28. INITIALIZED: 'initialized',
  29. CONNECTING: 'connecting',
  30. CONNECTED: 'connected',
  31. CLOSED: 'closed',
  32. RECONNECTING: 'reconnecting',
  33. DISCONNECTED: 'disconnected'
  34. }; // The event type the LiveQuery client should sent to server
  35. const OP_TYPES = {
  36. CONNECT: 'connect',
  37. SUBSCRIBE: 'subscribe',
  38. UNSUBSCRIBE: 'unsubscribe',
  39. ERROR: 'error'
  40. }; // The event we get back from LiveQuery server
  41. const OP_EVENTS = {
  42. CONNECTED: 'connected',
  43. SUBSCRIBED: 'subscribed',
  44. UNSUBSCRIBED: 'unsubscribed',
  45. ERROR: 'error',
  46. CREATE: 'create',
  47. UPDATE: 'update',
  48. ENTER: 'enter',
  49. LEAVE: 'leave',
  50. DELETE: 'delete'
  51. }; // The event the LiveQuery client should emit
  52. const CLIENT_EMMITER_TYPES = {
  53. CLOSE: 'close',
  54. ERROR: 'error',
  55. OPEN: 'open'
  56. }; // The event the LiveQuery subscription should emit
  57. const SUBSCRIPTION_EMMITER_TYPES = {
  58. OPEN: 'open',
  59. CLOSE: 'close',
  60. ERROR: 'error',
  61. CREATE: 'create',
  62. UPDATE: 'update',
  63. ENTER: 'enter',
  64. LEAVE: 'leave',
  65. DELETE: 'delete'
  66. };
  67. const generateInterval = k => {
  68. return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;
  69. };
  70. /**
  71. * Creates a new LiveQueryClient.
  72. * Extends events.EventEmitter
  73. * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>.
  74. *
  75. * A wrapper of a standard WebSocket client. We add several useful methods to
  76. * help you connect/disconnect to LiveQueryServer, subscribe/unsubscribe a ParseQuery easily.
  77. *
  78. * javascriptKey and masterKey are used for verifying the LiveQueryClient when it tries
  79. * to connect to the LiveQuery server
  80. *
  81. * We expose three events to help you monitor the status of the LiveQueryClient.
  82. *
  83. * <pre>
  84. * let Parse = require('parse/node');
  85. * let LiveQueryClient = Parse.LiveQueryClient;
  86. * let client = new LiveQueryClient({
  87. * applicationId: '',
  88. * serverURL: '',
  89. * javascriptKey: '',
  90. * masterKey: ''
  91. * });
  92. * </pre>
  93. *
  94. * Open - When we establish the WebSocket connection to the LiveQuery server, you'll get this event.
  95. * <pre>
  96. * client.on('open', () => {
  97. *
  98. * });</pre>
  99. *
  100. * Close - When we lose the WebSocket connection to the LiveQuery server, you'll get this event.
  101. * <pre>
  102. * client.on('close', () => {
  103. *
  104. * });</pre>
  105. *
  106. * Error - When some network error or LiveQuery server error happens, you'll get this event.
  107. * <pre>
  108. * client.on('error', (error) => {
  109. *
  110. * });</pre>
  111. *
  112. * @alias Parse.LiveQueryClient
  113. */
  114. class LiveQueryClient extends _EventEmitter.default {
  115. /*:: attempts: number;*/
  116. /*:: id: number;*/
  117. /*:: requestId: number;*/
  118. /*:: applicationId: string;*/
  119. /*:: serverURL: string;*/
  120. /*:: javascriptKey: ?string;*/
  121. /*:: masterKey: ?string;*/
  122. /*:: sessionToken: ?string;*/
  123. /*:: installationId: ?string;*/
  124. /*:: additionalProperties: boolean;*/
  125. /*:: connectPromise: Promise;*/
  126. /*:: subscriptions: Map;*/
  127. /*:: socket: any;*/
  128. /*:: state: string;*/
  129. /**
  130. * @param {object} options
  131. * @param {string} options.applicationId - applicationId of your Parse app
  132. * @param {string} options.serverURL - <b>the URL of your LiveQuery server</b>
  133. * @param {string} options.javascriptKey (optional)
  134. * @param {string} options.masterKey (optional) Your Parse Master Key. (Node.js only!)
  135. * @param {string} options.sessionToken (optional)
  136. * @param {string} options.installationId (optional)
  137. */
  138. constructor({
  139. applicationId,
  140. serverURL,
  141. javascriptKey,
  142. masterKey,
  143. sessionToken,
  144. installationId
  145. }) {
  146. super();
  147. if (!serverURL || serverURL.indexOf('ws') !== 0) {
  148. throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
  149. }
  150. this.reconnectHandle = null;
  151. this.attempts = 1;
  152. this.id = 0;
  153. this.requestId = 1;
  154. this.serverURL = serverURL;
  155. this.applicationId = applicationId;
  156. this.javascriptKey = javascriptKey || undefined;
  157. this.masterKey = masterKey || undefined;
  158. this.sessionToken = sessionToken || undefined;
  159. this.installationId = installationId || undefined;
  160. this.additionalProperties = true;
  161. this.connectPromise = (0, _promiseUtils.resolvingPromise)();
  162. this.subscriptions = new Map();
  163. this.state = CLIENT_STATE.INITIALIZED; // adding listener so process does not crash
  164. // best practice is for developer to register their own listener
  165. this.on('error', () => {});
  166. }
  167. shouldOpen()
  168. /*: any*/
  169. {
  170. return this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED;
  171. }
  172. /**
  173. * Subscribes to a ParseQuery
  174. *
  175. * If you provide the sessionToken, when the LiveQuery server gets ParseObject's
  176. * updates from parse server, it'll try to check whether the sessionToken fulfills
  177. * the ParseObject's ACL. The LiveQuery server will only send updates to clients whose
  178. * sessionToken is fit for the ParseObject's ACL. You can check the LiveQuery protocol
  179. * <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
  180. * from our Standard API.
  181. *
  182. * @param {object} query - the ParseQuery you want to subscribe to
  183. * @param {string} sessionToken (optional)
  184. * @returns {LiveQuerySubscription} subscription
  185. */
  186. subscribe(query
  187. /*: Object*/
  188. , sessionToken
  189. /*: ?string*/
  190. )
  191. /*: LiveQuerySubscription*/
  192. {
  193. if (!query) {
  194. return;
  195. }
  196. const className = query.className;
  197. const queryJSON = query.toJSON();
  198. const where = queryJSON.where;
  199. const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;
  200. const subscribeRequest = {
  201. op: OP_TYPES.SUBSCRIBE,
  202. requestId: this.requestId,
  203. query: {
  204. className,
  205. where,
  206. fields
  207. }
  208. };
  209. if (sessionToken) {
  210. subscribeRequest.sessionToken = sessionToken;
  211. }
  212. const subscription = new _LiveQuerySubscription.default(this.requestId, query, sessionToken);
  213. this.subscriptions.set(this.requestId, subscription);
  214. this.requestId += 1;
  215. this.connectPromise.then(() => {
  216. this.socket.send(JSON.stringify(subscribeRequest));
  217. });
  218. return subscription;
  219. }
  220. /**
  221. * After calling unsubscribe you'll stop receiving events from the subscription object.
  222. *
  223. * @param {object} subscription - subscription you would like to unsubscribe from.
  224. */
  225. unsubscribe(subscription
  226. /*: Object*/
  227. ) {
  228. if (!subscription) {
  229. return;
  230. }
  231. this.subscriptions.delete(subscription.id);
  232. const unsubscribeRequest = {
  233. op: OP_TYPES.UNSUBSCRIBE,
  234. requestId: subscription.id
  235. };
  236. this.connectPromise.then(() => {
  237. this.socket.send(JSON.stringify(unsubscribeRequest));
  238. });
  239. }
  240. /**
  241. * After open is called, the LiveQueryClient will try to send a connect request
  242. * to the LiveQuery server.
  243. *
  244. */
  245. open() {
  246. const WebSocketImplementation = _CoreManager.default.getWebSocketController();
  247. if (!WebSocketImplementation) {
  248. this.emit(CLIENT_EMMITER_TYPES.ERROR, 'Can not find WebSocket implementation');
  249. return;
  250. }
  251. if (this.state !== CLIENT_STATE.RECONNECTING) {
  252. this.state = CLIENT_STATE.CONNECTING;
  253. }
  254. this.socket = new WebSocketImplementation(this.serverURL); // Bind WebSocket callbacks
  255. this.socket.onopen = () => {
  256. this._handleWebSocketOpen();
  257. };
  258. this.socket.onmessage = event => {
  259. this._handleWebSocketMessage(event);
  260. };
  261. this.socket.onclose = () => {
  262. this._handleWebSocketClose();
  263. };
  264. this.socket.onerror = error => {
  265. this._handleWebSocketError(error);
  266. };
  267. }
  268. resubscribe() {
  269. this.subscriptions.forEach((subscription, requestId) => {
  270. const query = subscription.query;
  271. const queryJSON = query.toJSON();
  272. const where = queryJSON.where;
  273. const fields = queryJSON.keys ? queryJSON.keys.split(',') : undefined;
  274. const className = query.className;
  275. const sessionToken = subscription.sessionToken;
  276. const subscribeRequest = {
  277. op: OP_TYPES.SUBSCRIBE,
  278. requestId,
  279. query: {
  280. className,
  281. where,
  282. fields
  283. }
  284. };
  285. if (sessionToken) {
  286. subscribeRequest.sessionToken = sessionToken;
  287. }
  288. this.connectPromise.then(() => {
  289. this.socket.send(JSON.stringify(subscribeRequest));
  290. });
  291. });
  292. }
  293. /**
  294. * This method will close the WebSocket connection to this LiveQueryClient,
  295. * cancel the auto reconnect and unsubscribe all subscriptions based on it.
  296. *
  297. */
  298. close() {
  299. if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) {
  300. return;
  301. }
  302. this.state = CLIENT_STATE.DISCONNECTED;
  303. this.socket.close(); // Notify each subscription about the close
  304. for (const subscription of this.subscriptions.values()) {
  305. subscription.subscribed = false;
  306. subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
  307. }
  308. this._handleReset();
  309. this.emit(CLIENT_EMMITER_TYPES.CLOSE);
  310. } // ensure we start with valid state if connect is called again after close
  311. _handleReset() {
  312. this.attempts = 1;
  313. this.id = 0;
  314. this.requestId = 1;
  315. this.connectPromise = (0, _promiseUtils.resolvingPromise)();
  316. this.subscriptions = new Map();
  317. }
  318. _handleWebSocketOpen() {
  319. this.attempts = 1;
  320. const connectRequest = {
  321. op: OP_TYPES.CONNECT,
  322. applicationId: this.applicationId,
  323. javascriptKey: this.javascriptKey,
  324. masterKey: this.masterKey,
  325. sessionToken: this.sessionToken
  326. };
  327. if (this.additionalProperties) {
  328. connectRequest.installationId = this.installationId;
  329. }
  330. this.socket.send(JSON.stringify(connectRequest));
  331. }
  332. _handleWebSocketMessage(event
  333. /*: any*/
  334. ) {
  335. let data = event.data;
  336. if (typeof data === 'string') {
  337. data = JSON.parse(data);
  338. }
  339. let subscription = null;
  340. if (data.requestId) {
  341. subscription = this.subscriptions.get(data.requestId);
  342. }
  343. const response = {
  344. clientId: data.clientId,
  345. installationId: data.installationId
  346. };
  347. switch (data.op) {
  348. case OP_EVENTS.CONNECTED:
  349. if (this.state === CLIENT_STATE.RECONNECTING) {
  350. this.resubscribe();
  351. }
  352. this.emit(CLIENT_EMMITER_TYPES.OPEN);
  353. this.id = data.clientId;
  354. this.connectPromise.resolve();
  355. this.state = CLIENT_STATE.CONNECTED;
  356. break;
  357. case OP_EVENTS.SUBSCRIBED:
  358. if (subscription) {
  359. subscription.subscribed = true;
  360. subscription.subscribePromise.resolve();
  361. setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN, response), 200);
  362. }
  363. break;
  364. case OP_EVENTS.ERROR:
  365. if (data.requestId) {
  366. if (subscription) {
  367. subscription.subscribePromise.resolve();
  368. setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, data.error), 200);
  369. }
  370. } else {
  371. this.emit(CLIENT_EMMITER_TYPES.ERROR, data.error);
  372. }
  373. if (data.error === 'Additional properties not allowed') {
  374. this.additionalProperties = false;
  375. }
  376. if (data.reconnect) {
  377. this._handleReconnect();
  378. }
  379. break;
  380. case OP_EVENTS.UNSUBSCRIBED:
  381. // We have already deleted subscription in unsubscribe(), do nothing here
  382. break;
  383. default:
  384. {
  385. // create, update, enter, leave, delete cases
  386. if (!subscription) {
  387. break;
  388. }
  389. let override = false;
  390. if (data.original) {
  391. override = true;
  392. delete data.original.__type; // Check for removed fields
  393. for (const field in data.original) {
  394. if (!(field in data.object)) {
  395. data.object[field] = undefined;
  396. }
  397. }
  398. data.original = _ParseObject.default.fromJSON(data.original, false);
  399. }
  400. delete data.object.__type;
  401. const parseObject = _ParseObject.default.fromJSON(data.object, !(subscription.query && subscription.query._select) ? override : false);
  402. if (data.original) {
  403. subscription.emit(data.op, parseObject, data.original, response);
  404. } else {
  405. subscription.emit(data.op, parseObject, response);
  406. }
  407. const localDatastore = _CoreManager.default.getLocalDatastore();
  408. if (override && localDatastore.isEnabled) {
  409. localDatastore._updateObjectIfPinned(parseObject).then(() => {});
  410. }
  411. }
  412. }
  413. }
  414. _handleWebSocketClose() {
  415. if (this.state === CLIENT_STATE.DISCONNECTED) {
  416. return;
  417. }
  418. this.state = CLIENT_STATE.CLOSED;
  419. this.emit(CLIENT_EMMITER_TYPES.CLOSE); // Notify each subscription about the close
  420. for (const subscription of this.subscriptions.values()) {
  421. subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
  422. }
  423. this._handleReconnect();
  424. }
  425. _handleWebSocketError(error
  426. /*: any*/
  427. ) {
  428. this.emit(CLIENT_EMMITER_TYPES.ERROR, error);
  429. for (const subscription of this.subscriptions.values()) {
  430. subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, error);
  431. }
  432. this._handleReconnect();
  433. }
  434. _handleReconnect() {
  435. // if closed or currently reconnecting we stop attempting to reconnect
  436. if (this.state === CLIENT_STATE.DISCONNECTED) {
  437. return;
  438. }
  439. this.state = CLIENT_STATE.RECONNECTING;
  440. const time = generateInterval(this.attempts); // handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
  441. // we're unable to distinguish different between close/error when we're unable to reconnect therefore
  442. // we try to reconnect in both cases
  443. // server side ws and browser WebSocket behave differently in when close/error get triggered
  444. if (this.reconnectHandle) {
  445. clearTimeout(this.reconnectHandle);
  446. }
  447. this.reconnectHandle = setTimeout((() => {
  448. this.attempts++;
  449. this.connectPromise = (0, _promiseUtils.resolvingPromise)();
  450. this.open();
  451. }).bind(this), time);
  452. }
  453. }
  454. _CoreManager.default.setWebSocketController(require('ws'));
  455. var _default = LiveQueryClient;
  456. exports.default = _default;