index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. const Blob = require('../blob')
  4. // Dependencies are loaded at the bottom of the file to mitigate circular
  5. // dependency
  6. let BinaryFileData = null
  7. let HashFileData = null
  8. let HollowBinaryFileData = null
  9. let HollowStringFileData = null
  10. let LazyStringFileData = null
  11. let StringFileData = null
  12. /**
  13. * @typedef {import("../types").BlobStore} BlobStore
  14. * @typedef {import("../types").CommentRawData} CommentRawData
  15. */
  16. /**
  17. * Helper to represent the content of a file. This class and its subclasses
  18. * should be used only through {@link File}.
  19. */
  20. class FileData {
  21. /** @see File.fromRaw */
  22. static fromRaw(raw) {
  23. if (Object.prototype.hasOwnProperty.call(raw, 'hash')) {
  24. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  25. return BinaryFileData.fromRaw(raw)
  26. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  27. return LazyStringFileData.fromRaw(raw)
  28. return HashFileData.fromRaw(raw)
  29. }
  30. if (Object.prototype.hasOwnProperty.call(raw, 'byteLength'))
  31. return HollowBinaryFileData.fromRaw(raw)
  32. if (Object.prototype.hasOwnProperty.call(raw, 'stringLength'))
  33. return HollowStringFileData.fromRaw(raw)
  34. if (Object.prototype.hasOwnProperty.call(raw, 'content'))
  35. return StringFileData.fromRaw(raw)
  36. throw new Error('FileData: bad raw object ' + JSON.stringify(raw))
  37. }
  38. /** @see File.createHollow */
  39. static createHollow(byteLength, stringLength) {
  40. if (stringLength == null) {
  41. return new HollowBinaryFileData(byteLength)
  42. }
  43. return new HollowStringFileData(stringLength)
  44. }
  45. /** @see File.createLazyFromBlob */
  46. static createLazyFromBlob(blob) {
  47. assert.instance(blob, Blob, 'FileData: bad blob')
  48. if (blob.getStringLength() == null) {
  49. return new BinaryFileData(blob.getHash(), blob.getByteLength())
  50. }
  51. return new LazyStringFileData(blob.getHash(), blob.getStringLength())
  52. }
  53. toRaw() {
  54. throw new Error('FileData: toRaw not implemented')
  55. }
  56. /** @see File#getHash */
  57. getHash() {
  58. return null
  59. }
  60. /** @see File#getContent */
  61. getContent() {
  62. return null
  63. }
  64. /** @see File#isEditable */
  65. isEditable() {
  66. return null
  67. }
  68. /** @see File#getByteLength */
  69. getByteLength() {
  70. return null
  71. }
  72. /** @see File#getStringLength */
  73. getStringLength() {
  74. return null
  75. }
  76. /** @see File#edit */
  77. edit(textOperation) {
  78. throw new Error('edit not implemented for ' + JSON.stringify(this))
  79. }
  80. /**
  81. * @function
  82. * @param {BlobStore} blobStore
  83. * @return {Promise<FileData>}
  84. * @abstract
  85. * @see FileData#load
  86. */
  87. async toEager(blobStore) {
  88. throw new Error('toEager not implemented for ' + JSON.stringify(this))
  89. }
  90. /**
  91. * @function
  92. * @param {BlobStore} blobStore
  93. * @return {Promise<FileData>}
  94. * @abstract
  95. * @see FileData#load
  96. */
  97. async toLazy(blobStore) {
  98. throw new Error('toLazy not implemented for ' + JSON.stringify(this))
  99. }
  100. /**
  101. * @function
  102. * @param {BlobStore} blobStore
  103. * @return {Promise<FileData>}
  104. * @abstract
  105. * @see FileData#load
  106. */
  107. async toHollow(blobStore) {
  108. throw new Error('toHollow not implemented for ' + JSON.stringify(this))
  109. }
  110. /**
  111. * @see File#load
  112. * @param {string} kind
  113. * @param {BlobStore} blobStore
  114. * @return {Promise<FileData>}
  115. */
  116. async load(kind, blobStore) {
  117. if (kind === 'eager') return await this.toEager(blobStore)
  118. if (kind === 'lazy') return await this.toLazy(blobStore)
  119. if (kind === 'hollow') return await this.toHollow(blobStore)
  120. throw new Error('bad file data load kind: ' + kind)
  121. }
  122. /**
  123. * @see File#store
  124. * @function
  125. * @param {BlobStore} blobStore
  126. * @return {Promise<Object>} a raw HashFile
  127. * @abstract
  128. */
  129. async store(blobStore) {
  130. throw new Error('store not implemented for ' + JSON.stringify(this))
  131. }
  132. /**
  133. * @see File#getComments
  134. * @function
  135. * @return {CommentRawData[]}
  136. * @abstract
  137. */
  138. getComments() {
  139. throw new Error('getComments not implemented for ' + JSON.stringify(this))
  140. }
  141. }
  142. module.exports = FileData
  143. BinaryFileData = require('./binary_file_data')
  144. HashFileData = require('./hash_file_data')
  145. HollowBinaryFileData = require('./hollow_binary_file_data')
  146. HollowStringFileData = require('./hollow_string_file_data')
  147. LazyStringFileData = require('./lazy_string_file_data')
  148. StringFileData = require('./string_file_data')