LiveQueryClient.js 15 KB

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