CoreManager.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
  4. var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
  5. /*
  6. * Copyright (c) 2015-present, Parse, LLC.
  7. * All rights reserved.
  8. *
  9. * This source code is licensed under the BSD-style license found in the
  10. * LICENSE file in the root directory of this source tree. An additional grant
  11. * of patent rights can be found in the PATENTS file in the same directory.
  12. *
  13. * @flow
  14. */
  15. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  16. /*:: import type ParseFile from './ParseFile';*/
  17. /*:: import type { FileSource } from './ParseFile';*/
  18. /*:: import type { Op } from './ParseOp';*/
  19. /*:: import type ParseObject from './ParseObject';*/
  20. /*:: import type { QueryJSON } from './ParseQuery';*/
  21. /*:: import type ParseUser from './ParseUser';*/
  22. /*:: import type { AuthData } from './ParseUser';*/
  23. /*:: import type { PushData } from './Push';*/
  24. /*:: import type { RequestOptions, FullOptions } from './RESTController';*/
  25. /*:: type AnalyticsController = {
  26. track: (name: string, dimensions: { [key: string]: string }) => Promise,
  27. };*/
  28. /*:: type CloudController = {
  29. run: (name: string, data: mixed, options: RequestOptions) => Promise,
  30. getJobsData: (options: RequestOptions) => Promise,
  31. startJob: (name: string, data: mixed, options: RequestOptions) => Promise,
  32. };*/
  33. /*:: type ConfigController = {
  34. current: () => Promise,
  35. get: () => Promise,
  36. save: (attrs: { [key: string]: any }) => Promise,
  37. };*/
  38. /*:: type CryptoController = {
  39. encrypt: (obj: any, secretKey: string) => string,
  40. decrypt: (encryptedText: string, secretKey: any) => string,
  41. };*/
  42. /*:: type FileController = {
  43. saveFile: (name: string, source: FileSource, options: FullOptions) => Promise,
  44. saveBase64: (name: string, source: FileSource, options: FullOptions) => Promise,
  45. download: (uri: string) => Promise,
  46. };*/
  47. /*:: type InstallationController = {
  48. currentInstallationId: () => Promise,
  49. };*/
  50. /*:: type ObjectController = {
  51. fetch: (
  52. object: ParseObject | Array<ParseObject>,
  53. forceFetch: boolean,
  54. options: RequestOptions
  55. ) => Promise,
  56. save: (object: ParseObject | Array<ParseObject | ParseFile>, options: RequestOptions) => Promise,
  57. destroy: (object: ParseObject | Array<ParseObject>, options: RequestOptions) => Promise,
  58. };*/
  59. /*:: type ObjectStateController = {
  60. getState: (obj: any) => ?State,
  61. initializeState: (obj: any, initial?: State) => State,
  62. removeState: (obj: any) => ?State,
  63. getServerData: (obj: any) => AttributeMap,
  64. setServerData: (obj: any, attributes: AttributeMap) => void,
  65. getPendingOps: (obj: any) => Array<OpsMap>,
  66. setPendingOp: (obj: any, attr: string, op: ?Op) => void,
  67. pushPendingState: (obj: any) => void,
  68. popPendingState: (obj: any) => OpsMap,
  69. mergeFirstPendingState: (obj: any) => void,
  70. getObjectCache: (obj: any) => ObjectCache,
  71. estimateAttribute: (obj: any, attr: string) => mixed,
  72. estimateAttributes: (obj: any) => AttributeMap,
  73. commitServerChanges: (obj: any, changes: AttributeMap) => void,
  74. enqueueTask: (obj: any, task: () => Promise) => Promise,
  75. clearAllState: () => void,
  76. duplicateState: (source: any, dest: any) => void,
  77. };*/
  78. /*:: type PushController = {
  79. send: (data: PushData) => Promise,
  80. };*/
  81. /*:: type QueryController = {
  82. find: (className: string, params: QueryJSON, options: RequestOptions) => Promise,
  83. aggregate: (className: string, params: any, options: RequestOptions) => Promise,
  84. };*/
  85. /*:: type RESTController = {
  86. request: (method: string, path: string, data: mixed, options: RequestOptions) => Promise,
  87. ajax: (method: string, url: string, data: any, headers?: any, options: FullOptions) => Promise,
  88. };*/
  89. /*:: type SchemaController = {
  90. purge: (className: string) => Promise,
  91. get: (className: string, options: RequestOptions) => Promise,
  92. delete: (className: string, options: RequestOptions) => Promise,
  93. create: (className: string, params: any, options: RequestOptions) => Promise,
  94. update: (className: string, params: any, options: RequestOptions) => Promise,
  95. send(className: string, method: string, params: any, options: RequestOptions): Promise,
  96. };*/
  97. /*:: type SessionController = {
  98. getSession: (token: RequestOptions) => Promise,
  99. };*/
  100. /*:: type StorageController =
  101. | {
  102. async: 0,
  103. getItem: (path: string) => ?string,
  104. setItem: (path: string, value: string) => void,
  105. removeItem: (path: string) => void,
  106. getItemAsync?: (path: string) => Promise,
  107. setItemAsync?: (path: string, value: string) => Promise,
  108. removeItemAsync?: (path: string) => Promise,
  109. clear: () => void,
  110. }
  111. | {
  112. async: 1,
  113. getItem?: (path: string) => ?string,
  114. setItem?: (path: string, value: string) => void,
  115. removeItem?: (path: string) => void,
  116. getItemAsync: (path: string) => Promise,
  117. setItemAsync: (path: string, value: string) => Promise,
  118. removeItemAsync: (path: string) => Promise,
  119. clear: () => void,
  120. };*/
  121. /*:: type LocalDatastoreController = {
  122. fromPinWithName: (name: string) => ?any,
  123. pinWithName: (name: string, objects: any) => void,
  124. unPinWithName: (name: string) => void,
  125. getAllContents: () => ?any,
  126. clear: () => void,
  127. };*/
  128. /*:: type UserController = {
  129. setCurrentUser: (user: ParseUser) => Promise,
  130. currentUser: () => ?ParseUser,
  131. currentUserAsync: () => Promise,
  132. signUp: (user: ParseUser, attrs: AttributeMap, options: RequestOptions) => Promise,
  133. logIn: (user: ParseUser, options: RequestOptions) => Promise,
  134. become: (options: RequestOptions) => Promise,
  135. hydrate: (userJSON: AttributeMap) => Promise,
  136. logOut: (options: RequestOptions) => Promise,
  137. me: (options: RequestOptions) => Promise,
  138. requestPasswordReset: (email: string, options: RequestOptions) => Promise,
  139. updateUserOnDisk: (user: ParseUser) => Promise,
  140. upgradeToRevocableSession: (user: ParseUser, options: RequestOptions) => Promise,
  141. linkWith: (user: ParseUser, authData: AuthData) => Promise,
  142. removeUserFromDisk: () => Promise,
  143. verifyPassword: (username: string, password: string, options: RequestOptions) => Promise,
  144. requestEmailVerification: (email: string, options: RequestOptions) => Promise,
  145. };*/
  146. /*:: type HooksController = {
  147. get: (type: string, functionName?: string, triggerName?: string) => Promise,
  148. create: (hook: mixed) => Promise,
  149. delete: (hook: mixed) => Promise,
  150. update: (hook: mixed) => Promise,
  151. send: (method: string, path: string, body?: mixed) => Promise,
  152. };*/
  153. /*:: type WebSocketController = {
  154. onopen: () => void,
  155. onmessage: (message: any) => void,
  156. onclose: () => void,
  157. onerror: (error: any) => void,
  158. send: (data: any) => void,
  159. close: () => void,
  160. };*/
  161. /*:: type Config = {
  162. AnalyticsController?: AnalyticsController,
  163. CloudController?: CloudController,
  164. ConfigController?: ConfigController,
  165. FileController?: FileController,
  166. InstallationController?: InstallationController,
  167. ObjectController?: ObjectController,
  168. ObjectStateController?: ObjectStateController,
  169. PushController?: PushController,
  170. QueryController?: QueryController,
  171. RESTController?: RESTController,
  172. SchemaController?: SchemaController,
  173. SessionController?: SessionController,
  174. StorageController?: StorageController,
  175. LocalDatastoreController?: LocalDatastoreController,
  176. UserController?: UserController,
  177. HooksController?: HooksController,
  178. WebSocketController?: WebSocketController,
  179. };*/
  180. var config
  181. /*: Config & { [key: string]: mixed }*/
  182. = {
  183. // Defaults
  184. IS_NODE: typeof process !== 'undefined' && !!process.versions && !!process.versions.node && !process.versions.electron,
  185. REQUEST_ATTEMPT_LIMIT: 5,
  186. REQUEST_BATCH_SIZE: 20,
  187. REQUEST_HEADERS: {},
  188. SERVER_URL: 'https://api.parse.com/1',
  189. SERVER_AUTH_TYPE: null,
  190. SERVER_AUTH_TOKEN: null,
  191. LIVEQUERY_SERVER_URL: null,
  192. ENCRYPTED_KEY: null,
  193. VERSION: "js".concat("1.5.9"),
  194. APPLICATION_ID: null,
  195. JAVASCRIPT_KEY: null,
  196. MASTER_KEY: null,
  197. USE_MASTER_KEY: false,
  198. PERFORM_USER_REWRITE: true,
  199. FORCE_REVOCABLE_SESSION: false,
  200. ENCRYPTED_USER: false,
  201. IDEMPOTENCY: false
  202. };
  203. function requireMethods(name
  204. /*: string*/
  205. , methods
  206. /*: Array<string>*/
  207. , controller
  208. /*: any*/
  209. ) {
  210. (0, _forEach.default)(methods).call(methods, function (func) {
  211. if (typeof controller[func] !== 'function') {
  212. var _context;
  213. throw new Error((0, _concat.default)(_context = "".concat(name, " must implement ")).call(_context, func, "()"));
  214. }
  215. });
  216. }
  217. module.exports = {
  218. get: function (key
  219. /*: string*/
  220. )
  221. /*: any*/
  222. {
  223. if (config.hasOwnProperty(key)) {
  224. return config[key];
  225. }
  226. throw new Error("Configuration key not found: ".concat(key));
  227. },
  228. set: function (key
  229. /*: string*/
  230. , value
  231. /*: any*/
  232. )
  233. /*: void*/
  234. {
  235. config[key] = value;
  236. },
  237. /* Specialized Controller Setters/Getters */
  238. setAnalyticsController: function (controller
  239. /*: AnalyticsController*/
  240. ) {
  241. requireMethods('AnalyticsController', ['track'], controller);
  242. config.AnalyticsController = controller;
  243. },
  244. getAnalyticsController: function ()
  245. /*: AnalyticsController*/
  246. {
  247. return config.AnalyticsController;
  248. },
  249. setCloudController: function (controller
  250. /*: CloudController*/
  251. ) {
  252. requireMethods('CloudController', ['run', 'getJobsData', 'startJob'], controller);
  253. config.CloudController = controller;
  254. },
  255. getCloudController: function ()
  256. /*: CloudController*/
  257. {
  258. return config.CloudController;
  259. },
  260. setConfigController: function (controller
  261. /*: ConfigController*/
  262. ) {
  263. requireMethods('ConfigController', ['current', 'get', 'save'], controller);
  264. config.ConfigController = controller;
  265. },
  266. getConfigController: function ()
  267. /*: ConfigController*/
  268. {
  269. return config.ConfigController;
  270. },
  271. setCryptoController: function (controller
  272. /*: CryptoController*/
  273. ) {
  274. requireMethods('CryptoController', ['encrypt', 'decrypt'], controller);
  275. config.CryptoController = controller;
  276. },
  277. getCryptoController: function ()
  278. /*: CryptoController*/
  279. {
  280. return config.CryptoController;
  281. },
  282. setFileController: function (controller
  283. /*: FileController*/
  284. ) {
  285. requireMethods('FileController', ['saveFile', 'saveBase64'], controller);
  286. config.FileController = controller;
  287. },
  288. getFileController: function ()
  289. /*: FileController*/
  290. {
  291. return config.FileController;
  292. },
  293. setInstallationController: function (controller
  294. /*: InstallationController*/
  295. ) {
  296. requireMethods('InstallationController', ['currentInstallationId'], controller);
  297. config.InstallationController = controller;
  298. },
  299. getInstallationController: function ()
  300. /*: InstallationController*/
  301. {
  302. return config.InstallationController;
  303. },
  304. setObjectController: function (controller
  305. /*: ObjectController*/
  306. ) {
  307. requireMethods('ObjectController', ['save', 'fetch', 'destroy'], controller);
  308. config.ObjectController = controller;
  309. },
  310. getObjectController: function ()
  311. /*: ObjectController*/
  312. {
  313. return config.ObjectController;
  314. },
  315. setObjectStateController: function (controller
  316. /*: ObjectStateController*/
  317. ) {
  318. requireMethods('ObjectStateController', ['getState', 'initializeState', 'removeState', 'getServerData', 'setServerData', 'getPendingOps', 'setPendingOp', 'pushPendingState', 'popPendingState', 'mergeFirstPendingState', 'getObjectCache', 'estimateAttribute', 'estimateAttributes', 'commitServerChanges', 'enqueueTask', 'clearAllState'], controller);
  319. config.ObjectStateController = controller;
  320. },
  321. getObjectStateController: function ()
  322. /*: ObjectStateController*/
  323. {
  324. return config.ObjectStateController;
  325. },
  326. setPushController: function (controller
  327. /*: PushController*/
  328. ) {
  329. requireMethods('PushController', ['send'], controller);
  330. config.PushController = controller;
  331. },
  332. getPushController: function ()
  333. /*: PushController*/
  334. {
  335. return config.PushController;
  336. },
  337. setQueryController: function (controller
  338. /*: QueryController*/
  339. ) {
  340. requireMethods('QueryController', ['find', 'aggregate'], controller);
  341. config.QueryController = controller;
  342. },
  343. getQueryController: function ()
  344. /*: QueryController*/
  345. {
  346. return config.QueryController;
  347. },
  348. setRESTController: function (controller
  349. /*: RESTController*/
  350. ) {
  351. requireMethods('RESTController', ['request', 'ajax'], controller);
  352. config.RESTController = controller;
  353. },
  354. getRESTController: function ()
  355. /*: RESTController*/
  356. {
  357. return config.RESTController;
  358. },
  359. setSchemaController: function (controller
  360. /*: SchemaController*/
  361. ) {
  362. requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send', 'purge'], controller);
  363. config.SchemaController = controller;
  364. },
  365. getSchemaController: function ()
  366. /*: SchemaController*/
  367. {
  368. return config.SchemaController;
  369. },
  370. setSessionController: function (controller
  371. /*: SessionController*/
  372. ) {
  373. requireMethods('SessionController', ['getSession'], controller);
  374. config.SessionController = controller;
  375. },
  376. getSessionController: function ()
  377. /*: SessionController*/
  378. {
  379. return config.SessionController;
  380. },
  381. setStorageController: function (controller
  382. /*: StorageController*/
  383. ) {
  384. if (controller.async) {
  385. requireMethods('An async StorageController', ['getItemAsync', 'setItemAsync', 'removeItemAsync', 'getAllKeysAsync'], controller);
  386. } else {
  387. requireMethods('A synchronous StorageController', ['getItem', 'setItem', 'removeItem', 'getAllKeys'], controller);
  388. }
  389. config.StorageController = controller;
  390. },
  391. setLocalDatastoreController: function (controller
  392. /*: LocalDatastoreController*/
  393. ) {
  394. requireMethods('LocalDatastoreController', ['pinWithName', 'fromPinWithName', 'unPinWithName', 'getAllContents', 'clear'], controller);
  395. config.LocalDatastoreController = controller;
  396. },
  397. getLocalDatastoreController: function ()
  398. /*: LocalDatastoreController*/
  399. {
  400. return config.LocalDatastoreController;
  401. },
  402. setLocalDatastore: function (store
  403. /*: any*/
  404. ) {
  405. config.LocalDatastore = store;
  406. },
  407. getLocalDatastore: function () {
  408. return config.LocalDatastore;
  409. },
  410. getStorageController: function ()
  411. /*: StorageController*/
  412. {
  413. return config.StorageController;
  414. },
  415. setAsyncStorage: function (storage
  416. /*: any*/
  417. ) {
  418. config.AsyncStorage = storage;
  419. },
  420. getAsyncStorage: function () {
  421. return config.AsyncStorage;
  422. },
  423. setWebSocketController: function (controller
  424. /*: WebSocketController*/
  425. ) {
  426. config.WebSocketController = controller;
  427. },
  428. getWebSocketController: function ()
  429. /*: WebSocketController*/
  430. {
  431. return config.WebSocketController;
  432. },
  433. setUserController: function (controller
  434. /*: UserController*/
  435. ) {
  436. requireMethods('UserController', ['setCurrentUser', 'currentUser', 'currentUserAsync', 'signUp', 'logIn', 'become', 'logOut', 'me', 'requestPasswordReset', 'upgradeToRevocableSession', 'requestEmailVerification', 'verifyPassword', 'linkWith'], controller);
  437. config.UserController = controller;
  438. },
  439. getUserController: function ()
  440. /*: UserController*/
  441. {
  442. return config.UserController;
  443. },
  444. setLiveQueryController: function (controller
  445. /*: any*/
  446. ) {
  447. requireMethods('LiveQueryController', ['setDefaultLiveQueryClient', 'getDefaultLiveQueryClient', '_clearCachedDefaultClient'], controller);
  448. config.LiveQueryController = controller;
  449. },
  450. getLiveQueryController: function ()
  451. /*: any*/
  452. {
  453. return config.LiveQueryController;
  454. },
  455. setHooksController: function (controller
  456. /*: HooksController*/
  457. ) {
  458. requireMethods('HooksController', ['create', 'get', 'update', 'remove'], controller);
  459. config.HooksController = controller;
  460. },
  461. getHooksController: function ()
  462. /*: HooksController*/
  463. {
  464. return config.HooksController;
  465. }
  466. };