OfflineQuery.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  3. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  4. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  5. if (it) return (it = it.call(o)).next.bind(it);
  6. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  7. if (it) o = it;
  8. var i = 0;
  9. return function () {
  10. if (i >= o.length) return {
  11. done: true
  12. };
  13. return {
  14. done: false,
  15. value: o[i++]
  16. };
  17. };
  18. }
  19. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  20. }
  21. function _unsupportedIterableToArray(o, minLen) {
  22. if (!o) return;
  23. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  24. var n = Object.prototype.toString.call(o).slice(8, -1);
  25. if (n === "Object" && o.constructor) n = o.constructor.name;
  26. if (n === "Map" || n === "Set") return Array.from(o);
  27. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  28. }
  29. function _arrayLikeToArray(arr, len) {
  30. if (len == null || len > arr.length) len = arr.length;
  31. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  32. arr2[i] = arr[i];
  33. }
  34. return arr2;
  35. }
  36. var equalObjects = require('./equals').default;
  37. var decode = require('./decode').default;
  38. var ParseError = require('./ParseError').default;
  39. var ParsePolygon = require('./ParsePolygon').default;
  40. var ParseGeoPoint = require('./ParseGeoPoint').default;
  41. function contains(haystack, needle) {
  42. if (needle && needle.__type && (needle.__type === 'Pointer' || needle.__type === 'Object')) {
  43. for (var i in haystack) {
  44. var ptr = haystack[i];
  45. if (typeof ptr === 'string' && ptr === needle.objectId) {
  46. return true;
  47. }
  48. if (ptr.className === needle.className && ptr.objectId === needle.objectId) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. return haystack.indexOf(needle) > -1;
  55. }
  56. function transformObject(object) {
  57. if (object._toFullJSON) {
  58. return object._toFullJSON();
  59. }
  60. return object;
  61. }
  62. function matchesQuery(className, object, objects, query) {
  63. if (object.className !== className) {
  64. return false;
  65. }
  66. var obj = object;
  67. var q = query;
  68. if (object.toJSON) {
  69. obj = object.toJSON();
  70. }
  71. if (query.toJSON) {
  72. q = query.toJSON().where;
  73. }
  74. obj.className = className;
  75. for (var field in q) {
  76. if (!matchesKeyConstraints(className, obj, objects, field, q[field])) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. function equalObjectsGeneric(obj, compareTo, eqlFn) {
  83. if (Array.isArray(obj)) {
  84. for (var i = 0; i < obj.length; i++) {
  85. if (eqlFn(obj[i], compareTo)) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. return eqlFn(obj, compareTo);
  92. }
  93. function relativeTimeToDate(text) {
  94. var now = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Date();
  95. text = text.toLowerCase();
  96. var parts = text.split(' ');
  97. parts = parts.filter(function (part) {
  98. return part !== '';
  99. });
  100. var future = parts[0] === 'in';
  101. var past = parts[parts.length - 1] === 'ago';
  102. if (!future && !past && text !== 'now') {
  103. return {
  104. status: 'error',
  105. info: "Time should either start with 'in' or end with 'ago'"
  106. };
  107. }
  108. if (future && past) {
  109. return {
  110. status: 'error',
  111. info: "Time cannot have both 'in' and 'ago'"
  112. };
  113. }
  114. if (future) {
  115. parts = parts.slice(1);
  116. } else {
  117. parts = parts.slice(0, parts.length - 1);
  118. }
  119. if (parts.length % 2 !== 0 && text !== 'now') {
  120. return {
  121. status: 'error',
  122. info: 'Invalid time string. Dangling unit or number.'
  123. };
  124. }
  125. var pairs = [];
  126. while (parts.length) {
  127. pairs.push([parts.shift(), parts.shift()]);
  128. }
  129. var seconds = 0;
  130. for (var _i = 0, _pairs = pairs; _i < _pairs.length; _i++) {
  131. var _ref = _pairs[_i];
  132. var _ref2 = (0, _slicedToArray2.default)(_ref, 2);
  133. var num = _ref2[0];
  134. var interval = _ref2[1];
  135. var val = Number(num);
  136. if (!Number.isInteger(val)) {
  137. return {
  138. status: 'error',
  139. info: "'" + num + "' is not an integer."
  140. };
  141. }
  142. switch (interval) {
  143. case 'yr':
  144. case 'yrs':
  145. case 'year':
  146. case 'years':
  147. seconds += val * 31536000;
  148. break;
  149. case 'wk':
  150. case 'wks':
  151. case 'week':
  152. case 'weeks':
  153. seconds += val * 604800;
  154. break;
  155. case 'd':
  156. case 'day':
  157. case 'days':
  158. seconds += val * 86400;
  159. break;
  160. case 'hr':
  161. case 'hrs':
  162. case 'hour':
  163. case 'hours':
  164. seconds += val * 3600;
  165. break;
  166. case 'min':
  167. case 'mins':
  168. case 'minute':
  169. case 'minutes':
  170. seconds += val * 60;
  171. break;
  172. case 'sec':
  173. case 'secs':
  174. case 'second':
  175. case 'seconds':
  176. seconds += val;
  177. break;
  178. default:
  179. return {
  180. status: 'error',
  181. info: "Invalid interval: '" + interval + "'"
  182. };
  183. }
  184. }
  185. var milliseconds = seconds * 1000;
  186. if (future) {
  187. return {
  188. status: 'success',
  189. info: 'future',
  190. result: new Date(now.valueOf() + milliseconds)
  191. };
  192. } else if (past) {
  193. return {
  194. status: 'success',
  195. info: 'past',
  196. result: new Date(now.valueOf() - milliseconds)
  197. };
  198. } else {
  199. return {
  200. status: 'success',
  201. info: 'present',
  202. result: new Date(now.valueOf())
  203. };
  204. }
  205. }
  206. function matchesKeyConstraints(className, object, objects, key, constraints) {
  207. if (constraints === null) {
  208. return false;
  209. }
  210. if (key.indexOf('.') >= 0) {
  211. var keyComponents = key.split('.');
  212. var subObjectKey = keyComponents[0];
  213. var keyRemainder = keyComponents.slice(1).join('.');
  214. return matchesKeyConstraints(className, object[subObjectKey] || {}, objects, keyRemainder, constraints);
  215. }
  216. var i;
  217. if (key === '$or') {
  218. for (i = 0; i < constraints.length; i++) {
  219. if (matchesQuery(className, object, objects, constraints[i])) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. if (key === '$and') {
  226. for (i = 0; i < constraints.length; i++) {
  227. if (!matchesQuery(className, object, objects, constraints[i])) {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. if (key === '$nor') {
  234. for (i = 0; i < constraints.length; i++) {
  235. if (matchesQuery(className, object, objects, constraints[i])) {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. if (key === '$relatedTo') {
  242. return false;
  243. }
  244. if (!/^[A-Za-z][0-9A-Za-z_]*$/.test(key)) {
  245. throw new ParseError(ParseError.INVALID_KEY_NAME, "Invalid Key: " + key);
  246. }
  247. if (typeof constraints !== 'object') {
  248. if (Array.isArray(object[key])) {
  249. return object[key].indexOf(constraints) > -1;
  250. }
  251. return object[key] === constraints;
  252. }
  253. var compareTo;
  254. if (constraints.__type) {
  255. if (constraints.__type === 'Pointer') {
  256. return equalObjectsGeneric(object[key], constraints, function (obj, ptr) {
  257. return typeof obj !== 'undefined' && ptr.className === obj.className && ptr.objectId === obj.objectId;
  258. });
  259. }
  260. return equalObjectsGeneric(decode(object[key]), decode(constraints), equalObjects);
  261. }
  262. for (var condition in constraints) {
  263. compareTo = constraints[condition];
  264. if (compareTo.__type) {
  265. compareTo = decode(compareTo);
  266. }
  267. if (compareTo['$relativeTime']) {
  268. var parserResult = relativeTimeToDate(compareTo['$relativeTime']);
  269. if (parserResult.status !== 'success') {
  270. throw new ParseError(ParseError.INVALID_JSON, "bad $relativeTime (" + key + ") value. " + parserResult.info);
  271. }
  272. compareTo = parserResult.result;
  273. }
  274. if (toString.call(compareTo) === '[object Date]' || typeof compareTo === 'string' && new Date(compareTo) !== 'Invalid Date' && !isNaN(new Date(compareTo))) {
  275. object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
  276. }
  277. switch (condition) {
  278. case '$lt':
  279. if (object[key] >= compareTo) {
  280. return false;
  281. }
  282. break;
  283. case '$lte':
  284. if (object[key] > compareTo) {
  285. return false;
  286. }
  287. break;
  288. case '$gt':
  289. if (object[key] <= compareTo) {
  290. return false;
  291. }
  292. break;
  293. case '$gte':
  294. if (object[key] < compareTo) {
  295. return false;
  296. }
  297. break;
  298. case '$ne':
  299. if (equalObjects(object[key], compareTo)) {
  300. return false;
  301. }
  302. break;
  303. case '$in':
  304. if (!contains(compareTo, object[key])) {
  305. return false;
  306. }
  307. break;
  308. case '$nin':
  309. if (contains(compareTo, object[key])) {
  310. return false;
  311. }
  312. break;
  313. case '$all':
  314. for (i = 0; i < compareTo.length; i++) {
  315. if (object[key].indexOf(compareTo[i]) < 0) {
  316. return false;
  317. }
  318. }
  319. break;
  320. case '$exists':
  321. {
  322. var propertyExists = typeof object[key] !== 'undefined';
  323. var existenceIsRequired = constraints['$exists'];
  324. if (typeof constraints['$exists'] !== 'boolean') {
  325. break;
  326. }
  327. if (!propertyExists && existenceIsRequired || propertyExists && !existenceIsRequired) {
  328. return false;
  329. }
  330. break;
  331. }
  332. case '$regex':
  333. {
  334. if (typeof compareTo === 'object') {
  335. return compareTo.test(object[key]);
  336. }
  337. var expString = '';
  338. var escapeEnd = -2;
  339. var escapeStart = compareTo.indexOf('\\Q');
  340. while (escapeStart > -1) {
  341. expString += compareTo.substring(escapeEnd + 2, escapeStart);
  342. escapeEnd = compareTo.indexOf('\\E', escapeStart);
  343. if (escapeEnd > -1) {
  344. expString += compareTo.substring(escapeStart + 2, escapeEnd).replace(/\\\\\\\\E/g, '\\E').replace(/\W/g, '\\$&');
  345. }
  346. escapeStart = compareTo.indexOf('\\Q', escapeEnd);
  347. }
  348. expString += compareTo.substring(Math.max(escapeStart, escapeEnd + 2));
  349. var modifiers = constraints.$options || '';
  350. modifiers = modifiers.replace('x', '').replace('s', '');
  351. var exp = new RegExp(expString, modifiers);
  352. if (!exp.test(object[key])) {
  353. return false;
  354. }
  355. break;
  356. }
  357. case '$nearSphere':
  358. {
  359. if (!compareTo || !object[key]) {
  360. return false;
  361. }
  362. var distance = compareTo.radiansTo(object[key]);
  363. var max = constraints.$maxDistance || Infinity;
  364. return distance <= max;
  365. }
  366. case '$within':
  367. {
  368. if (!compareTo || !object[key]) {
  369. return false;
  370. }
  371. var southWest = compareTo.$box[0];
  372. var northEast = compareTo.$box[1];
  373. if (southWest.latitude > northEast.latitude || southWest.longitude > northEast.longitude) {
  374. return false;
  375. }
  376. return object[key].latitude > southWest.latitude && object[key].latitude < northEast.latitude && object[key].longitude > southWest.longitude && object[key].longitude < northEast.longitude;
  377. }
  378. case '$options':
  379. break;
  380. case '$maxDistance':
  381. break;
  382. case '$select':
  383. {
  384. var subQueryObjects = objects.filter(function (obj, index, arr) {
  385. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  386. });
  387. for (var _i2 = 0; _i2 < subQueryObjects.length; _i2 += 1) {
  388. var subObject = transformObject(subQueryObjects[_i2]);
  389. return equalObjects(object[key], subObject[compareTo.key]);
  390. }
  391. return false;
  392. }
  393. case '$dontSelect':
  394. {
  395. var _subQueryObjects = objects.filter(function (obj, index, arr) {
  396. return matchesQuery(compareTo.query.className, obj, arr, compareTo.query.where);
  397. });
  398. for (var _i3 = 0; _i3 < _subQueryObjects.length; _i3 += 1) {
  399. var _subObject = transformObject(_subQueryObjects[_i3]);
  400. return !equalObjects(object[key], _subObject[compareTo.key]);
  401. }
  402. return false;
  403. }
  404. case '$inQuery':
  405. {
  406. var _subQueryObjects2 = objects.filter(function (obj, index, arr) {
  407. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  408. });
  409. for (var _i4 = 0; _i4 < _subQueryObjects2.length; _i4 += 1) {
  410. var _subObject2 = transformObject(_subQueryObjects2[_i4]);
  411. if (object[key].className === _subObject2.className && object[key].objectId === _subObject2.objectId) {
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. case '$notInQuery':
  418. {
  419. var _subQueryObjects3 = objects.filter(function (obj, index, arr) {
  420. return matchesQuery(compareTo.className, obj, arr, compareTo.where);
  421. });
  422. for (var _i5 = 0; _i5 < _subQueryObjects3.length; _i5 += 1) {
  423. var _subObject3 = transformObject(_subQueryObjects3[_i5]);
  424. if (object[key].className === _subObject3.className && object[key].objectId === _subObject3.objectId) {
  425. return false;
  426. }
  427. }
  428. return true;
  429. }
  430. case '$containedBy':
  431. {
  432. for (var _iterator = _createForOfIteratorHelperLoose(object[key]), _step; !(_step = _iterator()).done;) {
  433. var value = _step.value;
  434. if (!contains(compareTo, value)) {
  435. return false;
  436. }
  437. }
  438. return true;
  439. }
  440. case '$geoWithin':
  441. {
  442. if (compareTo.$polygon) {
  443. var points = compareTo.$polygon.map(function (geoPoint) {
  444. return [geoPoint.latitude, geoPoint.longitude];
  445. });
  446. var polygon = new ParsePolygon(points);
  447. return polygon.containsPoint(object[key]);
  448. }
  449. if (compareTo.$centerSphere) {
  450. var _compareTo$$centerSph = (0, _slicedToArray2.default)(compareTo.$centerSphere, 2),
  451. WGS84Point = _compareTo$$centerSph[0],
  452. maxDistance = _compareTo$$centerSph[1];
  453. var centerPoint = new ParseGeoPoint({
  454. latitude: WGS84Point[1],
  455. longitude: WGS84Point[0]
  456. });
  457. var point = new ParseGeoPoint(object[key]);
  458. var _distance = point.radiansTo(centerPoint);
  459. return _distance <= maxDistance;
  460. }
  461. break;
  462. }
  463. case '$geoIntersects':
  464. {
  465. var _polygon = new ParsePolygon(object[key].coordinates);
  466. var _point = new ParseGeoPoint(compareTo.$point);
  467. return _polygon.containsPoint(_point);
  468. }
  469. default:
  470. return false;
  471. }
  472. }
  473. return true;
  474. }
  475. function validateQuery(query) {
  476. var q = query;
  477. if (query.toJSON) {
  478. q = query.toJSON().where;
  479. }
  480. var specialQuerykeys = ['$and', '$or', '$nor', '_rperm', '_wperm', '_perishable_token', '_email_verify_token', '_email_verify_token_expires_at', '_account_lockout_expires_at', '_failed_login_count'];
  481. Object.keys(q).forEach(function (key) {
  482. if (q && q[key] && q[key].$regex) {
  483. if (typeof q[key].$options === 'string') {
  484. if (!q[key].$options.match(/^[imxs]+$/)) {
  485. throw new ParseError(ParseError.INVALID_QUERY, "Bad $options value for query: " + q[key].$options);
  486. }
  487. }
  488. }
  489. if (specialQuerykeys.indexOf(key) < 0 && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
  490. throw new ParseError(ParseError.INVALID_KEY_NAME, "Invalid key name: " + key);
  491. }
  492. });
  493. }
  494. var OfflineQuery = {
  495. matchesQuery: matchesQuery,
  496. validateQuery: validateQuery
  497. };
  498. module.exports = OfflineQuery;