json-patch-duplex.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*!
  2. * https://github.com/Starcounter-Jack/JSON-Patch
  3. * json-patch-duplex.js version: 0.5.7
  4. * (c) 2013 Joachim Wester
  5. * MIT license
  6. */
  7. var __extends = (this && this.__extends) || function (d, b) {
  8. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  9. function __() { this.constructor = d; }
  10. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  11. };
  12. var OriginalError = Error;
  13. var jsonpatch;
  14. (function (jsonpatch) {
  15. var _objectKeys = function (obj) {
  16. if (_isArray(obj)) {
  17. var keys = new Array(obj.length);
  18. for (var k = 0; k < keys.length; k++) {
  19. keys[k] = "" + k;
  20. }
  21. return keys;
  22. }
  23. if (Object.keys) {
  24. return Object.keys(obj);
  25. }
  26. var keys = [];
  27. for (var i in obj) {
  28. if (obj.hasOwnProperty(i)) {
  29. keys.push(i);
  30. }
  31. }
  32. return keys;
  33. };
  34. function _equals(a, b) {
  35. switch (typeof a) {
  36. case 'undefined': //backward compatibility, but really I think we should return false
  37. case 'boolean':
  38. case 'string':
  39. case 'number':
  40. return a === b;
  41. case 'object':
  42. if (a === null)
  43. return b === null;
  44. if (_isArray(a)) {
  45. if (!_isArray(b) || a.length !== b.length)
  46. return false;
  47. for (var i = 0, l = a.length; i < l; i++)
  48. if (!_equals(a[i], b[i]))
  49. return false;
  50. return true;
  51. }
  52. var bKeys = _objectKeys(b);
  53. var bLength = bKeys.length;
  54. if (_objectKeys(a).length !== bLength)
  55. return false;
  56. for (var i = 0; i < bLength; i++)
  57. if (!_equals(a[i], b[i]))
  58. return false;
  59. return true;
  60. default:
  61. return false;
  62. }
  63. }
  64. /* We use a Javascript hash to store each
  65. function. Each hash entry (property) uses
  66. the operation identifiers specified in rfc6902.
  67. In this way, we can map each patch operation
  68. to its dedicated function in efficient way.
  69. */
  70. /* The operations applicable to an object */
  71. var objOps = {
  72. add: function (obj, key) {
  73. obj[key] = this.value;
  74. return true;
  75. },
  76. remove: function (obj, key) {
  77. delete obj[key];
  78. return true;
  79. },
  80. replace: function (obj, key) {
  81. obj[key] = this.value;
  82. return true;
  83. },
  84. move: function (obj, key, tree) {
  85. var temp = { op: "_get", path: this.from };
  86. apply(tree, [temp]);
  87. apply(tree, [
  88. { op: "remove", path: this.from }
  89. ]);
  90. apply(tree, [
  91. { op: "add", path: this.path, value: temp.value }
  92. ]);
  93. return true;
  94. },
  95. copy: function (obj, key, tree) {
  96. var temp = { op: "_get", path: this.from };
  97. apply(tree, [temp]);
  98. apply(tree, [
  99. { op: "add", path: this.path, value: temp.value }
  100. ]);
  101. return true;
  102. },
  103. test: function (obj, key) {
  104. return _equals(obj[key], this.value);
  105. },
  106. _get: function (obj, key) {
  107. this.value = obj[key];
  108. }
  109. };
  110. /* The operations applicable to an array. Many are the same as for the object */
  111. var arrOps = {
  112. add: function (arr, i) {
  113. arr.splice(i, 0, this.value);
  114. return true;
  115. },
  116. remove: function (arr, i) {
  117. arr.splice(i, 1);
  118. return true;
  119. },
  120. replace: function (arr, i) {
  121. arr[i] = this.value;
  122. return true;
  123. },
  124. move: objOps.move,
  125. copy: objOps.copy,
  126. test: objOps.test,
  127. _get: objOps._get
  128. };
  129. /* The operations applicable to object root. Many are the same as for the object */
  130. var rootOps = {
  131. add: function (obj) {
  132. rootOps.remove.call(this, obj);
  133. for (var key in this.value) {
  134. if (this.value.hasOwnProperty(key)) {
  135. obj[key] = this.value[key];
  136. }
  137. }
  138. return true;
  139. },
  140. remove: function (obj) {
  141. for (var key in obj) {
  142. if (obj.hasOwnProperty(key)) {
  143. objOps.remove.call(this, obj, key);
  144. }
  145. }
  146. return true;
  147. },
  148. replace: function (obj) {
  149. apply(obj, [
  150. { op: "remove", path: this.path }
  151. ]);
  152. apply(obj, [
  153. { op: "add", path: this.path, value: this.value }
  154. ]);
  155. return true;
  156. },
  157. move: objOps.move,
  158. copy: objOps.copy,
  159. test: function (obj) {
  160. return (JSON.stringify(obj) === JSON.stringify(this.value));
  161. },
  162. _get: function (obj) {
  163. this.value = obj;
  164. }
  165. };
  166. var observeOps = {
  167. add: function (patches, path) {
  168. var patch = {
  169. op: "add",
  170. path: path + escapePathComponent(this.name),
  171. value: this.object[this.name] };
  172. patches.push(patch);
  173. },
  174. 'delete': function (patches, path) {
  175. var patch = {
  176. op: "remove",
  177. path: path + escapePathComponent(this.name)
  178. };
  179. patches.push(patch);
  180. },
  181. update: function (patches, path) {
  182. var patch = {
  183. op: "replace",
  184. path: path + escapePathComponent(this.name),
  185. value: this.object[this.name]
  186. };
  187. patches.push(patch);
  188. }
  189. };
  190. function escapePathComponent(str) {
  191. if (str.indexOf('/') === -1 && str.indexOf('~') === -1)
  192. return str;
  193. return str.replace(/~/g, '~0').replace(/\//g, '~1');
  194. }
  195. function _getPathRecursive(root, obj) {
  196. var found;
  197. for (var key in root) {
  198. if (root.hasOwnProperty(key)) {
  199. if (root[key] === obj) {
  200. return escapePathComponent(key) + '/';
  201. }
  202. else if (typeof root[key] === 'object') {
  203. found = _getPathRecursive(root[key], obj);
  204. if (found != '') {
  205. return escapePathComponent(key) + '/' + found;
  206. }
  207. }
  208. }
  209. }
  210. return '';
  211. }
  212. function getPath(root, obj) {
  213. if (root === obj) {
  214. return '/';
  215. }
  216. var path = _getPathRecursive(root, obj);
  217. if (path === '') {
  218. throw new OriginalError("Object not found in root");
  219. }
  220. return '/' + path;
  221. }
  222. var beforeDict = [];
  223. var Mirror = (function () {
  224. function Mirror(obj) {
  225. this.observers = [];
  226. this.obj = obj;
  227. }
  228. return Mirror;
  229. })();
  230. var ObserverInfo = (function () {
  231. function ObserverInfo(callback, observer) {
  232. this.callback = callback;
  233. this.observer = observer;
  234. }
  235. return ObserverInfo;
  236. })();
  237. function getMirror(obj) {
  238. for (var i = 0, ilen = beforeDict.length; i < ilen; i++) {
  239. if (beforeDict[i].obj === obj) {
  240. return beforeDict[i];
  241. }
  242. }
  243. }
  244. function getObserverFromMirror(mirror, callback) {
  245. for (var j = 0, jlen = mirror.observers.length; j < jlen; j++) {
  246. if (mirror.observers[j].callback === callback) {
  247. return mirror.observers[j].observer;
  248. }
  249. }
  250. }
  251. function removeObserverFromMirror(mirror, observer) {
  252. for (var j = 0, jlen = mirror.observers.length; j < jlen; j++) {
  253. if (mirror.observers[j].observer === observer) {
  254. mirror.observers.splice(j, 1);
  255. return;
  256. }
  257. }
  258. }
  259. function unobserve(root, observer) {
  260. observer.unobserve();
  261. }
  262. jsonpatch.unobserve = unobserve;
  263. function deepClone(obj) {
  264. if (typeof obj === "object") {
  265. return JSON.parse(JSON.stringify(obj)); //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
  266. }
  267. else {
  268. return obj; //no need to clone primitives
  269. }
  270. }
  271. function observe(obj, callback) {
  272. var patches = [];
  273. var root = obj;
  274. var observer;
  275. var mirror = getMirror(obj);
  276. if (!mirror) {
  277. mirror = new Mirror(obj);
  278. beforeDict.push(mirror);
  279. }
  280. else {
  281. observer = getObserverFromMirror(mirror, callback);
  282. }
  283. if (observer) {
  284. return observer;
  285. }
  286. observer = {};
  287. mirror.value = deepClone(obj);
  288. if (callback) {
  289. observer.callback = callback;
  290. observer.next = null;
  291. var intervals = this.intervals || [100, 1000, 10000, 60000];
  292. if (intervals.push === void 0) {
  293. throw new OriginalError("jsonpatch.intervals must be an array");
  294. }
  295. var currentInterval = 0;
  296. var dirtyCheck = function () {
  297. generate(observer);
  298. };
  299. var fastCheck = function () {
  300. clearTimeout(observer.next);
  301. observer.next = setTimeout(function () {
  302. dirtyCheck();
  303. currentInterval = 0;
  304. observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
  305. }, 0);
  306. };
  307. var slowCheck = function () {
  308. dirtyCheck();
  309. if (currentInterval == intervals.length)
  310. currentInterval = intervals.length - 1;
  311. observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
  312. };
  313. if (typeof window !== 'undefined') {
  314. if (window.addEventListener) {
  315. window.addEventListener('mousedown', fastCheck);
  316. window.addEventListener('mouseup', fastCheck);
  317. window.addEventListener('keydown', fastCheck);
  318. }
  319. else {
  320. document.documentElement.attachEvent('onmousedown', fastCheck);
  321. document.documentElement.attachEvent('onmouseup', fastCheck);
  322. document.documentElement.attachEvent('onkeydown', fastCheck);
  323. }
  324. }
  325. observer.next = setTimeout(slowCheck, intervals[currentInterval++]);
  326. }
  327. observer.patches = patches;
  328. observer.object = obj;
  329. observer.unobserve = function () {
  330. generate(observer);
  331. clearTimeout(observer.next);
  332. removeObserverFromMirror(mirror, observer);
  333. if (typeof window !== 'undefined') {
  334. if (window.removeEventListener) {
  335. window.removeEventListener('mousedown', fastCheck);
  336. window.removeEventListener('mouseup', fastCheck);
  337. window.removeEventListener('keydown', fastCheck);
  338. }
  339. else {
  340. document.documentElement.detachEvent('onmousedown', fastCheck);
  341. document.documentElement.detachEvent('onmouseup', fastCheck);
  342. document.documentElement.detachEvent('onkeydown', fastCheck);
  343. }
  344. }
  345. };
  346. mirror.observers.push(new ObserverInfo(callback, observer));
  347. return observer;
  348. }
  349. jsonpatch.observe = observe;
  350. function generate(observer) {
  351. var mirror;
  352. for (var i = 0, ilen = beforeDict.length; i < ilen; i++) {
  353. if (beforeDict[i].obj === observer.object) {
  354. mirror = beforeDict[i];
  355. break;
  356. }
  357. }
  358. _generate(mirror.value, observer.object, observer.patches, "");
  359. if (observer.patches.length) {
  360. apply(mirror.value, observer.patches);
  361. }
  362. var temp = observer.patches;
  363. if (temp.length > 0) {
  364. observer.patches = [];
  365. if (observer.callback) {
  366. observer.callback(temp);
  367. }
  368. }
  369. return temp;
  370. }
  371. jsonpatch.generate = generate;
  372. // Dirty check if obj is different from mirror, generate patches and update mirror
  373. function _generate(mirror, obj, patches, path) {
  374. var newKeys = _objectKeys(obj);
  375. var oldKeys = _objectKeys(mirror);
  376. var changed = false;
  377. var deleted = false;
  378. //if ever "move" operation is implemented here, make sure this test runs OK: "should not generate the same patch twice (move)"
  379. for (var t = oldKeys.length - 1; t >= 0; t--) {
  380. var key = oldKeys[t];
  381. var oldVal = mirror[key];
  382. if (obj.hasOwnProperty(key)) {
  383. var newVal = obj[key];
  384. if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
  385. _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key));
  386. }
  387. else {
  388. if (oldVal != newVal) {
  389. changed = true;
  390. patches.push({ op: "replace", path: path + "/" + escapePathComponent(key), value: deepClone(newVal) });
  391. }
  392. }
  393. }
  394. else {
  395. patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
  396. deleted = true; // property has been deleted
  397. }
  398. }
  399. if (!deleted && newKeys.length == oldKeys.length) {
  400. return;
  401. }
  402. for (var t = 0; t < newKeys.length; t++) {
  403. var key = newKeys[t];
  404. if (!mirror.hasOwnProperty(key)) {
  405. patches.push({ op: "add", path: path + "/" + escapePathComponent(key), value: deepClone(obj[key]) });
  406. }
  407. }
  408. }
  409. var _isArray;
  410. if (Array.isArray) {
  411. _isArray = Array.isArray;
  412. }
  413. else {
  414. _isArray = function (obj) {
  415. return obj.push && typeof obj.length === 'number';
  416. };
  417. }
  418. //3x faster than cached /^\d+$/.test(str)
  419. function isInteger(str) {
  420. var i = 0;
  421. var len = str.length;
  422. var charCode;
  423. while (i < len) {
  424. charCode = str.charCodeAt(i);
  425. if (charCode >= 48 && charCode <= 57) {
  426. i++;
  427. continue;
  428. }
  429. return false;
  430. }
  431. return true;
  432. }
  433. /// Apply a json-patch operation on an object tree
  434. function apply(tree, patches, validate) {
  435. var result = false, p = 0, plen = patches.length, patch, key;
  436. while (p < plen) {
  437. patch = patches[p];
  438. p++;
  439. // Find the object
  440. var path = patch.path || "";
  441. var keys = path.split('/');
  442. var obj = tree;
  443. var t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift
  444. var len = keys.length;
  445. var existingPathFragment = undefined;
  446. while (true) {
  447. key = keys[t];
  448. if (validate) {
  449. if (existingPathFragment === undefined) {
  450. if (obj[key] === undefined) {
  451. existingPathFragment = keys.slice(0, t).join('/');
  452. }
  453. else if (t == len - 1) {
  454. existingPathFragment = patch.path;
  455. }
  456. if (existingPathFragment !== undefined) {
  457. this.validator(patch, p - 1, tree, existingPathFragment);
  458. }
  459. }
  460. }
  461. t++;
  462. if (key === undefined) {
  463. if (t >= len) {
  464. result = rootOps[patch.op].call(patch, obj, key, tree); // Apply patch
  465. break;
  466. }
  467. }
  468. if (_isArray(obj)) {
  469. if (key === '-') {
  470. key = obj.length;
  471. }
  472. else {
  473. if (validate && !isInteger(key)) {
  474. throw new JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", p - 1, patch.path, patch);
  475. }
  476. key = parseInt(key, 10);
  477. }
  478. if (t >= len) {
  479. if (validate && patch.op === "add" && key > obj.length) {
  480. throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", p - 1, patch.path, patch);
  481. }
  482. result = arrOps[patch.op].call(patch, obj, key, tree); // Apply patch
  483. break;
  484. }
  485. }
  486. else {
  487. if (key && key.indexOf('~') != -1)
  488. key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars
  489. if (t >= len) {
  490. result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch
  491. break;
  492. }
  493. }
  494. obj = obj[key];
  495. }
  496. }
  497. return result;
  498. }
  499. jsonpatch.apply = apply;
  500. function compare(tree1, tree2) {
  501. var patches = [];
  502. _generate(tree1, tree2, patches, '');
  503. return patches;
  504. }
  505. jsonpatch.compare = compare;
  506. var JsonPatchError = (function (_super) {
  507. __extends(JsonPatchError, _super);
  508. function JsonPatchError(message, name, index, operation, tree) {
  509. _super.call(this, message);
  510. this.message = message;
  511. this.name = name;
  512. this.index = index;
  513. this.operation = operation;
  514. this.tree = tree;
  515. }
  516. return JsonPatchError;
  517. })(OriginalError);
  518. jsonpatch.JsonPatchError = JsonPatchError;
  519. jsonpatch.Error = JsonPatchError;
  520. /**
  521. * Recursively checks whether an object has any undefined values inside.
  522. */
  523. function hasUndefined(obj) {
  524. if (obj === undefined) {
  525. return true;
  526. }
  527. if (typeof obj == "array" || typeof obj == "object") {
  528. for (var i in obj) {
  529. if (hasUndefined(obj[i])) {
  530. return true;
  531. }
  532. }
  533. }
  534. return false;
  535. }
  536. /**
  537. * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
  538. * @param {object} operation - operation object (patch)
  539. * @param {number} index - index of operation in the sequence
  540. * @param {object} [tree] - object where the operation is supposed to be applied
  541. * @param {string} [existingPathFragment] - comes along with `tree`
  542. */
  543. function validator(operation, index, tree, existingPathFragment) {
  544. if (typeof operation !== 'object' || operation === null || _isArray(operation)) {
  545. throw new JsonPatchError('Operation is not an object', 'OPERATION_NOT_AN_OBJECT', index, operation, tree);
  546. }
  547. else if (!objOps[operation.op]) {
  548. throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, tree);
  549. }
  550. else if (typeof operation.path !== 'string') {
  551. throw new JsonPatchError('Operation `path` property is not a string', 'OPERATION_PATH_INVALID', index, operation, tree);
  552. }
  553. else if ((operation.op === 'move' || operation.op === 'copy') && typeof operation.from !== 'string') {
  554. throw new JsonPatchError('Operation `from` property is not present (applicable in `move` and `copy` operations)', 'OPERATION_FROM_REQUIRED', index, operation, tree);
  555. }
  556. else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && operation.value === undefined) {
  557. throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, tree);
  558. }
  559. else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) {
  560. throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, tree);
  561. }
  562. else if (tree) {
  563. if (operation.op == "add") {
  564. var pathLen = operation.path.split("/").length;
  565. var existingPathLen = existingPathFragment.split("/").length;
  566. if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
  567. throw new JsonPatchError('Cannot perform an `add` operation at the desired path', 'OPERATION_PATH_CANNOT_ADD', index, operation, tree);
  568. }
  569. }
  570. else if (operation.op === 'replace' || operation.op === 'remove' || operation.op === '_get') {
  571. if (operation.path !== existingPathFragment) {
  572. throw new JsonPatchError('Cannot perform the operation at a path that does not exist', 'OPERATION_PATH_UNRESOLVABLE', index, operation, tree);
  573. }
  574. }
  575. else if (operation.op === 'move' || operation.op === 'copy') {
  576. var existingValue = { op: "_get", path: operation.from, value: undefined };
  577. var error = jsonpatch.validate([existingValue], tree);
  578. if (error && error.name === 'OPERATION_PATH_UNRESOLVABLE') {
  579. throw new JsonPatchError('Cannot perform the operation from a path that does not exist', 'OPERATION_FROM_UNRESOLVABLE', index, operation, tree);
  580. }
  581. }
  582. }
  583. }
  584. jsonpatch.validator = validator;
  585. /**
  586. * Validates a sequence of operations. If `tree` parameter is provided, the sequence is additionally validated against the object tree.
  587. * If error is encountered, returns a JsonPatchError object
  588. * @param sequence
  589. * @param tree
  590. * @returns {JsonPatchError|undefined}
  591. */
  592. function validate(sequence, tree) {
  593. try {
  594. if (!_isArray(sequence)) {
  595. throw new JsonPatchError('Patch sequence must be an array', 'SEQUENCE_NOT_AN_ARRAY');
  596. }
  597. if (tree) {
  598. tree = JSON.parse(JSON.stringify(tree)); //clone tree so that we can safely try applying operations
  599. apply.call(this, tree, sequence, true);
  600. }
  601. else {
  602. for (var i = 0; i < sequence.length; i++) {
  603. this.validator(sequence[i], i);
  604. }
  605. }
  606. }
  607. catch (e) {
  608. if (e instanceof JsonPatchError) {
  609. return e;
  610. }
  611. else {
  612. throw e;
  613. }
  614. }
  615. }
  616. jsonpatch.validate = validate;
  617. })(jsonpatch || (jsonpatch = {}));
  618. if (typeof exports !== "undefined") {
  619. exports.apply = jsonpatch.apply;
  620. exports.observe = jsonpatch.observe;
  621. exports.unobserve = jsonpatch.unobserve;
  622. exports.generate = jsonpatch.generate;
  623. exports.compare = jsonpatch.compare;
  624. exports.validate = jsonpatch.validate;
  625. exports.validator = jsonpatch.validator;
  626. exports.JsonPatchError = jsonpatch.JsonPatchError;
  627. exports.Error = jsonpatch.Error;
  628. }