change_request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. const AuthorList = require('./author_list')
  4. const Change = require('./change')
  5. const Operation = require('./operation')
  6. /**
  7. * @typedef {import("./author")} Author
  8. */
  9. /**
  10. * @constructor
  11. * @param {number} baseVersion
  12. * @param {Array.<Operation>} operations
  13. * @param {boolean} [untransformable]
  14. * @param {number[] | Author[]} [authors]
  15. * @classdesc
  16. * A `ChangeRequest` is a list of {@link Operation}s that the server can apply
  17. * as a {@link Change}.
  18. *
  19. * If the change is marked as `untransformable`, then the server will not
  20. * attempt to transform it if it is out of date (i.e. if the baseVersion no
  21. * longer matches the project's latest version). For example, if the client
  22. * needs to ensure that a metadata property is set on exactly one file, it can't
  23. * do that reliably if there's a chance that other clients will also change the
  24. * metadata at the same time. The expectation is that if the change is rejected,
  25. * the client will retry on a later version.
  26. */
  27. function ChangeRequest(baseVersion, operations, untransformable, authors) {
  28. assert.integer(baseVersion, 'bad baseVersion')
  29. assert.array.of.object(operations, 'bad operations')
  30. assert.maybe.boolean(untransformable, 'ChangeRequest: bad untransformable')
  31. // TODO remove authors once we have JWTs working --- pass as parameter to
  32. // makeChange instead
  33. authors = authors || []
  34. // check all are the same type
  35. AuthorList.assertV1(authors, 'bad authors')
  36. this.authors = authors
  37. this.baseVersion = baseVersion
  38. this.operations = operations
  39. this.untransformable = untransformable || false
  40. }
  41. module.exports = ChangeRequest
  42. /**
  43. * For serialization.
  44. *
  45. * @return {Object}
  46. */
  47. ChangeRequest.prototype.toRaw = function changeRequestToRaw() {
  48. function operationToRaw(operation) {
  49. return operation.toRaw()
  50. }
  51. return {
  52. baseVersion: this.baseVersion,
  53. operations: this.operations.map(operationToRaw),
  54. untransformable: this.untransformable,
  55. authors: this.authors,
  56. }
  57. }
  58. ChangeRequest.fromRaw = function changeRequestFromRaw(raw) {
  59. assert.array.of.object(raw.operations, 'bad raw.operations')
  60. return new ChangeRequest(
  61. raw.baseVersion,
  62. raw.operations.map(Operation.fromRaw),
  63. raw.untransformable,
  64. raw.authors
  65. )
  66. }
  67. ChangeRequest.prototype.getBaseVersion = function () {
  68. return this.baseVersion
  69. }
  70. ChangeRequest.prototype.isUntransformable = function () {
  71. return this.untransformable
  72. }
  73. ChangeRequest.prototype.makeChange = function changeRequestMakeChange(
  74. timestamp
  75. ) {
  76. return new Change(this.operations, timestamp, this.authors)
  77. }